Xml.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. );
  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') || !$this->_isAttributeSet($data, 'value')) {
  62. throw new InvalidMappingException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
  63. }
  64. $field = array(
  65. 'field' => $field,
  66. 'trackedField' => $this->_getAttribute($data, 'field'),
  67. 'value' => $this->_getAttribute($data, 'value')
  68. );
  69. }
  70. $config[$this->_getAttribute($data, 'on')][] = $field;
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * Checks if $field type is valid
  77. *
  78. * @param object $meta
  79. * @param string $field
  80. * @return boolean
  81. */
  82. protected function isValidField($meta, $field)
  83. {
  84. $mapping = $meta->getFieldMapping($field);
  85. return $mapping && in_array($mapping['type'], $this->validTypes);
  86. }
  87. }