Xml.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Gedmo\Blameable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. Gedmo\Exception\InvalidMappingException;
  5. /**
  6. * This is a xml mapping driver for Blameable
  7. * behavioral extension. Used for extraction of extended
  8. * metadata from xml specificaly for Blameable
  9. * extension.
  10. *
  11. * @author David Buchmann <mail@davidbu.ch>
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class Xml extends BaseXml
  15. {
  16. /**
  17. * List of types which are valid for blame
  18. *
  19. * @var array
  20. */
  21. private $validTypes = array(
  22. 'one',
  23. 'string',
  24. 'int',
  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->blameable)) {
  43. /**
  44. * @var \SimpleXmlElement $data
  45. */
  46. $data = $fieldMapping->blameable;
  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' or a reference 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("Blameable 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->blameable)) {
  78. $data = $fieldMapping->blameable;
  79. if (! $meta->isSingleValuedAssociation($field)) {
  80. throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
  81. }
  82. if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
  83. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  84. }
  85. if ($this->_getAttribute($data, 'on') == 'change') {
  86. if (!$this->_isAttributeSet($data, 'field')) {
  87. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  88. }
  89. $trackedFieldAttribute = $this->_getAttribute($data, 'field');
  90. $valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
  91. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  92. throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
  93. }
  94. $field = array(
  95. 'field' => $field,
  96. 'trackedField' => $trackedFieldAttribute,
  97. 'value' => $valueAttribute,
  98. );
  99. }
  100. $config[$this->_getAttribute($data, 'on')][] = $field;
  101. }
  102. }
  103. }
  104. }
  105. /**
  106. * Checks if $field type is valid
  107. *
  108. * @param object $meta
  109. * @param string $field
  110. * @return boolean
  111. */
  112. protected function isValidField($meta, $field)
  113. {
  114. $mapping = $meta->getFieldMapping($field);
  115. return $mapping && in_array($mapping['type'], $this->validTypes);
  116. }
  117. }