Xml.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\Xml as BaseXml,
  4. 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 specificaly 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. 'time',
  25. 'datetime',
  26. 'datetimetz',
  27. 'timestamp',
  28. 'zenddate',
  29. 'vardatetime',
  30. 'integer'
  31. );
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function readExtendedMetadata($meta, array &$config)
  36. {
  37. /**
  38. * @var \SimpleXmlElement $mapping
  39. */
  40. $mapping = $this->_getMapping($meta->name);
  41. if (isset($mapping->field)) {
  42. /**
  43. * @var \SimpleXmlElement $fieldMapping
  44. */
  45. foreach ($mapping->field as $fieldMapping) {
  46. $fieldMappingDoctrine = $fieldMapping;
  47. $fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
  48. if (isset($fieldMapping->timestampable)) {
  49. /**
  50. * @var \SimpleXmlElement $data
  51. */
  52. $data = $fieldMapping->timestampable;
  53. $field = $this->_getAttribute($fieldMappingDoctrine, 'name');
  54. if (!$this->isValidField($meta, $field)) {
  55. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  56. }
  57. if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
  58. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  59. }
  60. if ($this->_getAttribute($data, 'on') == 'change') {
  61. if (!$this->_isAttributeSet($data, 'field')) {
  62. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  63. }
  64. $trackedFieldAttribute = $this->_getAttribute($data, 'field');
  65. $valueAttribute = $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value' ) : null;
  66. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  67. throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
  68. }
  69. $field = array(
  70. 'field' => $field,
  71. 'trackedField' => $trackedFieldAttribute,
  72. 'value' => $valueAttribute,
  73. );
  74. }
  75. $config[$this->_getAttribute($data, 'on')][] = $field;
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * Checks if $field type is valid
  82. *
  83. * @param object $meta
  84. * @param string $field
  85. * @return boolean
  86. */
  87. protected function isValidField($meta, $field)
  88. {
  89. $mapping = $meta->getFieldMapping($field);
  90. return $mapping && in_array($mapping['type'], $this->validTypes);
  91. }
  92. }