From 62a74d344c09dde6f461306de8e078677cb7ae66 Mon Sep 17 00:00:00 2001 From: Jonathon Walz Date: Tue, 28 Oct 2014 10:35:30 -0500 Subject: [PATCH 1/6] Add chain mapper (prepareObject not supported) --- .../CreatePHP/Mapper/AbstractRdfMapper.php | 13 +- .../Mapper/BaseDoctrineRdfMapper.php | 26 ++- .../CreatePHP/Mapper/ChainRdfMapper.php | 165 ++++++++++++++++++ .../CreatePHP/Mapper/DoctrineOrmMapper.php | 2 +- .../CreatePHP/Metadata/AbstractRdfDriver.php | 13 -- .../CreatePHP/Metadata/RdfDriverInterface.php | 9 - .../CreatePHP/Metadata/RdfTypeFactory.php | 2 +- .../CreatePHP/RdfChainableMapperInterface.php | 37 ++++ src/Midgard/CreatePHP/RdfMapperInterface.php | 36 ++-- .../Extension/Twig/CreatephpExtensionTest.php | 13 +- tests/__files/MockMapper.php | 4 +- 11 files changed, 261 insertions(+), 59 deletions(-) create mode 100644 src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php create mode 100644 src/Midgard/CreatePHP/RdfChainableMapperInterface.php diff --git a/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php b/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php index 4e48e60..8f2181b 100644 --- a/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php @@ -145,14 +145,17 @@ protected function getField($object, RdfElementDefinitionInterface $child) } /** - * A dummy classname canonicalizer: returns the name unmodified. + * {@inheritDoc} * - * @param string $className - * @return string exactly the same as $className + * The default implementation uses get_class on objects */ - public function canonicalName($className) + public function objectToName($object) { - return $className; + if (! is_object($object)) { + throw new \RuntimeException("$object is not an object"); + } + + return get_class($object); } /** diff --git a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php index 58b153d..b4bc328 100644 --- a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php @@ -10,6 +10,7 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ManagerRegistry; +use Midgard\CreatePHP\RdfChainableMapperInterface; use Midgard\CreatePHP\Entity\EntityInterface; use Midgard\CreatePHP\Entity\PropertyInterface; @@ -20,7 +21,7 @@ * * @package Midgard.CreatePHP */ -abstract class BaseDoctrineRdfMapper extends AbstractRdfMapper +abstract class BaseDoctrineRdfMapper extends AbstractRdfMapper implements RdfChainableMapperInterface { /** @var ObjectManager */ protected $om; @@ -58,13 +59,14 @@ public function store(EntityInterface $entity) * * use getRealClass if className names a doctrine proxy class. */ - public function canonicalName($className) + public function objectToName($object) { - $refl = new \ReflectionClass($className); + $refl = new \ReflectionClass($object); + $className = $refl->getName(); if (in_array('Doctrine\\Common\\Persistence\\Proxy', $refl->getInterfaceNames())) { $className = \Doctrine\Common\Util\ClassUtils::getRealClass($className); } - + return $className; } @@ -94,4 +96,20 @@ public function setPropertyValue($object, PropertyInterface $property, $value) return parent::setPropertyValue($object, $property, $value); } + + /** + * {@inheritDoc} + */ + public function supports($object) + { + return $this->om->contains($object); + } + + /** + * {@inheritDoc} + */ + public function supportsCreate($object) + { + return false; + } } \ No newline at end of file diff --git a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php new file mode 100644 index 0000000..21e82a8 --- /dev/null +++ b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php @@ -0,0 +1,165 @@ +mappers[$mapperKey] = $mapper; + } + + /** + * Get the mapper than can handle object. + * + * @param mixed $object + * @return RdfChainableMapperInterface + * @throws RuntimeException when no mapper can handle the object + */ + protected function getMapperForObject($object) + { + foreach ($this->mappers as $mapper) + { + if ($mapper->supports($object)) { + return $mapper; + } + } + + $hash = spl_object_hash($object); + if (isset($this->createdObjects[$hash])) { + return $this->createdObjects[$hash]; + } + + throw new RuntimeException("No mapper can create a subject for object."); + } + /** + * {@inheritdoc} + */ + public function setPropertyValue($object, PropertyInterface $property, $value) + { + return $this->getMapperForObject($object)->setPropertyValue($object, $property, $value); + } + + /** + * {@inheritdoc} + */ + public function getPropertyValue($object, PropertyInterface $property) + { + return $this->getMapperForObject($object)->getPropertyValue($object, $property); + } + + /** + * {@inheritdoc} + */ + public function isEditable($object) + { + return $this->getMapperForObject($object)->isEditable($object); + } + + /** + * {@inheritdoc} + */ + public function getChildren($object, CollectionInterface $collection) + { + return $this->getMapperForObject($object)->getChildren($object, $collection); + } + + /** + * {@inheritdoc} + */ + public function objectToName($object) + { + return $this->getMapperForObject($object)->objectToName($object); + } + + /** + * {@inheritdoc} + */ + public function prepareObject(TypeInterface $controller, $parent = null) + { + throw new RuntimeException("Not implemented yet."); + } + + /** + * {@inheritdoc} + */ + public function store(EntityInterface $entity) + { + return $this->getMapperForObject($entity->getObject())->store($entity); + } + + /** + * {@inheritdoc} + */ + public function getBySubject($subject) + { + list($mapperKey, $mapperSubject) = explode('|', $subject, 2); + + if (!isset($this->mappers[$mapperKey])) + { + throw new RuntimeException("Invalid subject: $subject"); + } + + $object = $this->mappers[$mapperKey]->getBySubject($mapperSubject); + + return $object; + } + + /** + * {@inheritdoc} + */ + public function createSubject($object) + { + foreach ($this->mappers as $mapperKey => $mapper) + { + if ($mapper->supports($object)) { + return $mapperKey . '|' . $mapper->createSubject($object); + } + } + + throw new RuntimeException("No mapper can create a subject for object."); + } + + /** + * {@inheritdoc} + */ + public function orderChildren(EntityInterface $entity, CollectionInterface $node, $expectedOrder) + { + return $this->getMapperForObject($entity->getObject())->orderChildren($entity, $node, $expectedOrder); + } +} diff --git a/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php b/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php index 90e7c34..d855fdd 100644 --- a/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php +++ b/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php @@ -55,7 +55,7 @@ public function createSubject($object) $idstring = implode('|', $key); - return str_replace('\\', '-', $this->canonicalName(get_class($object))) . "|$idstring"; + return str_replace('\\', '-', $this->objectToName($object)) . "|$idstring"; } /** diff --git a/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php b/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php index f855c68..26ca1ed 100644 --- a/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php +++ b/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php @@ -71,19 +71,6 @@ protected abstract function getConfig($element); */ protected abstract function getAttributes($element); - /** - * {@inheritDoc} - * - * The default implementation uses get_class on objects - */ - public function objectToName($object, RdfMapperInterface $mapper) - { - if (! is_object($object)) { - throw new \RuntimeException("$object is not an object"); - } - return $mapper->canonicalName(get_class($object)); - } - /** * Create a type instance. * diff --git a/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php b/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php index e564895..8af6e9c 100644 --- a/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php +++ b/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php @@ -33,15 +33,6 @@ interface RdfDriverInterface */ public function loadType($name, RdfMapperInterface $mapper, RdfTypeFactory $typeFactory); - /** - * Get the name of an object - * - * @param object $object - * - * @return string the canonical name of this object - */ - public function objectToName($object, RdfMapperInterface $mapper); - /** * Gets a map of rdf types to names with all types known to this driver. * diff --git a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php index 3af3315..193cfb7 100644 --- a/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php +++ b/src/Midgard/CreatePHP/Metadata/RdfTypeFactory.php @@ -44,7 +44,7 @@ public function __construct(RdfMapperInterface $mapper, RdfDriverInterface $driv public function getTypeByObject($object) { return $this->getTypeByName( - $this->driver->objectToName($object, $this->mapper) + $this->mapper->objectToName($object) ); } diff --git a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php new file mode 100644 index 0000000..91fa4de --- /dev/null +++ b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php @@ -0,0 +1,37 @@ +will($this->returnValue($collection->getChildren())) ; $this->mapper->expects($this->any()) - ->method('canonicalName') - ->will($this->returnCallback(array($this, 'canonicalNameCallback'))) + ->method('objectToName') + ->will($this->returnCallback(array($this, 'objectToNameCallback'))) ; $xml = $this->renderXml('collection.twig'); @@ -147,9 +147,8 @@ public function testCollectionsTemplate() $this->assertEquals('sioc:content', $xml->ul->li[1]->div[1]['property']); } - public function canonicalNameCallback($className) { - - if ($className === 'Test\Midgard\CreatePHP\Collection') { + public function objectToNameCallback($object) { + if (get_class($object) === 'Test\Midgard\CreatePHP\Collection') { return 'Test\Midgard\CreatePHP\Collection'; } else { return 'Test\Midgard\CreatePHP\Model'; @@ -196,8 +195,8 @@ private function prepareBasicTest(){ ->will($this->returnValue('/the/subject')) ; $this->mapper->expects($this->any()) - ->method('canonicalName') - ->with(get_class($model)) + ->method('objectToName') + ->with($model) ->will($this->returnValue(get_class($model))) ; } diff --git a/tests/__files/MockMapper.php b/tests/__files/MockMapper.php index 4385fd5..acf2f8c 100644 --- a/tests/__files/MockMapper.php +++ b/tests/__files/MockMapper.php @@ -55,9 +55,9 @@ public function store(EntityInterface $entity) } - public function canonicalName($className) + public function objectToName($object) { - return $className; + return get_class($object); } public function getBySubject($identifier) From 71120bdd5d326df4c7cdc15c789074ee977d6611 Mon Sep 17 00:00:00 2001 From: Jonathon Walz Date: Fri, 30 Jan 2015 15:28:17 -0600 Subject: [PATCH 2/6] Implement object creating in the chain mapper --- .../CreatePHP/Mapper/AbstractRdfMapper.php | 21 ++++++++++++++----- .../Mapper/BaseDoctrineRdfMapper.php | 14 +++++++++++-- .../CreatePHP/Mapper/ChainRdfMapper.php | 12 ++++++++++- .../CreatePHP/RdfChainableMapperInterface.php | 6 ++++-- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php b/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php index 8f2181b..c82395a 100644 --- a/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php @@ -58,10 +58,7 @@ public function prepareObject(TypeInterface $type, $parent = null) if ($parent !== null) { throw new \Exception('Parent is not null, please extend this method to configure the parent'); } - list($prefix, $shortname) = explode(':', $type->getRdfType()); - $ns = $type->getVocabularies(); - $ns = $ns[$prefix]; - $name = $ns.$shortname; + $name = $this->getTypeMapKey($type); if (isset($this->typeMap[$name])) { $class = $this->typeMap[$name]; return new $class; @@ -69,6 +66,20 @@ public function prepareObject(TypeInterface $type, $parent = null) throw new \Exception('No information on ' . $name); } + /** + * Get's the possible key used in the typemap for the type + * + * @param TypeInterface $type + * @return string + */ + protected function getTypeMapKey(TypeInterface $type) + { + list($prefix, $shortname) = explode(':', $type->getRdfType()); + $ns = $type->getVocabularies(); + $ns = $ns[$prefix]; + return $ns.$shortname; + } + /** * {@inheritDoc} */ @@ -154,7 +165,7 @@ public function objectToName($object) if (! is_object($object)) { throw new \RuntimeException("$object is not an object"); } - + return get_class($object); } diff --git a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php index b4bc328..5eaed84 100644 --- a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php @@ -10,9 +10,11 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ManagerRegistry; +use Doctrine\Common\Persistence\Mapping\MappingException; use Midgard\CreatePHP\RdfChainableMapperInterface; use Midgard\CreatePHP\Entity\EntityInterface; use Midgard\CreatePHP\Entity\PropertyInterface; +use Midgard\CreatePHP\Type\TypeInterface; /** * Base mapper for doctrine, removing the proxy class names in canonicalClassName @@ -66,7 +68,7 @@ public function objectToName($object) if (in_array('Doctrine\\Common\\Persistence\\Proxy', $refl->getInterfaceNames())) { $className = \Doctrine\Common\Util\ClassUtils::getRealClass($className); } - + return $className; } @@ -108,8 +110,16 @@ public function supports($object) /** * {@inheritDoc} */ - public function supportsCreate($object) + public function supportsCreate(TypeInterface $type) { + $name = $this->getTypeMapKey($type); + if (isset($this->typeMap[$name])) { + try { + $metadata = $this->om->getClassMetadata($this->typeMap[$name]); + return is_object($metadata); + } catch (MappingException $e) { } + } + return false; } } \ No newline at end of file diff --git a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php index 21e82a8..4153a10 100644 --- a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php @@ -112,7 +112,17 @@ public function objectToName($object) */ public function prepareObject(TypeInterface $controller, $parent = null) { - throw new RuntimeException("Not implemented yet."); + foreach ($this->mappers as $mapper) + { + if ($mapper->supportsCreate($controller)) { + $object = $mapper->prepareObject($controller, $parent); + $this->createdObjects[spl_object_hash($object)] = $mapper; + + return $object; + } + } + + throw new RuntimeException("No mapper can create an object for type."); } /** diff --git a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php index 91fa4de..bab08e3 100644 --- a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php +++ b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php @@ -8,6 +8,8 @@ namespace Midgard\CreatePHP; +use Midgard\CreatePHP\Type\TypeInterface; + /** * Map from CreatePHP to your domain objects * @@ -29,9 +31,9 @@ public function supports($object); /** * Get if this mapper can create this type. * - * @param mixed $object + * @param TypeInterface $type * * @return boolean */ - public function supportsCreate($object); + public function supportsCreate(TypeInterface $type); } From d10bef1c145a88a3ef36202027a113ddad3d6262 Mon Sep 17 00:00:00 2001 From: Jonathon Walz Date: Mon, 9 Feb 2015 10:47:34 -0600 Subject: [PATCH 3/6] Deprecate rather than remove canonicalName and update documentation --- CHANGELOG.md | 8 ++++++++ .../CreatePHP/Mapper/AbstractRdfMapper.php | 14 +++++++++++++- .../CreatePHP/Mapper/BaseDoctrineRdfMapper.php | 16 +++++++++++++--- src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php | 8 ++++++++ .../CreatePHP/Metadata/AbstractRdfDriver.php | 10 ++++++++++ .../CreatePHP/Metadata/RdfDriverInterface.php | 11 +++++++++++ .../CreatePHP/RdfChainableMapperInterface.php | 4 ++-- src/Midgard/CreatePHP/RdfMapperInterface.php | 11 +++++++++++ 8 files changed, 76 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f4c05c..48e8267 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,14 @@ Changelog ========= +1.1 +----- +* Added ChainRdfMapper and ChainedRdfMapperInterface to allow using more than one mapper in parallel. +* BC break: If you implemented your own mapper, note that RdfMapperInterface::objectToName was added + +1.0 +----- + * **2014-01-13**: Moved workflows from Manager to RestService. If you used the Manager before, please update your code to use the RestService. Before: diff --git a/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php b/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php index c82395a..4ba3ea7 100644 --- a/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/AbstractRdfMapper.php @@ -155,6 +155,18 @@ protected function getField($object, RdfElementDefinitionInterface $child) throw new \Exception('Can not find anything called ' . $child->getIdentifier() . ' on ' . $typename); } + /** + * A dummy classname canonicalizer: returns the name unmodified. + * + * @param string $className + * @return string exactly the same as $className + * @deprecated Deprecated in 1.1 use objectToName instead. + */ + public function canonicalName($className) + { + return $className; + } + /** * {@inheritDoc} * @@ -166,7 +178,7 @@ public function objectToName($object) throw new \RuntimeException("$object is not an object"); } - return get_class($object); + return $this->canonicalName(get_class($object)); } /** diff --git a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php index 5eaed84..328f21b 100644 --- a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php @@ -11,6 +11,7 @@ use Doctrine\Common\Persistence\ObjectManager; use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\Common\Persistence\Mapping\MappingException; +use Doctrine\Common\Util\ClassUtils; use Midgard\CreatePHP\RdfChainableMapperInterface; use Midgard\CreatePHP\Entity\EntityInterface; use Midgard\CreatePHP\Entity\PropertyInterface; @@ -61,10 +62,9 @@ public function store(EntityInterface $entity) * * use getRealClass if className names a doctrine proxy class. */ - public function objectToName($object) + public function canonicalName($className) { - $refl = new \ReflectionClass($object); - $className = $refl->getName(); + $refl = new \ReflectionClass($className); if (in_array('Doctrine\\Common\\Persistence\\Proxy', $refl->getInterfaceNames())) { $className = \Doctrine\Common\Util\ClassUtils::getRealClass($className); } @@ -72,6 +72,16 @@ public function objectToName($object) return $className; } + /** + * {@inheritDoc} + * + * use getRealClass if className names a doctrine proxy class. + */ + public function objectToName($object) + { + return $this->canonicalName(ClassUtils::getClass($object)); + } + /** * {@inheritDoc} */ diff --git a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php index 4153a10..6f90eef 100644 --- a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php @@ -99,6 +99,14 @@ public function getChildren($object, CollectionInterface $collection) return $this->getMapperForObject($object)->getChildren($object, $collection); } + /** + * {@inheritdoc} + */ + public function canonicalName($className) + { + return $className; + } + /** * {@inheritdoc} */ diff --git a/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php b/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php index 26ca1ed..819a473 100644 --- a/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php +++ b/src/Midgard/CreatePHP/Metadata/AbstractRdfDriver.php @@ -71,6 +71,16 @@ protected abstract function getConfig($element); */ protected abstract function getAttributes($element); + /** + * {@inheritDoc} + * + * @deprecated Deprecated in 1.1 call on the mapper object instead. + */ + public function objectToName($object, RdfMapperInterface $mapper) + { + return $mapper->objectToName($object); + } + /** * Create a type instance. * diff --git a/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php b/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php index 8af6e9c..52e148e 100644 --- a/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php +++ b/src/Midgard/CreatePHP/Metadata/RdfDriverInterface.php @@ -33,6 +33,17 @@ interface RdfDriverInterface */ public function loadType($name, RdfMapperInterface $mapper, RdfTypeFactory $typeFactory); + /** + * Get the name of an object + * + * @param object $object + * + * @return string the canonical name of this object + * + * @deprecated Deprecated in 1.1 call on the mapper object instead. + */ + public function objectToName($object, RdfMapperInterface $mapper); + /** * Gets a map of rdf types to names with all types known to this driver. * diff --git a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php index bab08e3..cec90f3 100644 --- a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php +++ b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php @@ -11,9 +11,9 @@ use Midgard\CreatePHP\Type\TypeInterface; /** - * Map from CreatePHP to your domain objects + * Mappers that implement this interface can be used the the chain mapper. * - * You can have a mapper per type or a generic mapper that handles all types. + * Tests whether the mapper supports and object or can create an object of a type. * * @package Midgard.CreatePHP */ diff --git a/src/Midgard/CreatePHP/RdfMapperInterface.php b/src/Midgard/CreatePHP/RdfMapperInterface.php index cecf7ab..fd8eb78 100644 --- a/src/Midgard/CreatePHP/RdfMapperInterface.php +++ b/src/Midgard/CreatePHP/RdfMapperInterface.php @@ -62,6 +62,17 @@ public function isEditable($object); */ public function getChildren($object, CollectionInterface $collection); + /** + * Ensure the parameter is transformed into the canonical name string for + * the passed parameter. + * + * @param string $name a name as passed to the RDF type factory + * + * @return string the canonical name + * @deprecated Deprecated in 1.1 use objectToName instead. + */ + public function canonicalName($className); + /** * Instantiate a new object for the specified RDFa type * From 9625fc5b362703ce8beb7cbe34b0380ed4deb9bf Mon Sep 17 00:00:00 2001 From: Jonathon Walz Date: Mon, 9 Feb 2015 10:51:07 -0600 Subject: [PATCH 4/6] Update branch alias to 1.1-dev --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index efdc28a..5ecc131 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ }, "extra": { "branch-alias": { - "dev-master": "0.9-dev" + "dev-master": "1.1-dev" } } } From fe810de624db92e491d1248c542eb262e2a11946 Mon Sep 17 00:00:00 2001 From: Jonathon Walz Date: Mon, 9 Feb 2015 11:00:23 -0600 Subject: [PATCH 5/6] Add missing canonicalname function back to mock mapper --- tests/__files/MockMapper.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/__files/MockMapper.php b/tests/__files/MockMapper.php index acf2f8c..b4ba10e 100644 --- a/tests/__files/MockMapper.php +++ b/tests/__files/MockMapper.php @@ -55,6 +55,11 @@ public function store(EntityInterface $entity) } + public function canonicalName($className) + { + return $className; + } + public function objectToName($object) { return get_class($object); From 88fe97e80637469680bbc7d33a4252ddd9d1b01c Mon Sep 17 00:00:00 2001 From: Jonathon Walz Date: Tue, 10 Feb 2015 16:21:53 -0600 Subject: [PATCH 6/6] Fix code style and better exception messages --- .../Mapper/BaseDoctrineRdfMapper.php | 6 ++-- .../CreatePHP/Mapper/ChainRdfMapper.php | 28 ++++++++----------- .../CreatePHP/Mapper/DoctrineOrmMapper.php | 2 +- .../CreatePHP/RdfChainableMapperInterface.php | 1 - 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php index 328f21b..8915de0 100644 --- a/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/BaseDoctrineRdfMapper.php @@ -126,10 +126,12 @@ public function supportsCreate(TypeInterface $type) if (isset($this->typeMap[$name])) { try { $metadata = $this->om->getClassMetadata($this->typeMap[$name]); + return is_object($metadata); - } catch (MappingException $e) { } + } catch (MappingException $e) { + } } return false; } -} \ No newline at end of file +} diff --git a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php index 6f90eef..64aeb45 100644 --- a/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php +++ b/src/Midgard/CreatePHP/Mapper/ChainRdfMapper.php @@ -2,15 +2,13 @@ namespace Midgard\CreatePHP\Mapper; - use Midgard\CreatePHP\RdfChainableMapperInterface; use Midgard\CreatePHP\RdfMapperInterface; use Midgard\CreatePHP\Entity\PropertyInterface; use Midgard\CreatePHP\Entity\CollectionInterface; use Midgard\CreatePHP\Type\TypeInterface; use Midgard\CreatePHP\Entity\EntityInterface; - -use \RuntimeException; +use RuntimeException; /** * Looks at all registered mappers to find one that can handle objects. @@ -37,7 +35,7 @@ class ChainRdfMapper implements RdfMapperInterface * Register a mapper with a key. The key will be prefixed to all subjects. * * @param RdfChainableMapperInterface $mapper - * @param string $mapperKey + * @param string $mapperKey */ public function registerMapper(RdfChainableMapperInterface $mapper, $mapperKey) { @@ -47,14 +45,13 @@ public function registerMapper(RdfChainableMapperInterface $mapper, $mapperKey) /** * Get the mapper than can handle object. * - * @param mixed $object + * @param mixed $object * @return RdfChainableMapperInterface - * @throws RuntimeException when no mapper can handle the object + * @throws RuntimeException when no mapper can handle the object */ protected function getMapperForObject($object) { - foreach ($this->mappers as $mapper) - { + foreach ($this->mappers as $mapper) { if ($mapper->supports($object)) { return $mapper; } @@ -120,8 +117,7 @@ public function objectToName($object) */ public function prepareObject(TypeInterface $controller, $parent = null) { - foreach ($this->mappers as $mapper) - { + foreach ($this->mappers as $mapper) { if ($mapper->supportsCreate($controller)) { $object = $mapper->prepareObject($controller, $parent); $this->createdObjects[spl_object_hash($object)] = $mapper; @@ -130,7 +126,7 @@ public function prepareObject(TypeInterface $controller, $parent = null) } } - throw new RuntimeException("No mapper can create an object for type."); + throw new RuntimeException(sprintf('None of the registered mappers can create an object of type %s', $controller->getRdfType())); } /** @@ -148,8 +144,7 @@ public function getBySubject($subject) { list($mapperKey, $mapperSubject) = explode('|', $subject, 2); - if (!isset($this->mappers[$mapperKey])) - { + if (!isset($this->mappers[$mapperKey])) { throw new RuntimeException("Invalid subject: $subject"); } @@ -163,14 +158,13 @@ public function getBySubject($subject) */ public function createSubject($object) { - foreach ($this->mappers as $mapperKey => $mapper) - { + foreach ($this->mappers as $mapperKey => $mapper) { if ($mapper->supports($object)) { - return $mapperKey . '|' . $mapper->createSubject($object); + return $mapperKey.'|'.$mapper->createSubject($object); } } - throw new RuntimeException("No mapper can create a subject for object."); + throw new RuntimeException(sprintf('None of the registered mappers can create the subject for object of class %s', get_class($object))); } /** diff --git a/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php b/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php index d855fdd..2d8f3cb 100644 --- a/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php +++ b/src/Midgard/CreatePHP/Mapper/DoctrineOrmMapper.php @@ -55,7 +55,7 @@ public function createSubject($object) $idstring = implode('|', $key); - return str_replace('\\', '-', $this->objectToName($object)) . "|$idstring"; + return str_replace('\\', '-', $this->objectToName($object))."|$idstring"; } /** diff --git a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php index cec90f3..d0c28a9 100644 --- a/src/Midgard/CreatePHP/RdfChainableMapperInterface.php +++ b/src/Midgard/CreatePHP/RdfChainableMapperInterface.php @@ -5,7 +5,6 @@ * @license Dual licensed under the MIT (MIT-LICENSE.txt) and LGPL (LGPL-LICENSE.txt) licenses. * @package Midgard.CreatePHP */ - namespace Midgard\CreatePHP; use Midgard\CreatePHP\Type\TypeInterface;