Xml.php 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. * @package Gedmo.Timestampable.Mapping.Driver
  14. * @subpackage Xml
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Xml extends BaseXml
  19. {
  20. /**
  21. * List of types which are valid for timestamp
  22. *
  23. * @var array
  24. */
  25. private $validTypes = array(
  26. 'date',
  27. 'time',
  28. 'datetime',
  29. 'timestamp',
  30. 'zenddate',
  31. 'vardatetime'
  32. );
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function readExtendedMetadata($meta, array &$config)
  37. {
  38. /**
  39. * @var \SimpleXmlElement $mapping
  40. */
  41. $mapping = $this->_getMapping($meta->name);
  42. if (isset($mapping->field)) {
  43. /**
  44. * @var \SimpleXmlElement $fieldMapping
  45. */
  46. foreach ($mapping->field as $fieldMapping) {
  47. $fieldMappingDoctrine = $fieldMapping;
  48. $fieldMapping = $fieldMapping->children(self::GEDMO_NAMESPACE_URI);
  49. if (isset($fieldMapping->timestampable)) {
  50. /**
  51. * @var \SimpleXmlElement $data
  52. */
  53. $data = $fieldMapping->timestampable;
  54. $field = $this->_getAttribute($fieldMappingDoctrine, 'name');
  55. if (!$this->isValidField($meta, $field)) {
  56. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  57. }
  58. if (!$this->_isAttributeSet($data, 'on') || !in_array($this->_getAttribute($data, 'on'), array('update', 'create', 'change'))) {
  59. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  60. }
  61. if ($this->_getAttribute($data, 'on') == 'change') {
  62. if (!$this->_isAttributeSet($data, 'field')) {
  63. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  64. }
  65. $field = array(
  66. 'field' => $field,
  67. 'trackedField' => $this->_getAttribute($data, 'field'),
  68. 'value' => $this->_isAttributeSet($data, 'value') ? $this->_getAttribute($data, 'value') : null,
  69. );
  70. }
  71. $config[$this->_getAttribute($data, 'on')][] = $field;
  72. }
  73. }
  74. }
  75. }
  76. /**
  77. * Checks if $field type is valid
  78. *
  79. * @param object $meta
  80. * @param string $field
  81. * @return boolean
  82. */
  83. protected function isValidField($meta, $field)
  84. {
  85. $mapping = $meta->getFieldMapping($field);
  86. return $mapping && in_array($mapping['type'], $this->validTypes);
  87. }
  88. }