Xml.php 6.9 KB

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