123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- <?php
- namespace Gedmo\Tree\Mapping\Driver;
- use Gedmo\Mapping\Driver\File,
- Gedmo\Mapping\Driver,
- Doctrine\ORM\Mapping\ClassMetadataInfo,
- Gedmo\Tree\Mapping\MappingException;
- /**
- * This is a yaml mapping driver for Tree
- * behavioral extension. Used for extraction of extended
- * metadata from yaml specificaly for Tree
- * extension.
- *
- * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
- * @package Gedmo.Tree.Mapping.Driver
- * @subpackage Yaml
- * @link http://www.gediminasm.org
- * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
- */
- class Yaml extends File implements Driver
- {
- /**
- * File extension
- * @var string
- */
- protected $_extension = '.dcm.yml';
-
- /**
- * List of types which are valid for timestamp
- *
- * @var array
- */
- private $_validTypes = array(
- 'integer',
- 'smallint',
- 'bigint'
- );
-
- /**
- * {@inheritDoc}
- */
- public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
- {
- if ($config) {
- $missingFields = array();
- if (!isset($config['parent'])) {
- $missingFields[] = 'ancestor';
- }
- if (!isset($config['left'])) {
- $missingFields[] = 'left';
- }
- if (!isset($config['right'])) {
- $missingFields[] = 'right';
- }
- if ($missingFields) {
- throw MappingException::missingMetaProperties($missingFields, $meta->name);
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
- $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
- $mapping = $yaml[$meta->name];
- if (isset($mapping['fields'])) {
- foreach ($mapping['fields'] as $field => $fieldMapping) {
- if (isset($fieldMapping['gedmo'])) {
- if (in_array('treeLeft', $fieldMapping['gedmo'])) {
- if (!$this->_isValidField($meta, $field)) {
- throw MappingException::notValidFieldType($field, $meta->name);
- }
- $config['left'] = $field;
- } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
- if (!$this->_isValidField($meta, $field)) {
- throw MappingException::notValidFieldType($field, $meta->name);
- }
- $config['right'] = $field;
- } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
- if (!$this->_isValidField($meta, $field)) {
- throw MappingException::notValidFieldType($field, $meta->name);
- }
- $config['level'] = $field;
- }
- }
- }
- }
- if (isset($mapping['manyToOne'])) {
- foreach ($mapping['manyToOne'] as $field => $relationMapping) {
- if (isset($relationMapping['gedmo'])) {
- if (in_array('treeParent', $relationMapping['gedmo'])) {
- if ($relationMapping['targetEntity'] != $meta->name) {
- throw MappingException::parentFieldNotMappedOrRelated($field, $meta->name);
- }
- $config['parent'] = $field;
- }
- }
- }
- }
- }
-
- /**
- * {@inheritDoc}
- */
- protected function _loadMappingFile($file)
- {
- return \Symfony\Component\Yaml\Yaml::load($file);
- }
-
- /**
- * Checks if $field type is valid
- *
- * @param ClassMetadataInfo $meta
- * @param string $field
- * @return boolean
- */
- protected function _isValidField(ClassMetadataInfo $meta, $field)
- {
- return in_array($meta->getTypeOfField($field), $this->_validTypes);
- }
- }
|