Yaml.php 9.4 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. Gedmo\Tree\Mapping\Validator;
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Yaml extends File implements Driver
  17. {
  18. /**
  19. * File extension
  20. * @var string
  21. */
  22. protected $_extension = '.dcm.yml';
  23. /**
  24. * List of tree strategies available
  25. *
  26. * @var array
  27. */
  28. private $strategies = array(
  29. 'nested',
  30. 'closure',
  31. 'materializedPath'
  32. );
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function readExtendedMetadata($meta, array &$config)
  37. {
  38. $mapping = $this->_getMapping($meta->name);
  39. $validator = new Validator();
  40. if (isset($mapping['gedmo'])) {
  41. $classMapping = $mapping['gedmo'];
  42. if (isset($classMapping['tree']['type'])) {
  43. $strategy = $classMapping['tree']['type'];
  44. if (!in_array($strategy, $this->strategies)) {
  45. throw new InvalidMappingException("Tree type: $strategy is not available.");
  46. }
  47. $config['strategy'] = $strategy;
  48. $config['activate_locking'] = isset($classMapping['tree']['activateLocking']) ?
  49. $classMapping['tree']['activateLocking'] : false;
  50. $config['locking_timeout'] = isset($classMapping['tree']['lockingTimeout']) ?
  51. (int) $classMapping['tree']['lockingTimeout'] : 3;
  52. if ($config['locking_timeout'] < 1) {
  53. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  54. }
  55. }
  56. if (isset($classMapping['tree']['closure'])) {
  57. $class = $classMapping['tree']['closure'];
  58. if (!class_exists($class)) {
  59. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  60. }
  61. $config['closure'] = $class;
  62. }
  63. }
  64. if (isset($mapping['fields'])) {
  65. foreach ($mapping['fields'] as $field => $fieldMapping) {
  66. if (isset($fieldMapping['gedmo'])) {
  67. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  68. if (!$validator->isValidField($meta, $field)) {
  69. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  70. }
  71. $config['left'] = $field;
  72. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  73. if (!$validator->isValidField($meta, $field)) {
  74. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  75. }
  76. $config['right'] = $field;
  77. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  78. if (!$validator->isValidField($meta, $field)) {
  79. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  80. }
  81. $config['level'] = $field;
  82. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  83. if (!$validator->isValidFieldForRoot($meta, $field)) {
  84. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be any of the 'integer' types or 'string' in class - {$meta->name}");
  85. }
  86. $config['root'] = $field;
  87. } elseif (in_array('treePath', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['treePath'])) {
  88. if (!$validator->isValidFieldForPath($meta, $field)) {
  89. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  90. }
  91. $treePathInfo = isset($fieldMapping['gedmo']['treePath']) ? $fieldMapping['gedmo']['treePath'] :
  92. $fieldMapping['gedmo'][array_search('treePath', $fieldMapping['gedmo'])];
  93. if (is_array($treePathInfo) && isset($treePathInfo['separator'])) {
  94. $separator = $treePathInfo['separator'];
  95. } else {
  96. $separator = '|';
  97. }
  98. if (strlen($separator) > 1) {
  99. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  100. }
  101. if (is_array($treePathInfo) && isset($treePathInfo['appendId'])) {
  102. $appendId = $treePathInfo['appendId'];
  103. } else {
  104. $appendId = null;
  105. }
  106. if (is_array($treePathInfo) && isset($treePathInfo['startsWithSeparator'])) {
  107. $startsWithSeparator = $treePathInfo['startsWithSeparator'];
  108. } else {
  109. $startsWithSeparator = false;
  110. }
  111. if (is_array($treePathInfo) && isset($treePathInfo['endsWithSeparator'])) {
  112. $endsWithSeparator = $treePathInfo['endsWithSeparator'];
  113. } else {
  114. $endsWithSeparator = true;
  115. }
  116. $config['path'] = $field;
  117. $config['path_separator'] = $separator;
  118. $config['path_append_id'] = $appendId;
  119. $config['path_starts_with_separator'] = $startsWithSeparator;
  120. $config['path_ends_with_separator'] = $endsWithSeparator;
  121. } elseif (in_array('treePathSource', $fieldMapping['gedmo'])) {
  122. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  123. throw new InvalidMappingException("Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}");
  124. }
  125. $config['path_source'] = $field;
  126. } elseif (in_array('treePathHash', $fieldMapping['gedmo'])) {
  127. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  128. throw new InvalidMappingException("Tree PathHash field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  129. }
  130. $config['path_hash'] = $field;
  131. } elseif (in_array('treeLockTime', $fieldMapping['gedmo'])) {
  132. if (!$validator->isValidFieldForLocktime($meta, $field)) {
  133. throw new InvalidMappingException("Tree LockTime field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  134. }
  135. $config['lock_time'] = $field;
  136. } elseif (in_array('treeParent', $fieldMapping['gedmo'])) {
  137. $config['parent'] = $field;
  138. }
  139. }
  140. }
  141. }
  142. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  143. throw new InvalidMappingException("You need to map a date|datetime|timestamp field as the tree lock time field to activate locking support.");
  144. }
  145. if (isset($mapping['manyToOne'])) {
  146. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  147. if (isset($relationMapping['gedmo'])) {
  148. if (in_array('treeParent', $relationMapping['gedmo'])) {
  149. if ($relationMapping['targetEntity'] != $meta->name) {
  150. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  151. }
  152. $config['parent'] = $field;
  153. }
  154. }
  155. }
  156. }
  157. if (!$meta->isMappedSuperclass && $config) {
  158. if (isset($config['strategy'])) {
  159. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  160. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  161. }
  162. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  163. $validator->$method($meta, $config);
  164. } else {
  165. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  166. }
  167. }
  168. }
  169. /**
  170. * {@inheritDoc}
  171. */
  172. protected function _loadMappingFile($file)
  173. {
  174. return \Symfony\Component\Yaml\Yaml::parse($file);
  175. }
  176. }