Yaml.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. 'closure'
  43. );
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function readExtendedMetadata($meta, array &$config)
  48. {
  49. $mapping = $this->_getMapping($meta->name);
  50. if (isset($mapping['gedmo'])) {
  51. $classMapping = $mapping['gedmo'];
  52. if (isset($classMapping['tree']['type'])) {
  53. $strategy = $classMapping['tree']['type'];
  54. if (!in_array($strategy, $this->strategies)) {
  55. throw new InvalidMappingException("Tree type: $strategy is not available.");
  56. }
  57. $config['strategy'] = $strategy;
  58. }
  59. if (isset($classMapping['tree']['closure'])) {
  60. $class = $classMapping['tree']['closure'];
  61. if (!class_exists($class)) {
  62. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  63. }
  64. $config['closure'] = $class;
  65. }
  66. }
  67. if (isset($mapping['fields'])) {
  68. foreach ($mapping['fields'] as $field => $fieldMapping) {
  69. if (isset($fieldMapping['gedmo'])) {
  70. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  71. if (!$this->isValidField($meta, $field)) {
  72. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  73. }
  74. $config['left'] = $field;
  75. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  76. if (!$this->isValidField($meta, $field)) {
  77. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  78. }
  79. $config['right'] = $field;
  80. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  81. if (!$this->isValidField($meta, $field)) {
  82. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  83. }
  84. $config['level'] = $field;
  85. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  86. if (!$meta->getFieldMapping($field)) {
  87. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid in class - {$meta->name}");
  88. }
  89. $config['root'] = $field;
  90. }
  91. }
  92. }
  93. }
  94. if (isset($mapping['manyToOne'])) {
  95. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  96. if (isset($relationMapping['gedmo'])) {
  97. if (in_array('treeParent', $relationMapping['gedmo'])) {
  98. if ($relationMapping['targetEntity'] != $meta->name) {
  99. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  100. }
  101. $config['parent'] = $field;
  102. }
  103. }
  104. }
  105. }
  106. if (!$meta->isMappedSuperclass && $config) {
  107. if (isset($config['strategy'])) {
  108. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  109. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  110. }
  111. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  112. $this->$method($meta, $config);
  113. } else {
  114. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  115. }
  116. }
  117. }
  118. /**
  119. * {@inheritDoc}
  120. */
  121. protected function _loadMappingFile($file)
  122. {
  123. return \Symfony\Component\Yaml\Yaml::load($file);
  124. }
  125. /**
  126. * Checks if $field type is valid
  127. *
  128. * @param object $meta
  129. * @param string $field
  130. * @return boolean
  131. */
  132. protected function isValidField($meta, $field)
  133. {
  134. $mapping = $meta->getFieldMapping($field);
  135. return $mapping && in_array($mapping['type'], $this->validTypes);
  136. }
  137. /**
  138. * Validates metadata for nested type tree
  139. *
  140. * @param object $meta
  141. * @param array $config
  142. * @throws InvalidMappingException
  143. * @return void
  144. */
  145. private function validateNestedTreeMetadata($meta, array $config)
  146. {
  147. $missingFields = array();
  148. if (!isset($config['parent'])) {
  149. $missingFields[] = 'ancestor';
  150. }
  151. if (!isset($config['left'])) {
  152. $missingFields[] = 'left';
  153. }
  154. if (!isset($config['right'])) {
  155. $missingFields[] = 'right';
  156. }
  157. if ($missingFields) {
  158. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  159. }
  160. }
  161. /**
  162. * Validates metadata for closure type tree
  163. *
  164. * @param object $meta
  165. * @param array $config
  166. * @throws InvalidMappingException
  167. * @return void
  168. */
  169. private function validateClosureTreeMetadata($meta, array $config)
  170. {
  171. $missingFields = array();
  172. if (!isset($config['parent'])) {
  173. $missingFields[] = 'ancestor';
  174. }
  175. if (!isset($config['closure'])) {
  176. $missingFields[] = 'closure class';
  177. }
  178. if ($missingFields) {
  179. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  180. }
  181. }
  182. }