Xml.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace Gedmo\IpTraceable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml;
  4. use Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is a xml mapping driver for IpTraceable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specifically for IpTraceable
  9. * extension.
  10. *
  11. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  12. * @author Miha Vrhovnik <miha.vrhovnik@gmail.com>
  13. * @author Pierre-Charles Bertineau <pc.bertineau@alterphp.com>
  14. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  15. */
  16. class Xml extends BaseXml
  17. {
  18. /**
  19. * List of types which are valid for IP
  20. *
  21. * @var array
  22. */
  23. private $validTypes = array(
  24. 'string',
  25. );
  26. /**
  27. * {@inheritDoc}
  28. */
  29. public function readExtendedMetadata($meta, array &$config)
  30. {
  31. /**
  32. * @var \SimpleXmlElement $mapping
  33. */
  34. $mapping = $this->_getMapping($meta->name);
  35. if (isset($mapping->field)) {
  36. /**
  37. * @var \SimpleXmlElement $fieldMapping
  38. */
  39. foreach ($mapping->field as $fieldMapping) {
  40. $fieldMappingDoctrine = $fieldMapping;
  41. $fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
  42. if (isset($fieldMapping->{'ip-traceable'})) {
  43. /**
  44. * @var \SimpleXmlElement $data
  45. */
  46. $data = $fieldMapping->{'ip-traceable'};
  47. $field = $this->_getAttribute($fieldMappingDoctrine, 'name');
  48. if (!$this->isValidField($meta, $field)) {
  49. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  50. }
  51. if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
  52. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  53. }
  54. if ($this->_getAttribute($data, 'on') == 'change') {
  55. if (!$this->_isAttributeSet($data, 'field')) {
  56. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  57. }
  58. $trackedFieldAttribute = $this->_getAttribute($data, 'field');
  59. $valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
  60. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  61. throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
  62. }
  63. $field = array(
  64. 'field' => $field,
  65. 'trackedField' => $trackedFieldAttribute,
  66. 'value' => $valueAttribute,
  67. );
  68. }
  69. $config[$this->_getAttribute($data, 'on')][] = $field;
  70. }
  71. }
  72. }
  73. if (isset($mapping->{'many-to-one'})) {
  74. foreach ($mapping->{'many-to-one'} as $fieldMapping) {
  75. $field = $this->_getAttribute($fieldMapping, 'field');
  76. $fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
  77. if (isset($fieldMapping->{'ip-traceable'})) {
  78. /**
  79. * @var \SimpleXmlElement $data
  80. */
  81. $data = $fieldMapping->{'ip-traceable'};
  82. if (! $meta->isSingleValuedAssociation($field)) {
  83. throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
  84. }
  85. if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
  86. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  87. }
  88. if ($this->_getAttribute($data, 'on') == 'change') {
  89. if (!$this->_isAttributeSet($data, 'field')) {
  90. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  91. }
  92. $trackedFieldAttribute = $this->_getAttribute($data, 'field');
  93. $valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
  94. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  95. throw new InvalidMappingException("IpTraceable extension does not support multiple value changeset detection yet.");
  96. }
  97. $field = array(
  98. 'field' => $field,
  99. 'trackedField' => $trackedFieldAttribute,
  100. 'value' => $valueAttribute,
  101. );
  102. }
  103. $config[$this->_getAttribute($data, 'on')][] = $field;
  104. }
  105. }
  106. }
  107. }
  108. /**
  109. * Checks if $field type is valid
  110. *
  111. * @param object $meta
  112. * @param string $field
  113. *
  114. * @return boolean
  115. */
  116. protected function isValidField($meta, $field)
  117. {
  118. $mapping = $meta->getFieldMapping($field);
  119. return $mapping && in_array($mapping['type'], $this->validTypes);
  120. }
  121. }