PageRenderTime 43ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/gedmo/doctrine-extensions/lib/Gedmo/Tree/Mapping/Driver/Xml.php

https://bitbucket.org/iiic/iszp
PHP | 178 lines | 133 code | 14 blank | 31 comment | 42 complexity | 83c9a4a4c0b841ca9ff4e58d6bba86cd MD5 | raw file
  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 (isset($xml->tree) && $this->_isAttributeSet($xml->tree, 'type')) {
  44. $strategy = $this->_getAttribute($xml->tree, 'type');
  45. if (!in_array($strategy, $this->strategies)) {
  46. throw new InvalidMappingException("Tree type: $strategy is not available.");
  47. }
  48. $config['strategy'] = $strategy;
  49. $config['activate_locking'] = $this->_getAttribute($xml->tree, 'activate-locking') === 'true' ? true : false;
  50. if ($lockingTimeout = $this->_getAttribute($xml->tree, 'locking-timeout')) {
  51. $config['locking_timeout'] = (int) $lockingTimeout;
  52. if ($config['locking_timeout'] < 1) {
  53. throw new InvalidMappingException("Tree Locking Timeout must be at least of 1 second.");
  54. }
  55. } else {
  56. $config['locking_timeout'] = 3;
  57. }
  58. }
  59. if (isset($xml->{'tree-closure'}) && $this->_isAttributeSet($xml->{'tree-closure'}, 'class')) {
  60. $class = $this->_getAttribute($xml->{'tree-closure'}, 'class');
  61. if (!class_exists($class)) {
  62. throw new InvalidMappingException("Tree closure class: {$class} does not exist.");
  63. }
  64. $config['closure'] = $class;
  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 (!$validator->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 (!$validator->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 (!$validator->isValidFieldForRoot($meta, $field)) {
  83. 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}");
  84. }
  85. $config['root'] = $field;
  86. } elseif (isset($mapping->{'tree-level'})) {
  87. if (!$validator->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. } elseif (isset($mapping->{'tree-path'})) {
  92. if (!$validator->isValidFieldForPath($meta, $field)) {
  93. throw new InvalidMappingException("Tree Path field - [{$field}] type is not valid. It must be string or text in class - {$meta->name}");
  94. }
  95. $separator = $this->_getAttribute($mapping->{'tree-path'}, 'separator');
  96. if (strlen($separator) > 1) {
  97. throw new InvalidMappingException("Tree Path field - [{$field}] Separator {$separator} is invalid. It must be only one character long.");
  98. }
  99. $config['path'] = $field;
  100. $config['path_separator'] = $separator;
  101. } elseif (isset($mapping->{'tree-path-source'})) {
  102. if (!$validator->isValidFieldForPathSource($meta, $field)) {
  103. 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}");
  104. }
  105. $config['path_source'] = $field;
  106. } elseif (isset($mapping->{'tree-lock-time'})) {
  107. if (!$validator->isValidFieldForLockTime($meta, $field)) {
  108. throw new InvalidMappingException("Tree LockTime field - [{$field}] type is not valid. It must be \"date\" in class - {$meta->name}");
  109. }
  110. $config['lock_time'] = $field;
  111. }
  112. }
  113. }
  114. if (isset($config['activate_locking']) && $config['activate_locking'] && !isset($config['lock_time'])) {
  115. throw new InvalidMappingException("You need to map a date field as the tree lock time field to activate locking support.");
  116. }
  117. if ($xmlDoctrine->getName() == 'entity') {
  118. if (isset($xmlDoctrine->{'many-to-one'})) {
  119. foreach ($xmlDoctrine->{'many-to-one'} as $manyToOneMapping) {
  120. /**
  121. * @var \SimpleXMLElement $manyToOneMapping
  122. */
  123. $manyToOneMappingDoctrine = $manyToOneMapping;
  124. $manyToOneMapping = $manyToOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  125. if (isset($manyToOneMapping->{'tree-parent'})) {
  126. $field = $this->_getAttribute($manyToOneMappingDoctrine, 'field');
  127. if ($meta->associationMappings[$field]['targetEntity'] != $meta->name) {
  128. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  129. }
  130. $config['parent'] = $field;
  131. }
  132. }
  133. }
  134. } else if ($xmlDoctrine->getName() == 'document') {
  135. if (isset($xmlDoctrine->{'reference-one'})) {
  136. foreach ($xmlDoctrine->{'reference-one'} as $referenceOneMapping) {
  137. /**
  138. * @var \SimpleXMLElement $referenceOneMapping
  139. */
  140. $referenceOneMappingDoctrine = $referenceOneMapping;
  141. $referenceOneMapping = $referenceOneMapping->children(self::GEDMO_NAMESPACE_URI);;
  142. if (isset($referenceOneMapping->{'tree-parent'})) {
  143. $field = $this->_getAttribute($referenceOneMappingDoctrine, 'field');
  144. if ($this->_getAttribute($referenceOneMappingDoctrine, 'target-document') != $meta->name) {
  145. throw new InvalidMappingException("Unable to find ancestor/parent child relation through ancestor field - [{$field}] in class - {$meta->name}");
  146. }
  147. $config['parent'] = $field;
  148. }
  149. }
  150. }
  151. }
  152. if (!$meta->isMappedSuperclass && $config) {
  153. if (isset($config['strategy'])) {
  154. if (is_array($meta->identifier) && count($meta->identifier) > 1) {
  155. throw new InvalidMappingException("Tree does not support composite identifiers in class - {$meta->name}");
  156. }
  157. $method = 'validate' . ucfirst($config['strategy']) . 'TreeMetadata';
  158. $validator->$method($meta, $config);
  159. } else {
  160. throw new InvalidMappingException("Cannot find Tree type for class: {$meta->name}");
  161. }
  162. }
  163. }
  164. }