Xml.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. * @package Gedmo.Tree.Mapping.Driver
  16. * @subpackage Xml
  17. * @link http://www.gediminasm.org
  18. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  19. */
  20. class Xml extends BaseXml
  21. {
  22. /**
  23. * List of tree strategies available
  24. *
  25. * @var array
  26. */
  27. private $strategies = array(
  28. 'nested',
  29. 'closure',
  30. 'materializedPath'
  31. );
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function readExtendedMetadata($meta, array &$config) {
  36. /**
  37. * @var \SimpleXmlElement $xml
  38. */
  39. $xml = $this->_getMapping($meta->name);
  40. $xmlDoctrine = $xml;
  41. $xml = $xml->children(self::GEDMO_NAMESPACE_URI);
  42. $validator = new Validator();
  43. if ($xmlDoctrine->getName() == 'entity') {
  44. if (isset($xml->tree) && $this->_isAttributeSet($xml->tree, 'type')) {
  45. $strategy = $this->_getAttribute($xml->tree, 'type');
  46. if (!in_array($strategy, $this->strategies)) {
  47. throw new InvalidMappingException("Tree type: $strategy is not available.");
  48. }
  49. $config['strategy'] = $strategy;
  50. $config['activate_locking'] = $this->_getAttribute($xml->tree, 'activate-locking') === 'true' ? true : false;
  51. if ($lockingTimeout = $this->_getAttribute($xml->tree, 'locking-timeout')) {
  52. $config['locking_timeout'] = (int) $lockingTimeout;
  53. if ($config['locking_timeout'] < 1) {
  54. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  55. }
  56. } else {
  57. $config['locking_timeout'] = 3;
  58. }
  59. }
  60. if (isset($xml->{'tree-closure'}) && $this->_isAttributeSet($xml->{'tree-closure'}, 'class')) {
  61. $class = $this->_getAttribute($xml->{'tree-closure'}, 'class');
  62. if (!class_exists($class)) {
  63. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  64. }
  65. $config['closure'] = $class;
  66. }
  67. }
  68. if (isset($xmlDoctrine->field)) {
  69. foreach ($xmlDoctrine->field as $mapping) {
  70. $mappingDoctrine = $mapping;
  71. $mapping = $mapping->children(self::GEDMO_NAMESPACE_URI);
  72. $field = $this->_getAttribute($mappingDoctrine, 'name');
  73. if (isset($mapping->{'tree-left'})) {
  74. if (!$validator->isValidField($meta, $field)) {
  75. throw new InvalidMappingException("Tree left field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  76. }
  77. $config['left'] = $field;
  78. } elseif (isset($mapping->{'tree-right'})) {
  79. if (!$validator->isValidField($meta, $field)) {
  80. throw new InvalidMappingException("Tree right field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  81. }
  82. $config['right'] = $field;
  83. } elseif (isset($mapping->{'tree-root'})) {
  84. if (!$validator->isValidFieldForRoot($meta, $field)) {
  85. 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}");
  86. }
  87. $config['root'] = $field;
  88. } elseif (isset($mapping->{'tree-level'})) {
  89. if (!$validator->isValidField($meta, $field)) {
  90. throw new InvalidMappingException("Tree level field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  91. }
  92. $config['level'] = $field;
  93. } elseif (isset($mapping->{'tree-path'})) {
  94. if (!$validator->isValidFieldForPath($meta, $field)) {
  95. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  96. }
  97. $separator = $this->_getAttribute($mapping->{'tree-path'}, 'separator');
  98. if (strlen($separator) > 1) {
  99. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  100. }
  101. $config['path'] = $field;
  102. $config['path_separator'] = $separator;
  103. } elseif (isset($mapping->{'tree-path-source'})) {
  104. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  105. 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}");
  106. }
  107. $config['path_source'] = $field;
  108. } elseif (isset($mapping->{'tree-lock-time'})) {
  109. if (!$validator->isValidFieldForLockTime($meta, $field)) {
  110. throw new InvalidMappingException("Tree LockTime field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  111. }
  112. $config['lock_time'] = $field;
  113. }
  114. }
  115. }
  116. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  117. throw new InvalidMappingException("You need to map a date field as the tree lock time field to activate locking support.");
  118. }
  119. if (isset($xmlDoctrine->{'many-to-one'})) {
  120. foreach ($xmlDoctrine->{'many-to-one'} as $manyToOneMapping) {
  121. /**
  122. * @var \SimpleXMLElement $manyToOneMapping
  123. */
  124. $manyToOneMappingDoctrine = $manyToOneMapping;
  125. $manyToOneMapping = $manyToOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  126. if (isset($manyToOneMapping->{'tree-parent'})) {
  127. $field = $this->_getAttribute($manyToOneMappingDoctrine, 'field');
  128. if ($meta->associationMappings[$field]['targetEntity'] != $meta->name) {
  129. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  130. }
  131. $config['parent'] = $field;
  132. }
  133. }
  134. }
  135. if (!$meta->isMappedSuperclass && $config) {
  136. if (isset($config['strategy'])) {
  137. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  138. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  139. }
  140. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  141. $validator->$method($meta, $config);
  142. } else {
  143. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  144. }
  145. }
  146. }
  147. }