/lib/Gedmo/Tree/Mapping/Driver/Xml.php

https://github.com/Funsational/DoctrineExtensions · PHP · 195 lines · 124 code · 13 blank · 58 comment · 30 complexity · 3e3538d795bfacc84d217496578ef112 MD5 · raw file

  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is a xml mapping driver for Tree
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specificaly for Tree
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  13. * @package Gedmo.Tree.Mapping.Driver
  14. * @subpackage Xml
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Xml extends BaseXml
  19. {
  20. /**
  21. * List of types which are valid for timestamp
  22. *
  23. * @var array
  24. */
  25. private $validTypes = array(
  26. 'integer',
  27. 'smallint',
  28. 'bigint'
  29. );
  30. /**
  31. * List of tree strategies available
  32. *
  33. * @var array
  34. */
  35. private $strategies = array(
  36. 'nested',
  37. 'closure'
  38. );
  39. /**
  40. * {@inheritDoc}
  41. */
  42. public function validateFullMetadata($meta, array $config)
  43. {
  44. if (isset($config['strategy'])) {
  45. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  46. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  47. }
  48. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  49. $this->$method($meta, $config);
  50. } elseif ($config) {
  51. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  52. }
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function readExtendedMetadata($meta, array &$config) {
  58. /**
  59. * @var \SimpleXmlElement $xml
  60. */
  61. $xml = $this->_getMapping($meta->name);
  62. $xmlDoctrine = $xml;
  63. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  64. if ($xmlDoctrine->getName() == 'entity') {
  65. if (isset($xml->tree) && $this->_isAttributeSet($xml->tree, 'type')) {
  66. $strategy = $this->_getAttribute($xml->tree, 'type');
  67. if (!in_array($strategy, $this->strategies)) {
  68. throw new InvalidMappingException("Tree type: $strategy is not available.");
  69. }
  70. $config['strategy'] = $strategy;
  71. }
  72. if (isset($xml->{'tree-closure'}) && $this->_isAttributeSet($xml->{'tree-closure'}, 'class')) {
  73. $class = $this->_getAttribute($xml->{'tree-closure'}, 'class');
  74. if (!class_exists($class)) {
  75. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  76. }
  77. $config['closure'] = $class;
  78. }
  79. }
  80. if (isset($xmlDoctrine->field)) {
  81. foreach ($xmlDoctrine->field as $mapping) {
  82. $mappingDoctrine = $mapping;
  83. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  84. $field = $this->_getAttribute($mappingDoctrine, 'name');
  85. if (isset($mapping->{'tree-left'})) {
  86. if (!$this->isValidField($meta, $field)) {
  87. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  88. }
  89. $config['left'] = $field;
  90. } elseif (isset($mapping->{'tree-right'})) {
  91. if (!$this->isValidField($meta, $field)) {
  92. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  93. }
  94. $config['right'] = $field;
  95. } elseif (isset($mapping->{'tree-root'})) {
  96. if (!$this->isValidField($meta, $field)) {
  97. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  98. }
  99. $config['root'] = $field;
  100. } elseif (isset($mapping->{'tree-level'})) {
  101. if (!$this->isValidField($meta, $field)) {
  102. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  103. }
  104. $config['level'] = $field;
  105. }
  106. }
  107. }
  108. if (isset($xmlDoctrine->{'many-to-one'})) {
  109. foreach ($xmlDoctrine->{'many-to-one'} as $manyToOneMapping) {
  110. /**
  111. * @var \SimpleXMLElement $manyToOneMapping
  112. */
  113. $manyToOneMappingDoctrine = $manyToOneMapping;
  114. $manyToOneMapping = $manyToOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  115. if (isset($manyToOneMapping->{'tree-parent'})) {
  116. $field = $this->_getAttribute($manyToOneMappingDoctrine, 'field');
  117. if ($meta->associationMappings[$field]['targetEntity'] != $meta->name) {
  118. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  119. }
  120. $config['parent'] = $field;
  121. }
  122. }
  123. }
  124. }
  125. /**
  126. * Checks if $field type is valid
  127. *
  128. * @param ClassMetadata $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 ClassMetadata $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 ClassMetadata $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. }