Xml.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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 validateFullMetadata(ClassMetadata $meta, array $config)
  44. {
  45. if (isset($config['strategy'])) {
  46. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  47. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  48. }
  49. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  50. $this->$method($meta, $config);
  51. } elseif ($config) {
  52. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  53. }
  54. }
  55. /**
  56. * {@inheritDoc}
  57. */
  58. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  59. /**
  60. * @var \SimpleXmlElement $xml
  61. */
  62. $xml = $this->_getMapping($meta->name);
  63. $xmlDoctrine = $xml;
  64. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  65. if ($xmlDoctrine->getName() == 'entity') {
  66. if (isset($xml->tree) && $this->_isAttributeSet($xml->tree, 'type')) {
  67. $strategy = $this->_getAttribute($xml->tree, 'type');
  68. if (!in_array($strategy, $this->strategies)) {
  69. throw new InvalidMappingException("Tree type: $strategy is not available.");
  70. }
  71. $config['strategy'] = $strategy;
  72. }
  73. if (isset($xml->{'tree-closure'}) && $this->_isAttributeSet($xml->{'tree-closure'}, 'class')) {
  74. $class = $this->_getAttribute($xml->{'tree-closure'}, 'class');
  75. if (!class_exists($class)) {
  76. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  77. }
  78. $config['closure'] = $class;
  79. }
  80. }
  81. if (isset($xmlDoctrine->field)) {
  82. foreach ($xmlDoctrine->field as $mapping) {
  83. $mappingDoctrine = $mapping;
  84. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  85. $field = $this->_getAttribute($mappingDoctrine, 'name');
  86. if (isset($mapping->{'tree-left'})) {
  87. if (!$this->isValidField($meta, $field)) {
  88. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  89. }
  90. $config['left'] = $field;
  91. } elseif (isset($mapping->{'tree-right'})) {
  92. if (!$this->isValidField($meta, $field)) {
  93. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  94. }
  95. $config['right'] = $field;
  96. } elseif (isset($mapping->{'tree-root'})) {
  97. if (!$this->isValidField($meta, $field)) {
  98. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  99. }
  100. $config['root'] = $field;
  101. } elseif (isset($mapping->{'tree-level'})) {
  102. if (!$this->isValidField($meta, $field)) {
  103. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  104. }
  105. $config['level'] = $field;
  106. }
  107. }
  108. }
  109. if (isset($xmlDoctrine->{'many-to-one'})) {
  110. foreach ($xmlDoctrine->{'many-to-one'} as $manyToOneMapping) {
  111. /**
  112. * @var \SimpleXMLElement $manyToOneMapping
  113. */
  114. $manyToOneMappingDoctrine = $manyToOneMapping;
  115. $manyToOneMapping = $manyToOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  116. if (isset($manyToOneMapping->{'tree-parent'})) {
  117. $field = $this->_getAttribute($manyToOneMappingDoctrine, 'field');
  118. if ($meta->associationMappings[$field]['targetEntity'] != $meta->name) {
  119. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  120. }
  121. $config['parent'] = $field;
  122. }
  123. }
  124. }
  125. }
  126. /**
  127. * Checks if $field type is valid
  128. *
  129. * @param ClassMetadata $meta
  130. * @param string $field
  131. * @return boolean
  132. */
  133. protected function isValidField(ClassMetadata $meta, $field)
  134. {
  135. $mapping = $meta->getFieldMapping($field);
  136. return $mapping && in_array($mapping['type'], $this->validTypes);
  137. }
  138. /**
  139. * Validates metadata for nested type tree
  140. *
  141. * @param ClassMetadata $meta
  142. * @param array $config
  143. * @throws InvalidMappingException
  144. * @return void
  145. */
  146. private function validateNestedTreeMetadata(ClassMetadata $meta, array $config)
  147. {
  148. $missingFields = array();
  149. if (!isset($config['parent'])) {
  150. $missingFields[] = 'ancestor';
  151. }
  152. if (!isset($config['left'])) {
  153. $missingFields[] = 'left';
  154. }
  155. if (!isset($config['right'])) {
  156. $missingFields[] = 'right';
  157. }
  158. if ($missingFields) {
  159. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  160. }
  161. }
  162. /**
  163. * Validates metadata for closure type tree
  164. *
  165. * @param ClassMetadata $meta
  166. * @param array $config
  167. * @throws InvalidMappingException
  168. * @return void
  169. */
  170. private function validateClosureTreeMetadata(ClassMetadata $meta, array $config)
  171. {
  172. $missingFields = array();
  173. if (!isset($config['parent'])) {
  174. $missingFields[] = 'ancestor';
  175. }
  176. if (!isset($config['closure'])) {
  177. $missingFields[] = 'closure class';
  178. }
  179. if ($missingFields) {
  180. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  181. }
  182. }
  183. }