diff --git a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php index 3af3315..b3a66fa 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,18 +38,21 @@ 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) { return $this->getTypeByName( - $this->driver->objectToName($object, $this->mapper) + $this->driver->objectToName($object, $this->defaultMapper) ); } @@ -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 * diff --git a/src/Midgard/CreatePHP/RestService.php b/src/Midgard/CreatePHP/RestService.php index f295cb3..66242d3 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,12 +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 +217,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 +228,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); } } 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'))