123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383 |
- <?php
- namespace Gedmo\Tree\Mapping\Driver;
- use Gedmo\Mapping\Driver\AnnotationDriverInterface,
- Gedmo\Exception\InvalidMappingException;
- /**
- * This is an annotation mapping driver for Tree
- * behavioral extension. Used for extraction of extended
- * metadata from Annotations specificaly for Tree
- * extension.
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @package Gedmo.Tree.Mapping.Driver
- * @subpackage Annotation
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class Annotation implements AnnotationDriverInterface
- {
- /**
- * Annotation to define the tree type
- */
- const TREE = 'Gedmo\\Mapping\\Annotation\\Tree';
- /**
- * Annotation to mark field as one which will store left value
- */
- const LEFT = 'Gedmo\\Mapping\\Annotation\\TreeLeft';
- /**
- * Annotation to mark field as one which will store right value
- */
- const RIGHT = 'Gedmo\\Mapping\\Annotation\\TreeRight';
- /**
- * Annotation to mark relative parent field
- */
- const PARENT = 'Gedmo\\Mapping\\Annotation\\TreeParent';
- /**
- * Annotation to mark node level
- */
- const LEVEL = 'Gedmo\\Mapping\\Annotation\\TreeLevel';
- /**
- * Annotation to mark field as tree root
- */
- const ROOT = 'Gedmo\\Mapping\\Annotation\\TreeRoot';
- /**
- * Annotation to specify closure tree class
- */
- const CLOSURE = 'Gedmo\\Mapping\\Annotation\\TreeClosure';
- /**
- * Annotation to specify path class
- */
- const PATH = 'Gedmo\\Mapping\\Annotation\\TreePath';
- /**
- * Annotation to specify path source class
- */
- const PATH_SOURCE = 'Gedmo\\Mapping\\Annotation\\TreePathSource';
- /**
- * List of types which are valid for tree fields
- *
- * @var array
- */
- private $validTypes = array(
- 'integer',
- 'smallint',
- 'bigint',
- 'int'
- );
- /**
- * List of types which are valid for the path (materialized path strategy)
- *
- * @var array
- */
- private $validPathTypes = array(
- 'string',
- 'text'
- );
- /**
- * List of types which are valid for the path source (materialized path strategy)
- *
- * @var array
- */
- private $validPathSourceTypes = array(
- 'id',
- 'integer',
- 'smallint',
- 'bigint',
- 'string',
- 'int',
- 'float'
- );
- /**
- * List of tree strategies available
- *
- * @var array
- */
- private $strategies = array(
- 'nested',
- 'closure',
- 'materializedPath'
- );
- /**
- * Annotation reader instance
- *
- * @var object
- */
- private $reader;
- /**
- * original driver if it is available
- */
- protected $_originalDriver = null;
- /**
- * {@inheritDoc}
- */
- public function setAnnotationReader($reader)
- {
- $this->reader = $reader;
- }
- /**
- * {@inheritDoc}
- */
- public function readExtendedMetadata($meta, array &$config)
- {
- $class = $meta->getReflectionClass();
- if (!$class) {
- // based on recent doctrine 2.3.0-DEV maybe will be fixed in some way
- // this happens when running annotation driver in combination with
- // static reflection services. This is not the nicest fix
- $class = new \ReflectionClass($meta->name);
- }
- // class annotations
- if ($annot = $this->reader->getClassAnnotation($class, self::TREE)) {
- if (!in_array($annot->type, $this->strategies)) {
- throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
- }
- $config['strategy'] = $annot->type;
- }
- if ($annot = $this->reader->getClassAnnotation($class, self::CLOSURE)) {
- if (!class_exists($annot->class)) {
- throw new InvalidMappingException("Tree closure class: {$annot->class} does not exist.");
- }
- $config['closure'] = $annot->class;
- }
- // property annotations
- foreach ($class->getProperties() as $property) {
- if ($meta->isMappedSuperclass && !$property->isPrivate() ||
- $meta->isInheritedField($property->name) ||
- isset($meta->associationMappings[$property->name]['inherited'])
- ) {
- continue;
- }
- // left
- if ($this->reader->getPropertyAnnotation($property, self::LEFT)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'left' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$this->isValidField($meta, $field)) {
- throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
- }
- $config['left'] = $field;
- }
- // right
- if ($this->reader->getPropertyAnnotation($property, self::RIGHT)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'right' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$this->isValidField($meta, $field)) {
- throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
- }
- $config['right'] = $field;
- }
- // ancestor/parent
- if ($this->reader->getPropertyAnnotation($property, self::PARENT)) {
- $field = $property->getName();
- if (!$meta->isSingleValuedAssociation($field)) {
- throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
- }
- $config['parent'] = $field;
- }
- // root
- if ($this->reader->getPropertyAnnotation($property, self::ROOT)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$meta->getFieldMapping($field)) {
- throw new InvalidMappingException("Tree root field - [{$field}] type is not valid in class - {$meta->name}");
- }
- $config['root'] = $field;
- }
- // level
- if ($this->reader->getPropertyAnnotation($property, self::LEVEL)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'level' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$this->isValidField($meta, $field)) {
- throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
- }
- $config['level'] = $field;
- }
- // path
- if ($pathAnnotation = $this->reader->getPropertyAnnotation($property, self::PATH)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'path' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$this->isValidFieldForPath($meta, $field)) {
- throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
- }
- if (strlen($pathAnnotation->separator) > 1) {
- throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$pathAnnotation->separator} is invalid. It must be only one character long.");
- }
- $config['path'] = $field;
- $config['path_separator'] = $pathAnnotation->separator;
- }
- // path source
- if ($this->reader->getPropertyAnnotation($property, self::PATH_SOURCE)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'path_source' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$this->isValidFieldForPathSource($meta, $field)) {
- throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}");
- }
- $config['path_source'] = $field;
- }
- }
- if (!$meta->isMappedSuperclass && $config) {
- if (isset($config['strategy'])) {
- if (is_array($meta->identifier) && count($meta->identifier) > 1) {
- throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
- }
- $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
- $this->$method($meta, $config);
- } else {
- throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
- }
- }
- }
- /**
- * Checks if $field type is valid
- *
- * @param object $meta
- * @param string $field
- * @return boolean
- */
- protected function isValidField($meta, $field)
- {
- $mapping = $meta->getFieldMapping($field);
- return $mapping && in_array($mapping['type'], $this->validTypes);
- }
- /**
- * Checks if $field type is valid for Path field
- *
- * @param object $meta
- * @param string $field
- * @return boolean
- */
- protected function isValidFieldForPath($meta, $field)
- {
- $mapping = $meta->getFieldMapping($field);
- return $mapping && in_array($mapping['type'], $this->validPathTypes);
- }
- /**
- * Checks if $field type is valid for PathSource field
- *
- * @param object $meta
- * @param string $field
- * @return boolean
- */
- protected function isValidFieldForPathSource($meta, $field)
- {
- $mapping = $meta->getFieldMapping($field);
- return $mapping && in_array($mapping['type'], $this->validPathSourceTypes);
- }
- /**
- * Validates metadata for nested type tree
- *
- * @param object $meta
- * @param array $config
- * @throws InvalidMappingException
- * @return void
- */
- private function validateNestedTreeMetadata($meta, array $config)
- {
- $missingFields = array();
- if (!isset($config['parent'])) {
- $missingFields[] = 'ancestor';
- }
- if (!isset($config['left'])) {
- $missingFields[] = 'left';
- }
- if (!isset($config['right'])) {
- $missingFields[] = 'right';
- }
- if ($missingFields) {
- throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
- }
- }
- /**
- * Validates metadata for closure type tree
- *
- * @param object $meta
- * @param array $config
- * @throws InvalidMappingException
- * @return void
- */
- private function validateClosureTreeMetadata($meta, array $config)
- {
- $missingFields = array();
- if (!isset($config['parent'])) {
- $missingFields[] = 'ancestor';
- }
- if (!isset($config['closure'])) {
- $missingFields[] = 'closure class';
- }
- if ($missingFields) {
- throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
- }
- }
- /**
- * Validates metadata for materialized path type tree
- *
- * @param object $meta
- * @param array $config
- * @throws InvalidMappingException
- * @return void
- */
- private function validateMaterializedPathTreeMetadata($meta, array $config)
- {
- $missingFields = array();
- if (!isset($config['parent'])) {
- $missingFields[] = 'ancestor';
- }
- if (!isset($config['path'])) {
- $missingFields[] = 'path';
- }
- if (!isset($config['path_source'])) {
- $missingFields[] = 'path_source';
- }
- if ($missingFields) {
- throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
- }
- }
- /**
- * Passes in the mapping read by original driver
- *
- * @param $driver
- * @return void
- */
- public function setOriginalDriver($driver)
- {
- $this->_originalDriver = $driver;
- }
- }
|