Yaml.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. * @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 tree strategies available
  28. *
  29. * @var array
  30. */
  31. private $strategies = array(
  32. 'nested',
  33. 'closure',
  34. 'materializedPath'
  35. );
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function readExtendedMetadata($meta, array &$config)
  40. {
  41. $mapping = $this->_getMapping($meta->name);
  42. $validator = new Validator();
  43. if (isset($mapping['gedmo'])) {
  44. $classMapping = $mapping['gedmo'];
  45. if (isset($classMapping['tree']['type'])) {
  46. $strategy = $classMapping['tree']['type'];
  47. if (!in_array($strategy, $this->strategies)) {
  48. throw new InvalidMappingException("Tree type: $strategy is not available.");
  49. }
  50. $config['strategy'] = $strategy;
  51. $config['activate_locking'] = isset($classMapping['tree']['activateLocking']) ?
  52. $classMapping['tree']['activateLocking'] : false;
  53. $config['locking_timeout'] = isset($classMapping['tree']['lockingTimeout']) ?
  54. (int) $classMapping['tree']['lockingTimeout'] : 3;
  55. if ($config['locking_timeout'] < 1) {
  56. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  57. }
  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 (!$validator->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 (!$validator->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 (!$validator->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 (!$validator->isValidFieldForRoot($meta, $field)) {
  87. 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}");
  88. }
  89. $config['root'] = $field;
  90. } elseif (in_array('treePath', $fieldMapping['gedmo']) || isset($fieldMapping['gedmo']['treePath'])) {
  91. if (!$validator->isValidFieldForPath($meta, $field)) {
  92. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  93. }
  94. $treePathInfo = isset($fieldMapping['gedmo']['treePath']) ? $fieldMapping['gedmo']['treePath'] :
  95. $fieldMapping['gedmo'][array_search('treePath', $fieldMapping['gedmo'])];
  96. if (is_array($treePathInfo) && isset($treePathInfo['separator'])) {
  97. $separator = $treePathInfo['separator'];
  98. } else {
  99. $separator = '|';
  100. }
  101. if (strlen($separator) > 1) {
  102. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  103. }
  104. $config['path'] = $field;
  105. $config['path_separator'] = $separator;
  106. } elseif (in_array('treePathSource', $fieldMapping['gedmo'])) {
  107. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  108. 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}");
  109. }
  110. $config['path_source'] = $field;
  111. } elseif (in_array('treeLockTime', $fieldMapping['gedmo'])) {
  112. if (!$validator->isValidFieldForLocktime($meta, $field)) {
  113. throw new InvalidMappingException("Tree LockTime field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  114. }
  115. $config['lock_time'] = $field;
  116. }
  117. }
  118. }
  119. }
  120. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  121. throw new InvalidMappingException("You need to map a date|datetime|timestamp field as the tree lock time field to activate locking support.");
  122. }
  123. if (isset($mapping['manyToOne'])) {
  124. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  125. if (isset($relationMapping['gedmo'])) {
  126. if (in_array('treeParent', $relationMapping['gedmo'])) {
  127. if ($relationMapping['targetEntity'] != $meta->name) {
  128. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  129. }
  130. $config['parent'] = $field;
  131. }
  132. }
  133. }
  134. }
  135. if (!$meta->isMappedSuperclass && $config) {
  136. if (isset($config['strategy'])) {
  137. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  138. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  139. }
  140. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  141. $validator->$method($meta, $config);
  142. } else {
  143. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  144. }
  145. }
  146. }
  147. /**
  148. * {@inheritDoc}
  149. */
  150. protected function _loadMappingFile($file)
  151. {
  152. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  153. }
  154. }