Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Composition #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
ChangeLog
=========

0.1.1 (????-??-??)
------------------

* Now allowing users to register atom (de-)serializers on their existing
`Sabre\Xml\Service` objects.


0.1.0 (2016-02-14)
------------------

Expand Down
38 changes: 24 additions & 14 deletions lib/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,31 @@ class Service extends \Sabre\Xml\Service {
*/
function __construct() {

$this->namespaceMap[self::ATOM_NS] = self::ATOM_DEFAULT_PREFIX;
self::setupService($this);

}

/**
* This method takes an existing Sabre/Xml/Service object and registers
* all the class mappings for the atom format.
*
* @param Sabre\Xml\Service $service
* @return void
*/
static function setupService(\Sabre\Xml\Service $service) {

$service->namespaceMap[self::ATOM_NS] = self::ATOM_DEFAULT_PREFIX;
$atom = '{' . self::ATOM_NS . '}';

// The following elements are all simple xml elements, and we can use
// the VO system for mapping from PHP object to XML element.
$this->mapValueObject($atom . 'feed', Element\Feed::class);
$this->mapValueObject($atom . 'entry', Element\Entry::class);
$this->mapValueObject($atom . 'source', Element\Source::class);
$this->mapValueObject($atom . 'author', Element\Person::class);
$this->mapValueObject($atom . 'contributer', Element\Person::class);
$service->mapValueObject($atom . 'feed', Element\Feed::class);
$service->mapValueObject($atom . 'entry', Element\Entry::class);
$service->mapValueObject($atom . 'source', Element\Source::class);
$service->mapValueObject($atom . 'author', Element\Person::class);
$service->mapValueObject($atom . 'contributer', Element\Person::class);

// The following elements need custom (de-)serializers
// The following elements need custom (de-)serializers

// This serializer takes an object and encodes all its properties as
// XML attributes.
Expand All @@ -53,7 +65,7 @@ function($item) {
};

// This deserializer takes all attributes from an xml element and
// turns the into properties of a class.
// turns the into properties of a class.
$attributeReader = function($reader, $class) {

$attributes = $reader->parseAttributes();
Expand All @@ -71,16 +83,14 @@ function($item) {

};



// atom:category
$this->classMap[Element\Category::class] = $attributeWriter;
$this->elementMap[$atom . 'category'] = function($reader) use ($attributeReader) {
$service->classMap[Element\Category::class] = $attributeWriter;
$service->elementMap[$atom . 'category'] = function($reader) use ($attributeReader) {
return $attributeReader($reader, Element\Category::class);
};
// atom:link
$this->classMap[Element\Link::class] = $attributeWriter;
$this->elementMap[$atom . 'link'] = function($reader) use ($attributeReader) {
$service->classMap[Element\Link::class] = $attributeWriter;
$service->elementMap[$atom . 'link'] = function($reader) use ($attributeReader) {
return $attributeReader($reader, Element\Link::class);
};

Expand Down
2 changes: 1 addition & 1 deletion lib/Version.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ class Version {
/**
* Full version number
*/
const VERSION = '0.1.0';
const VERSION = '0.1.1';

}