Yaml.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\ORM\Mapping\ClassMetadataInfo,
  6. Gedmo\Tree\Mapping\MappingException;
  7. /**
  8. * This is a yaml mapping driver for Tree
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Tree
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree.Mapping.Driver
  15. * @subpackage Yaml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Yaml extends File implements Driver
  20. {
  21. /**
  22. * File extension
  23. * @var string
  24. */
  25. protected $_extension = '.dcm.yml';
  26. /**
  27. * List of types which are valid for timestamp
  28. *
  29. * @var array
  30. */
  31. private $_validTypes = array(
  32. 'integer',
  33. 'smallint',
  34. 'bigint'
  35. );
  36. /**
  37. * (non-PHPdoc)
  38. * @see Gedmo\Mapping.Driver::validateFullMetadata()
  39. */
  40. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  41. {
  42. if ($config) {
  43. $missingFields = array();
  44. if (!isset($config['parent'])) {
  45. $missingFields[] = 'ancestor';
  46. }
  47. if (!isset($config['left'])) {
  48. $missingFields[] = 'left';
  49. }
  50. if (!isset($config['right'])) {
  51. $missingFields[] = 'right';
  52. }
  53. if ($missingFields) {
  54. throw MappingException::missingMetaProperties($missingFields, $meta->name);
  55. }
  56. }
  57. }
  58. /**
  59. * (non-PHPdoc)
  60. * @see Gedmo\Mapping.Driver::readExtendedMetadata()
  61. */
  62. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  63. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  64. $mapping = $yaml[$meta->name];
  65. if (isset($mapping['fields'])) {
  66. foreach ($mapping['fields'] as $field => $fieldMapping) {
  67. if (isset($fieldMapping['gedmo']['tree'])) {
  68. $mappingProperty = $fieldMapping['gedmo']['tree'];
  69. if ($mappingProperty == 'left') {
  70. if (!$this->_isValidField($meta, $field)) {
  71. throw MappingException::notValidFieldType($field, $meta->name);
  72. }
  73. $config['left'] = $field;
  74. } elseif ($mappingProperty == 'right') {
  75. if (!$this->_isValidField($meta, $field)) {
  76. throw MappingException::notValidFieldType($field, $meta->name);
  77. }
  78. $config['right'] = $field;
  79. } elseif ($mappingProperty == 'level') {
  80. if (!$this->_isValidField($meta, $field)) {
  81. throw MappingException::notValidFieldType($field, $meta->name);
  82. }
  83. $config['level'] = $field;
  84. }
  85. }
  86. }
  87. }
  88. if (isset($mapping['manyToOne'])) {
  89. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  90. if (isset($relationMapping['gedmo']['tree'])) {
  91. $mappingProperty = $relationMapping['gedmo']['tree'];
  92. if ($mappingProperty == 'parent') {
  93. if ($relationMapping['targetEntity'] != $meta->name) {
  94. throw MappingException::parentFieldNotMappedOrRelated($field, $meta->name);
  95. }
  96. $config['parent'] = $field;
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * (non-PHPdoc)
  104. * @see Gedmo\Mapping\Driver.File::_loadMappingFile()
  105. */
  106. protected function _loadMappingFile($file)
  107. {
  108. return \Symfony\Component\Yaml\Yaml::load($file);
  109. }
  110. /**
  111. * Checks if $field type is valid
  112. *
  113. * @param ClassMetadataInfo $meta
  114. * @param string $field
  115. * @return boolean
  116. */
  117. protected function _isValidField(ClassMetadataInfo $meta, $field)
  118. {
  119. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  120. }
  121. }