From 157ef7a470eafa742c63fb8e12b0972fca5f95d7 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 18 Jan 2013 13:36:19 +0100 Subject: [PATCH 1/5] refactor rdf type factory to accept a list of mappers per type name --- .../CreatePHP/Metadata/RdfTypeFactory.php | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php index 3af3315..991b98e 100644 --- a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php +++ b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php @@ -23,7 +23,12 @@ class RdfTypeFactory /** * @var RdfMapperInterface */ - private $mapper; + private $defaultMapper; + + /** + * @var RdfMapperInterface[] + */ + private $mappers; /** * @var RdfDriverInterface @@ -33,12 +38,15 @@ class RdfTypeFactory private $loadedTypes = array(); /** - * @param RdfMapperInterface $mapper the mapper to use in this project + * @param RdfMapperInterface $defaultMapper the default mapper to use if there is no specific one + * @param RdfDriverInterface $driver the driver to load types from + * @param RdfMapperInterface[] $mappers rdf mappers per type name */ - public function __construct(RdfMapperInterface $mapper, RdfDriverInterface $driver) + public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterface $driver, $mappers = array()) { - $this->mapper = $mapper; + $this->defaultMapper = $defaultMapper; $this->driver = $driver; + $this->mappers = $mappers; } public function getTypeByObject($object) @@ -58,7 +66,8 @@ public function getTypeByObject($object) public function getTypeByName($name) { if (!isset($this->loadedTypes[$name])) { - $this->loadedTypes[$name] = $this->driver->loadType($name, $this->mapper, $this); + $mapper = $this->getMapper($name); + $this->loadedTypes[$name] = $this->driver->loadType($name, $mapper, $this); } // TODO: combine types from parent models... @@ -66,6 +75,18 @@ public function getTypeByName($name) return $this->loadedTypes[$name]; } + /** + * Get the mapper for type $name, or the defaultMapper if there is no specific mapper. + * + * @param string $name the type name for which to get the mapper + * + * @return RdfMapperInterface + */ + protected function getMapper($name) + { + return isset($this->mappers[$name]) ? $this->mappers[$name] : $this->defaultMapper; + } + /** * Get the type information by (full) RDF name * From 1622bf55c147aef1edf1857fb75898495dbfaf56 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 18 Jan 2013 16:15:25 +0100 Subject: [PATCH 2/5] fix typo: this RdfTypeFactory::mapper is now defaultMapper --- src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php index 991b98e..b3a66fa 100644 --- a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php +++ b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php @@ -52,7 +52,7 @@ public function __construct(RdfMapperInterface $defaultMapper, RdfDriverInterfac public function getTypeByObject($object) { return $this->getTypeByName( - $this->driver->objectToName($object, $this->mapper) + $this->driver->objectToName($object, $this->defaultMapper) ); } From 194d1166e99bf33ec7864db0376f271bd6e6c3b3 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Fri, 18 Jan 2013 17:36:02 +0100 Subject: [PATCH 3/5] remove static reference to mapper --- src/Midgard/CreatePHP/RestService.php | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/Midgard/CreatePHP/RestService.php b/src/Midgard/CreatePHP/RestService.php index f295cb3..08ee6b1 100644 --- a/src/Midgard/CreatePHP/RestService.php +++ b/src/Midgard/CreatePHP/RestService.php @@ -28,13 +28,6 @@ class RestService const HTTP_PUT = 'put'; const HTTP_DELETE = 'delete'; - /** - * The mapper object - * - * @var RdfMapperInterface - */ - protected $_mapper; - /** * Custom workflows for post, put, delete or get * @@ -132,7 +125,7 @@ public function run($data, TypeInterface $type, $subject = null, $method = null) $subject = $_GET["subject"]; } // TODO: workflows should expect subject rather than instance - $object = $this->_mapper->getBySubject($subject); + $object = $type->getMapper()->getBySubject($subject); return $this->_workflows[$method]->run($object); } @@ -170,12 +163,12 @@ private function _handleCreate($received_data, TypeInterface $type) $about = $received_data[$this->jsonldEncode($rdf)]; if (! empty($about)) { - $parent = $this->_mapper->getBySubject($this->jsonldDecode(current($about))); + $parent = $type->getMapper()->getBySubject($this->jsonldDecode(current($about))); break; } } - $object = $this->_mapper->prepareObject($type, $parent); + $object = $type->getMapper()->prepareObject($type, $parent); $entity = $type->createWithObject($object); return $this->_storeData($received_data, $entity); @@ -189,7 +182,7 @@ private function _handleUpdate($data, TypeInterface $type, $subject = null) if (null === $subject) { $subject = $this->jsonldDecode($data['@subject']); } - $object = $this->_mapper->getBySubject($subject); + $object = $type->getMapper()->getBySubject($subject); $entity = $type->createWithObject($object); return $this->_storeData($data, $entity); } @@ -208,11 +201,11 @@ private function _storeData($new_values, EntityInterface $entity) $expanded_name = $this->_expandPropertyName($rdf_name, $entity); if (array_key_exists($expanded_name, $new_values)) { - $object = $this->_mapper->setPropertyValue($object, $node, $new_values[$expanded_name]); + $object = $entity->getMapper()->setPropertyValue($object, $node, $new_values[$expanded_name]); } } - if ($this->_mapper->store($entity)) + if ($entity->getMapper()->store($entity)) { return $this->_convertToJsonld($new_values, $object, $entity); } @@ -225,7 +218,7 @@ private function _convertToJsonld($data, $object, EntityInterface $entity) // lazy: copy stuff from the sent json-ld to not have to rebuild everything. $jsonld = $data; - $jsonld['@subject'] = $this->jsonldEncode($this->_mapper->createSubject($object)); + $jsonld['@subject'] = $this->jsonldEncode($entity->getMapper()->createSubject($object)); foreach ($entity->getChildDefinitions() as $node) { if (!$node instanceof PropertyInterface) { continue; @@ -236,7 +229,7 @@ private function _convertToJsonld($data, $object, EntityInterface $entity) $expanded_name = $this->_expandPropertyName($rdf_name, $entity); if (array_key_exists($expanded_name, $jsonld)) { - $jsonld[$expanded_name] = $this->_mapper->getPropertyValue($object, $node); + $jsonld[$expanded_name] = $entity->getMapper()->getPropertyValue($object, $node); } } From 10d3fab4dd2ca6947ef18bc19d28510f2d419885 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Sat, 19 Jan 2013 10:26:07 +0100 Subject: [PATCH 4/5] fix tests --- tests/Test/Midgard/CreatePHP/RestServiceTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Test/Midgard/CreatePHP/RestServiceTest.php b/tests/Test/Midgard/CreatePHP/RestServiceTest.php index 55b10a8..7bb3cfb 100644 --- a/tests/Test/Midgard/CreatePHP/RestServiceTest.php +++ b/tests/Test/Midgard/CreatePHP/RestServiceTest.php @@ -75,6 +75,10 @@ public function setUp() ->method('getChildDefinitions') ->will($this->returnValue(array('title' => $this->property, 'children' => $this->collection))) ; + $this->type->expects($this->any()) + ->method('getMapper') + ->will($this->returnValue($this->mapper)) + ; $this->child_type->expects($this->any()) ->method('getVocabularies') @@ -98,6 +102,10 @@ public function setUp() ->method('getVocabularies') ->will($this->returnValue(array('dcterms' => 'http://purl.org/dc/terms/'))) ; + $this->entity->expects($this->any()) + ->method('getMapper') + ->will($this->returnValue($this->mapper)) + ; $this->property->expects($this->any()) ->method('getProperty') ->will($this->returnValue('dcterms:title')) From 24e4ed75e47a252e19c86a0711e31b563d410d09 Mon Sep 17 00:00:00 2001 From: Adrien Nicolet Date: Wed, 23 Jan 2013 15:20:01 +0100 Subject: [PATCH 5/5] fix CS after lsmith77 review --- src/Midgard/CreatePHP/RestService.php | 3 +-- tests/Test/Midgard/CreatePHP/Model.php | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Midgard/CreatePHP/RestService.php b/src/Midgard/CreatePHP/RestService.php index 08ee6b1..66242d3 100644 --- a/src/Midgard/CreatePHP/RestService.php +++ b/src/Midgard/CreatePHP/RestService.php @@ -205,8 +205,7 @@ private function _storeData($new_values, EntityInterface $entity) } } - if ($entity->getMapper()->store($entity)) - { + if ($entity->getMapper()->store($entity)) { return $this->_convertToJsonld($new_values, $object, $entity); } diff --git a/tests/Test/Midgard/CreatePHP/Model.php b/tests/Test/Midgard/CreatePHP/Model.php index d1b582f..f22253c 100644 --- a/tests/Test/Midgard/CreatePHP/Model.php +++ b/tests/Test/Midgard/CreatePHP/Model.php @@ -4,12 +4,28 @@ class Model { + private $title; + private $content; + private $subject; + + public function __construct($title = 'the model title', + $content = 'the model content', + $subject = '/the/subject/model') + { + $this->title = $title; + $this->content = $content; + $this->subject = $subject; + } public function getTitle() { - return 'the title'; + return $this->title; } public function getContent() { - return 'the content'; + return $this->content; + } + public function getSubject() + { + return $this->subject; } } \ No newline at end of file