Yaml.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File;
  4. use Gedmo\Mapping\Driver;
  5. use Gedmo\Exception\InvalidMappingException;
  6. /**
  7. * This is a yaml mapping driver for Timestampable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specifically for Timestampable
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Yaml extends File implements Driver
  16. {
  17. /**
  18. * File extension
  19. * @var string
  20. */
  21. protected $_extension = '.dcm.yml';
  22. /**
  23. * List of types which are valid for timestamp
  24. *
  25. * @var array
  26. */
  27. private $validTypes = array(
  28. 'date',
  29. 'date_immutable',
  30. 'time',
  31. 'time_immutable',
  32. 'datetime',
  33. 'datetime_immutable',
  34. 'datetimetz',
  35. 'datetimetz_immutable',
  36. 'timestamp',
  37. 'zenddate',
  38. 'vardatetime',
  39. 'integer',
  40. );
  41. /**
  42. * {@inheritDoc}
  43. */
  44. public function readExtendedMetadata($meta, array &$config)
  45. {
  46. $mapping = $this->_getMapping($meta->name);
  47. if (isset($mapping['fields'])) {
  48. foreach ($mapping['fields'] as $field => $fieldMapping) {
  49. if (isset($fieldMapping['gedmo']['timestampable'])) {
  50. $mappingProperty = $fieldMapping['gedmo']['timestampable'];
  51. if (!$this->isValidField($meta, $field)) {
  52. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  53. }
  54. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  55. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  56. }
  57. if ($mappingProperty['on'] == 'change') {
  58. if (!isset($mappingProperty['field'])) {
  59. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  60. }
  61. $trackedFieldAttribute = $mappingProperty['field'];
  62. $valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
  63. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  64. throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
  65. }
  66. $field = array(
  67. 'field' => $field,
  68. 'trackedField' => $trackedFieldAttribute,
  69. 'value' => $valueAttribute,
  70. );
  71. }
  72. $config[$mappingProperty['on']][] = $field;
  73. }
  74. }
  75. }
  76. }
  77. /**
  78. * {@inheritDoc}
  79. */
  80. protected function _loadMappingFile($file)
  81. {
  82. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  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. }