Yaml.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File;
  4. use Gedmo\Mapping\Driver;
  5. use Gedmo\Exception\InvalidMappingException;
  6. use 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 specifically 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. if (!$class = $this->getRelatedClassName($meta, $classMapping['tree']['closure'])) {
  58. throw new InvalidMappingException("Tree closure class: {$classMapping['tree']['closure']} does not exist.");
  59. }
  60. $config['closure'] = $class;
  61. }
  62. }
  63. if (isset($mapping['id'])) {
  64. foreach($mapping['id'] as $field => $fieldMapping) {
  65. if (isset($fieldMapping['gedmo'])) {
  66. if (in_array('treePathSource', $fieldMapping['gedmo'])) {
  67. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  68. throw new InvalidMappingException(
  69. "Tree PathSource field - [{$field}] type is not valid. It can be any of the integer variants, double, float or string in class - {$meta->name}"
  70. );
  71. }
  72. $config['path_source'] = $field;
  73. }
  74. }
  75. }
  76. }
  77. if (isset($mapping['fields'])) {
  78. foreach ($mapping['fields'] as $field => $fieldMapping) {
  79. if (isset($fieldMapping['gedmo'])) {
  80. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  81. if (!$validator->isValidField($meta, $field)) {
  82. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  83. }
  84. $config['left'] = $field;
  85. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  86. if (!$validator->isValidField($meta, $field)) {
  87. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  88. }
  89. $config['right'] = $field;
  90. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  91. if (!$validator->isValidField($meta, $field)) {
  92. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  93. }
  94. $config['level'] = $field;
  95. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  96. if (!$validator->isValidFieldForRoot($meta, $field)) {
  97. 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}");
  98. }
  99. $config['root'] = $field;
  100. } elseif (in_array('treePath', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['treePath'])) {
  101. if (!$validator->isValidFieldForPath($meta, $field)) {
  102. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  103. }
  104. $treePathInfo = isset($fieldMapping['gedmo']['treePath']) ? $fieldMapping['gedmo']['treePath'] :
  105. $fieldMapping['gedmo'][array_search('treePath', $fieldMapping['gedmo'])];
  106. if (is_array($treePathInfo) && isset($treePathInfo['separator'])) {
  107. $separator = $treePathInfo['separator'];
  108. } else {
  109. $separator = '|';
  110. }
  111. if (strlen($separator) > 1) {
  112. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  113. }
  114. if (is_array($treePathInfo) && isset($treePathInfo['appendId'])) {
  115. $appendId = $treePathInfo['appendId'];
  116. } else {
  117. $appendId = null;
  118. }
  119. if (is_array($treePathInfo) && isset($treePathInfo['startsWithSeparator'])) {
  120. $startsWithSeparator = $treePathInfo['startsWithSeparator'];
  121. } else {
  122. $startsWithSeparator = false;
  123. }
  124. if (is_array($treePathInfo) && isset($treePathInfo['endsWithSeparator'])) {
  125. $endsWithSeparator = $treePathInfo['endsWithSeparator'];
  126. } else {
  127. $endsWithSeparator = true;
  128. }
  129. $config['path'] = $field;
  130. $config['path_separator'] = $separator;
  131. $config['path_append_id'] = $appendId;
  132. $config['path_starts_with_separator'] = $startsWithSeparator;
  133. $config['path_ends_with_separator'] = $endsWithSeparator;
  134. } elseif (in_array('treePathSource', $fieldMapping['gedmo'])) {
  135. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  136. 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}");
  137. }
  138. $config['path_source'] = $field;
  139. } elseif (in_array('treePathHash', $fieldMapping['gedmo'])) {
  140. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  141. throw new InvalidMappingException("Tree PathHash field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  142. }
  143. $config['path_hash'] = $field;
  144. } elseif (in_array('treeLockTime', $fieldMapping['gedmo'])) {
  145. if (!$validator->isValidFieldForLocktime($meta, $field)) {
  146. throw new InvalidMappingException("Tree LockTime field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  147. }
  148. $config['lock_time'] = $field;
  149. } elseif (in_array('treeParent', $fieldMapping['gedmo'])) {
  150. $config['parent'] = $field;
  151. }
  152. }
  153. }
  154. }
  155. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  156. throw new InvalidMappingException("You need to map a date|datetime|timestamp field as the tree lock time field to activate locking support.");
  157. }
  158. if (isset($mapping['manyToOne'])) {
  159. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  160. if (isset($relationMapping['gedmo'])) {
  161. if (in_array('treeParent', $relationMapping['gedmo'])) {
  162. if (!$rel = $this->getRelatedClassName($meta, $relationMapping['targetEntity'])) {
  163. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  164. }
  165. $config['parent'] = $field;
  166. }
  167. if (in_array('treeRoot', $relationMapping['gedmo'])) {
  168. if (!$rel = $this->getRelatedClassName($meta, $relationMapping['targetEntity'])) {
  169. throw new InvalidMappingException("Unable to find root-descendant relation through root field - [{$field}] in class - {$meta->name}");
  170. }
  171. $config['root'] = $field;
  172. }
  173. }
  174. }
  175. }
  176. if (!$meta->isMappedSuperclass && $config) {
  177. if (isset($config['strategy'])) {
  178. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  179. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  180. }
  181. $method = 'validate'.ucfirst($config['strategy']).'TreeMetadata';
  182. $validator->$method($meta, $config);
  183. } else {
  184. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  185. }
  186. }
  187. }
  188. /**
  189. * {@inheritDoc}
  190. */
  191. protected function _loadMappingFile($file)
  192. {
  193. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  194. }
  195. }