Yaml.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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'])) {
  68. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  69. if (!$this->_isValidField($meta, $field)) {
  70. throw MappingException::notValidFieldType($field, $meta->name);
  71. }
  72. $config['left'] = $field;
  73. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  74. if (!$this->_isValidField($meta, $field)) {
  75. throw MappingException::notValidFieldType($field, $meta->name);
  76. }
  77. $config['right'] = $field;
  78. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  79. if (!$this->_isValidField($meta, $field)) {
  80. throw MappingException::notValidFieldType($field, $meta->name);
  81. }
  82. $config['level'] = $field;
  83. }
  84. }
  85. }
  86. }
  87. if (isset($mapping['manyToOne'])) {
  88. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  89. if (isset($relationMapping['gedmo'])) {
  90. if (in_array('treeParent', $relationMapping['gedmo'])) {
  91. if ($relationMapping['targetEntity'] != $meta->name) {
  92. throw MappingException::parentFieldNotMappedOrRelated($field, $meta->name);
  93. }
  94. $config['parent'] = $field;
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /**
  101. * (non-PHPdoc)
  102. * @see Gedmo\Mapping\Driver.File::_loadMappingFile()
  103. */
  104. protected function _loadMappingFile($file)
  105. {
  106. return \Symfony\Component\Yaml\Yaml::load($file);
  107. }
  108. /**
  109. * Checks if $field type is valid
  110. *
  111. * @param ClassMetadataInfo $meta
  112. * @param string $field
  113. * @return boolean
  114. */
  115. protected function _isValidField(ClassMetadataInfo $meta, $field)
  116. {
  117. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  118. }
  119. }