Xml.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  5. Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a xml mapping driver for Tree
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from xml specificaly for Tree
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  14. * @package Gedmo.Tree.Mapping.Driver
  15. * @subpackage Xml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Xml extends BaseXml
  20. {
  21. /**
  22. * List of types which are valid for timestamp
  23. *
  24. * @var array
  25. */
  26. private $validTypes = array(
  27. 'integer',
  28. 'smallint',
  29. 'bigint'
  30. );
  31. /**
  32. * List of tree strategies available
  33. *
  34. * @var array
  35. */
  36. private $strategies = array(
  37. 'nested',
  38. 'closure'
  39. );
  40. /**
  41. * {@inheritDoc}
  42. */
  43. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  44. /**
  45. * @var \SimpleXmlElement $xml
  46. */
  47. $xml = $this->_getMapping($meta->name);
  48. $xmlDoctrine = $xml;
  49. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  50. if ($xmlDoctrine->getName() == 'entity') {
  51. if (isset($xml->tree) && $this->_isAttributeSet($xml->tree, 'type')) {
  52. $strategy = $this->_getAttribute($xml->tree, 'type');
  53. if (!in_array($strategy, $this->strategies)) {
  54. throw new InvalidMappingException("Tree type: $strategy is not available.");
  55. }
  56. $config['strategy'] = $strategy;
  57. }
  58. if (isset($xml->{'tree-closure'}) && $this->_isAttributeSet($xml->{'tree-closure'}, 'class')) {
  59. $class = $this->_getAttribute($xml->{'tree-closure'}, 'class');
  60. if (!class_exists($class)) {
  61. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  62. }
  63. $config['closure'] = $class;
  64. }
  65. }
  66. if (isset($xmlDoctrine->field)) {
  67. foreach ($xmlDoctrine->field as $mapping) {
  68. $mappingDoctrine = $mapping;
  69. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  70. $field = $this->_getAttribute($mappingDoctrine, 'name');
  71. if (isset($mapping->{'tree-left'})) {
  72. if (!$this->isValidField($meta, $field)) {
  73. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  74. }
  75. $config['left'] = $field;
  76. } elseif (isset($mapping->{'tree-right'})) {
  77. if (!$this->isValidField($meta, $field)) {
  78. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  79. }
  80. $config['right'] = $field;
  81. } elseif (isset($mapping->{'tree-root'})) {
  82. if (!$this->isValidField($meta, $field)) {
  83. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  84. }
  85. $config['root'] = $field;
  86. } elseif (isset($mapping->{'tree-level'})) {
  87. if (!$this->isValidField($meta, $field)) {
  88. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  89. }
  90. $config['level'] = $field;
  91. }
  92. }
  93. }
  94. if (isset($xmlDoctrine->{'many-to-one'})) {
  95. foreach ($xmlDoctrine->{'many-to-one'} as $manyToOneMapping) {
  96. /**
  97. * @var \SimpleXMLElement $manyToOneMapping
  98. */
  99. $manyToOneMappingDoctrine = $manyToOneMapping;
  100. $manyToOneMapping = $manyToOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  101. if (isset($manyToOneMapping->{'tree-parent'})) {
  102. $field = $this->_getAttribute($manyToOneMappingDoctrine, 'field');
  103. if ($meta->associationMappings[$field]['targetEntity'] != $meta->name) {
  104. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  105. }
  106. $config['parent'] = $field;
  107. }
  108. }
  109. }
  110. if (!$meta->isMappedSuperclass && $config) {
  111. if (isset($config['strategy'])) {
  112. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  113. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  114. }
  115. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  116. $this->$method($meta, $config);
  117. } else {
  118. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  119. }
  120. }
  121. }
  122. /**
  123. * Checks if $field type is valid
  124. *
  125. * @param ClassMetadata $meta
  126. * @param string $field
  127. * @return boolean
  128. */
  129. protected function isValidField(ClassMetadata $meta, $field)
  130. {
  131. $mapping = $meta->getFieldMapping($field);
  132. return $mapping && in_array($mapping['type'], $this->validTypes);
  133. }
  134. /**
  135. * Validates metadata for nested type tree
  136. *
  137. * @param ClassMetadata $meta
  138. * @param array $config
  139. * @throws InvalidMappingException
  140. * @return void
  141. */
  142. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  143. {
  144. $missingFields = array();
  145. if (!isset($config['parent'])) {
  146. $missingFields[] = 'ancestor';
  147. }
  148. if (!isset($config['left'])) {
  149. $missingFields[] = 'left';
  150. }
  151. if (!isset($config['right'])) {
  152. $missingFields[] = 'right';
  153. }
  154. if ($missingFields) {
  155. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  156. }
  157. }
  158. /**
  159. * Validates metadata for closure type tree
  160. *
  161. * @param ClassMetadata $meta
  162. * @param array $config
  163. * @throws InvalidMappingException
  164. * @return void
  165. */
  166. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  167. {
  168. $missingFields = array();
  169. if (!isset($config['parent'])) {
  170. $missingFields[] = 'ancestor';
  171. }
  172. if (!isset($config['closure'])) {
  173. $missingFields[] = 'closure class';
  174. }
  175. if ($missingFields) {
  176. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  177. }
  178. }
  179. }