Xml.php 3.6 KB

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