Xml.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Gedmo\Exception\InvalidMappingException,
  5. Gedmo\Tree\Mapping\Validator;
  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 Gustavo Falco <comfortablynumb84@gmail.com>
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  15. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  16. */
  17. class Xml extends BaseXml
  18. {
  19. /**
  20. * List of tree strategies available
  21. *
  22. * @var array
  23. */
  24. private $strategies = array(
  25. 'nested',
  26. 'closure',
  27. 'materializedPath'
  28. );
  29. /**
  30. * {@inheritDoc}
  31. */
  32. public function readExtendedMetadata($meta, array &$config) {
  33. /**
  34. * @var \SimpleXmlElement $xml
  35. */
  36. $xml = $this->_getMapping($meta->name);
  37. $xmlDoctrine = $xml;
  38. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  39. $validator = new Validator();
  40. if (isset($xml->tree) && $this->_isAttributeSet($xml->tree, 'type')) {
  41. $strategy = $this->_getAttribute($xml->tree, 'type');
  42. if (!in_array($strategy, $this->strategies)) {
  43. throw new InvalidMappingException("Tree type: $strategy is not available.");
  44. }
  45. $config['strategy'] = $strategy;
  46. $config['activate_locking'] = $this->_getAttribute($xml->tree, 'activate-locking') === 'true' ? true : false;
  47. if ($lockingTimeout = $this->_getAttribute($xml->tree, 'locking-timeout')) {
  48. $config['locking_timeout'] = (int) $lockingTimeout;
  49. if ($config['locking_timeout'] < 1) {
  50. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  51. }
  52. } else {
  53. $config['locking_timeout'] = 3;
  54. }
  55. }
  56. if (isset($xml->{'tree-closure'}) && $this->_isAttributeSet($xml->{'tree-closure'}, 'class')) {
  57. $class = $this->_getAttribute($xml->{'tree-closure'}, 'class');
  58. if (!class_exists($class)) {
  59. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  60. }
  61. $config['closure'] = $class;
  62. }
  63. if (isset($xmlDoctrine->field)) {
  64. foreach ($xmlDoctrine->field as $mapping) {
  65. $mappingDoctrine = $mapping;
  66. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  67. $field = $this->_getAttribute($mappingDoctrine, 'name');
  68. if (isset($mapping->{'tree-left'})) {
  69. if (!$validator->isValidField($meta, $field)) {
  70. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  71. }
  72. $config['left'] = $field;
  73. } elseif (isset($mapping->{'tree-right'})) {
  74. if (!$validator->isValidField($meta, $field)) {
  75. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  76. }
  77. $config['right'] = $field;
  78. } elseif (isset($mapping->{'tree-root'})) {
  79. if (!$validator->isValidFieldForRoot($meta, $field)) {
  80. throw new InvalidMappingException("Tree root field - [{$field}] type is not valid and must be any of the 'integer' types or 'string' in class - {$meta->name}");
  81. }
  82. $config['root'] = $field;
  83. } elseif (isset($mapping->{'tree-level'})) {
  84. if (!$validator->isValidField($meta, $field)) {
  85. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  86. }
  87. $config['level'] = $field;
  88. } elseif (isset($mapping->{'tree-path'})) {
  89. if (!$validator->isValidFieldForPath($meta, $field)) {
  90. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  91. }
  92. $separator = $this->_getAttribute($mapping->{'tree-path'}, 'separator');
  93. if (strlen($separator) > 1) {
  94. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  95. }
  96. $appendId = $this->_getAttribute($mapping->{'tree-path'}, 'append_id');
  97. if (!$appendId) {
  98. $appendId = true;
  99. } else {
  100. $appendId = strtolower($appendId) == 'false' ? false : true;
  101. }
  102. $startsWithSeparator = $this->_getAttribute($mapping->{'tree-path'}, 'starts_with_separator');
  103. if (!$startsWithSeparator) {
  104. $startsWithSeparator = false;
  105. } else {
  106. $startsWithSeparator = strtolower($startsWithSeparator) == 'false' ? false : true;
  107. }
  108. $endsWithSeparator = $this->_getAttribute($mapping->{'tree-path'}, 'ends_with_separator');
  109. if (!$endsWithSeparator) {
  110. $endsWithSeparator = true;
  111. } else {
  112. $endsWithSeparator = strtolower($endsWithSeparator) == 'false' ? false : true;
  113. }
  114. $config['path'] = $field;
  115. $config['path_separator'] = $separator;
  116. $config['path_append_id'] = $appendId;
  117. $config['path_starts_with_separator'] = $startsWithSeparator;
  118. $config['path_ends_with_separator'] = $endsWithSeparator;
  119. } elseif (isset($mapping->{'tree-path-source'})) {
  120. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  121. 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}");
  122. }
  123. $config['path_source'] = $field;
  124. } elseif (isset($mapping->{'tree-lock-time'})) {
  125. if (!$validator->isValidFieldForLockTime($meta, $field)) {
  126. throw new InvalidMappingException("Tree LockTime field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  127. }
  128. $config['lock_time'] = $field;
  129. }
  130. }
  131. }
  132. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  133. throw new InvalidMappingException("You need to map a date field as the tree lock time field to activate locking support.");
  134. }
  135. if ($xmlDoctrine->getName() == 'entity' || $xmlDoctrine->getName() == 'mapped-superclass') {
  136. if (isset($xmlDoctrine->{'many-to-one'})) {
  137. foreach ($xmlDoctrine->{'many-to-one'} as $manyToOneMapping) {
  138. /**
  139. * @var \SimpleXMLElement $manyToOneMapping
  140. */
  141. $manyToOneMappingDoctrine = $manyToOneMapping;
  142. $manyToOneMapping = $manyToOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  143. if (isset($manyToOneMapping->{'tree-parent'})) {
  144. $field = $this->_getAttribute($manyToOneMappingDoctrine, 'field');
  145. $targetEntity = $meta->associationMappings[$field]['targetEntity'];
  146. $reflectionClass = new \ReflectionClass($targetEntity);
  147. if ($targetEntity != $meta->name && !$reflectionClass->isSubclassOf($meta->name)) {
  148. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  149. }
  150. $config['parent'] = $field;
  151. }
  152. }
  153. }
  154. } else if ($xmlDoctrine->getName() == 'document') {
  155. if (isset($xmlDoctrine->{'reference-one'})) {
  156. foreach ($xmlDoctrine->{'reference-one'} as $referenceOneMapping) {
  157. /**
  158. * @var \SimpleXMLElement $referenceOneMapping
  159. */
  160. $referenceOneMappingDoctrine = $referenceOneMapping;
  161. $referenceOneMapping = $referenceOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  162. if (isset($referenceOneMapping->{'tree-parent'})) {
  163. $field = $this->_getAttribute($referenceOneMappingDoctrine, 'field');
  164. if ($this->_getAttribute($referenceOneMappingDoctrine, 'target-document') != $meta->name) {
  165. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  166. }
  167. $config['parent'] = $field;
  168. }
  169. }
  170. }
  171. }
  172. if (!$meta->isMappedSuperclass && $config) {
  173. if (isset($config['strategy'])) {
  174. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  175. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  176. }
  177. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  178. $validator->$method($meta, $config);
  179. } else {
  180. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  181. }
  182. }
  183. }
  184. }