123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- <?php
- namespace Gedmo\Tree\Mapping\Driver;
- use Gedmo\Mapping\Driver,
- Doctrine\Common\Annotations\AnnotationReader,
- 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 Driver
- {
- /**
- * Annotation to define the tree type
- */
- const ANNOTATION_TREE = 'Gedmo\Tree\Mapping\Tree';
-
- /**
- * Annotation to mark field as one which will store left value
- */
- const ANNOTATION_LEFT = 'Gedmo\Tree\Mapping\TreeLeft';
-
- /**
- * Annotation to mark field as one which will store right value
- */
- const ANNOTATION_RIGHT = 'Gedmo\Tree\Mapping\TreeRight';
-
- /**
- * Annotation to mark relative parent field
- */
- const ANNOTATION_PARENT = 'Gedmo\Tree\Mapping\TreeParent';
-
- /**
- * Annotation to mark node level
- */
- const ANNOTATION_LEVEL = 'Gedmo\Tree\Mapping\TreeLevel';
-
- /**
- * Annotation to mark field as tree root
- */
- const ANNOTATION_ROOT = 'Gedmo\Tree\Mapping\TreeRoot';
-
- /**
- * List of types which are valid for tree fields
- *
- * @var array
- */
- private $validTypes = array(
- 'integer',
- 'smallint',
- 'bigint'
- );
-
- /**
- * List of tree strategies available
- *
- * @var array
- */
- private $strategies = array(
- 'nested'
- );
-
- /**
- * {@inheritDoc}
- */
- public function validateFullMetadata($meta, array $config)
- {
- if (isset($config['strategy'])) {
- $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
- $this->$method($meta, $config);
- } elseif ($config) {
- throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public function readExtendedMetadata($meta, array &$config) {
- require_once __DIR__ . '/../Annotations.php';
- $reader = new AnnotationReader();
- $reader->setAnnotationNamespaceAlias('Gedmo\Tree\Mapping\\', 'gedmo');
-
- $class = $meta->getReflectionClass();
- // class annotations
- $classAnnotations = $reader->getClassAnnotations($class);
- if (isset($classAnnotations[self::ANNOTATION_TREE])) {
- $annot = $classAnnotations[self::ANNOTATION_TREE];
- if (!in_array($annot->type, $this->strategies)) {
- throw new InvalidMappingException("Tree type: {$annot->type} is not available.");
- }
- $config['strategy'] = $annot->type;
- }
-
- // 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 ($left = $reader->getPropertyAnnotation($property, self::ANNOTATION_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 ($right = $reader->getPropertyAnnotation($property, self::ANNOTATION_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 ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_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 ($root = $reader->getPropertyAnnotation($property, self::ANNOTATION_ROOT)) {
- $field = $property->getName();
- if (!$meta->hasField($field)) {
- throw new InvalidMappingException("Unable to find 'root' - [{$field}] as mapped property in entity - {$meta->name}");
- }
- if (!$this->isValidField($meta, $field)) {
- throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
- }
- $config['root'] = $field;
- }
- // level
- if ($parent = $reader->getPropertyAnnotation($property, self::ANNOTATION_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;
- }
- }
- }
-
- /**
- * Checks if $field type is valid
- *
- * @param ClassMetadataInfo $meta
- * @param string $field
- * @return boolean
- */
- protected function isValidField($meta, $field)
- {
- $mapping = $meta->getFieldMapping($field);
- return $mapping && in_array($mapping['type'], $this->validTypes);
- }
-
- /**
- * Validates metadata for nested type tree
- *
- * @param ClassMetadataInfo $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}");
- }
- }
- }
|