Xml.php 3.8 KB

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