Yaml.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a yaml mapping driver for Tree
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specificaly for Tree
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Tree.Mapping.Driver
  14. * @subpackage Yaml
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Yaml extends File implements Driver
  19. {
  20. /**
  21. * File extension
  22. * @var string
  23. */
  24. protected $_extension = '.dcm.yml';
  25. /**
  26. * List of types which are valid for timestamp
  27. *
  28. * @var array
  29. */
  30. private $_validTypes = array(
  31. 'integer',
  32. 'smallint',
  33. 'bigint'
  34. );
  35. /**
  36. * List of tree strategies available
  37. *
  38. * @var array
  39. */
  40. private $strategies = array(
  41. 'nested'
  42. );
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function validateFullMetadata($meta, array $config)
  47. {
  48. if (isset($config['strategy'])) {
  49. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  50. $this->$method($meta, $config);
  51. } elseif ($config) {
  52. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  53. }
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function readExtendedMetadata($meta, array &$config) {
  59. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  60. $mapping = $yaml[$meta->name];
  61. if (isset($mapping['gedmo'])) {
  62. $classMapping = $mapping['gedmo'];
  63. if (isset($classMapping['tree']['type'])) {
  64. $strategy = $classMapping['tree']['type'];
  65. if (!in_array($strategy, $this->strategies)) {
  66. throw new InvalidMappingException("Tree type: $strategy is not available.");
  67. }
  68. $config['strategy'] = $strategy;
  69. }
  70. }
  71. if (isset($mapping['fields'])) {
  72. foreach ($mapping['fields'] as $field => $fieldMapping) {
  73. if (isset($fieldMapping['gedmo'])) {
  74. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  75. if (!$this->_isValidField($meta, $field)) {
  76. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  77. }
  78. $config['left'] = $field;
  79. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  80. if (!$this->_isValidField($meta, $field)) {
  81. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  82. }
  83. $config['right'] = $field;
  84. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  85. if (!$this->_isValidField($meta, $field)) {
  86. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  87. }
  88. $config['level'] = $field;
  89. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  90. if (!$this->_isValidField($meta, $field)) {
  91. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  92. }
  93. $config['root'] = $field;
  94. }
  95. }
  96. }
  97. }
  98. if (isset($mapping['manyToOne'])) {
  99. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  100. if (isset($relationMapping['gedmo'])) {
  101. if (in_array('treeParent', $relationMapping['gedmo'])) {
  102. if ($relationMapping['targetEntity'] != $meta->name) {
  103. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  104. }
  105. $config['parent'] = $field;
  106. }
  107. }
  108. }
  109. }
  110. }
  111. /**
  112. * {@inheritDoc}
  113. */
  114. protected function _loadMappingFile($file)
  115. {
  116. return \Symfony\Component\Yaml\Yaml::load($file);
  117. }
  118. /**
  119. * Checks if $field type is valid
  120. *
  121. * @param ClassMetadata $meta
  122. * @param string $field
  123. * @return boolean
  124. */
  125. protected function _isValidField($meta, $field)
  126. {
  127. $mapping = $meta->getFieldMapping($field);
  128. return $mapping && in_array($mapping['type'], $this->_validTypes);
  129. }
  130. /**
  131. * Validates metadata for nested type tree
  132. *
  133. * @param ClassMetadataInfo $meta
  134. * @param array $config
  135. * @throws InvalidMappingException
  136. * @return void
  137. */
  138. private function validateNestedTreeMetadata($meta, array $config)
  139. {
  140. $missingFields = array();
  141. if (!isset($config['parent'])) {
  142. $missingFields[] = 'ancestor';
  143. }
  144. if (!isset($config['left'])) {
  145. $missingFields[] = 'left';
  146. }
  147. if (!isset($config['right'])) {
  148. $missingFields[] = 'right';
  149. }
  150. if ($missingFields) {
  151. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  152. }
  153. }
  154. }