Yaml.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 tree fields
  27. *
  28. * @var array
  29. */
  30. private $validTypes = array(
  31. 'integer',
  32. 'smallint',
  33. 'bigint',
  34. 'int'
  35. );
  36. /**
  37. * List of types which are valid for the path (materialized path strategy)
  38. *
  39. * @var array
  40. */
  41. private $validPathTypes = array(
  42. 'string',
  43. 'text'
  44. );
  45. /**
  46. * List of types which are valid for the path source (materialized path strategy)
  47. *
  48. * @var array
  49. */
  50. private $validPathSourceTypes = array(
  51. 'id',
  52. 'integer',
  53. 'smallint',
  54. 'bigint',
  55. 'string',
  56. 'int',
  57. 'float'
  58. );
  59. /**
  60. * List of tree strategies available
  61. *
  62. * @var array
  63. */
  64. private $strategies = array(
  65. 'nested',
  66. 'closure',
  67. 'materializedPath'
  68. );
  69. /**
  70. * {@inheritDoc}
  71. */
  72. public function readExtendedMetadata($meta, array &$config)
  73. {
  74. $mapping = $this->_getMapping($meta->name);
  75. if (isset($mapping['gedmo'])) {
  76. $classMapping = $mapping['gedmo'];
  77. if (isset($classMapping['tree']['type'])) {
  78. $strategy = $classMapping['tree']['type'];
  79. if (!in_array($strategy, $this->strategies)) {
  80. throw new InvalidMappingException("Tree type: $strategy is not available.");
  81. }
  82. $config['strategy'] = $strategy;
  83. }
  84. if (isset($classMapping['tree']['closure'])) {
  85. $class = $classMapping['tree']['closure'];
  86. if (!class_exists($class)) {
  87. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  88. }
  89. $config['closure'] = $class;
  90. }
  91. }
  92. if (isset($mapping['fields'])) {
  93. foreach ($mapping['fields'] as $field => $fieldMapping) {
  94. if (isset($fieldMapping['gedmo'])) {
  95. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  96. if (!$this->isValidField($meta, $field)) {
  97. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  98. }
  99. $config['left'] = $field;
  100. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  101. if (!$this->isValidField($meta, $field)) {
  102. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  103. }
  104. $config['right'] = $field;
  105. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  106. if (!$this->isValidField($meta, $field)) {
  107. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  108. }
  109. $config['level'] = $field;
  110. } elseif (in_array('treeRoot', $fieldMapping['gedmo'])) {
  111. if (!$meta->getFieldMapping($field)) {
  112. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid in class - {$meta->name}");
  113. }
  114. $config['root'] = $field;
  115. } elseif (in_array('treePath', $fieldMapping['gedmo'])) {
  116. if (!$this->isValidFieldForPath($meta, $field)) {
  117. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  118. }
  119. $separator = $fieldMapping['gedmo']['treePath']['separator'];
  120. if (strlen($separator) > 1) {
  121. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  122. }
  123. $config['path'] = $field;
  124. $config['path_separator'] = $separator;
  125. } elseif (in_array('treePathSource', $fieldMapping['gedmo'])) {
  126. if (!$this->isValidFieldForPathSource($meta, $field)) {
  127. 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}");
  128. }
  129. $config['path_source'] = $field;
  130. }
  131. }
  132. }
  133. }
  134. if (isset($mapping['manyToOne'])) {
  135. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  136. if (isset($relationMapping['gedmo'])) {
  137. if (in_array('treeParent', $relationMapping['gedmo'])) {
  138. if ($relationMapping['targetEntity'] != $meta->name) {
  139. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  140. }
  141. $config['parent'] = $field;
  142. }
  143. }
  144. }
  145. }
  146. if (!$meta->isMappedSuperclass && $config) {
  147. if (isset($config['strategy'])) {
  148. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  149. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  150. }
  151. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  152. $this->$method($meta, $config);
  153. } else {
  154. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  155. }
  156. }
  157. }
  158. /**
  159. * {@inheritDoc}
  160. */
  161. protected function _loadMappingFile($file)
  162. {
  163. return \Symfony\Component\Yaml\Yaml::load($file);
  164. }
  165. /**
  166. * Checks if $field type is valid
  167. *
  168. * @param object $meta
  169. * @param string $field
  170. * @return boolean
  171. */
  172. protected function isValidField($meta, $field)
  173. {
  174. $mapping = $meta->getFieldMapping($field);
  175. return $mapping && in_array($mapping['type'], $this->validTypes);
  176. }
  177. /**
  178. * Checks if $field type is valid for Path field
  179. *
  180. * @param object $meta
  181. * @param string $field
  182. * @return boolean
  183. */
  184. protected function isValidFieldForPath($meta, $field)
  185. {
  186. $mapping = $meta->getFieldMapping($field);
  187. return $mapping && in_array($mapping['type'], $this->validPathTypes);
  188. }
  189. /**
  190. * Checks if $field type is valid for PathSource field
  191. *
  192. * @param object $meta
  193. * @param string $field
  194. * @return boolean
  195. */
  196. protected function isValidFieldForPathSource($meta, $field)
  197. {
  198. $mapping = $meta->getFieldMapping($field);
  199. return $mapping && in_array($mapping['type'], $this->validPathSourceTypes);
  200. }
  201. /**
  202. * Validates metadata for nested type tree
  203. *
  204. * @param object $meta
  205. * @param array $config
  206. * @throws InvalidMappingException
  207. * @return void
  208. */
  209. private function validateNestedTreeMetadata($meta, array $config)
  210. {
  211. $missingFields = array();
  212. if (!isset($config['parent'])) {
  213. $missingFields[] = 'ancestor';
  214. }
  215. if (!isset($config['left'])) {
  216. $missingFields[] = 'left';
  217. }
  218. if (!isset($config['right'])) {
  219. $missingFields[] = 'right';
  220. }
  221. if ($missingFields) {
  222. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  223. }
  224. }
  225. /**
  226. * Validates metadata for closure type tree
  227. *
  228. * @param object $meta
  229. * @param array $config
  230. * @throws InvalidMappingException
  231. * @return void
  232. */
  233. private function validateClosureTreeMetadata($meta, array $config)
  234. {
  235. $missingFields = array();
  236. if (!isset($config['parent'])) {
  237. $missingFields[] = 'ancestor';
  238. }
  239. if (!isset($config['closure'])) {
  240. $missingFields[] = 'closure class';
  241. }
  242. if ($missingFields) {
  243. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  244. }
  245. }
  246. /**
  247. * Validates metadata for materialized path type tree
  248. *
  249. * @param object $meta
  250. * @param array $config
  251. * @throws InvalidMappingException
  252. * @return void
  253. */
  254. private function validateMaterializedPathTreeMetadata($meta, array $config)
  255. {
  256. $missingFields = array();
  257. if (!isset($config['parent'])) {
  258. $missingFields[] = 'ancestor';
  259. }
  260. if (!isset($config['path'])) {
  261. $missingFields[] = 'path';
  262. }
  263. if (!isset($config['path_source'])) {
  264. $missingFields[] = 'path_source';
  265. }
  266. if ($missingFields) {
  267. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  268. }
  269. }
  270. }