Yaml.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. 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 specificaly 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. 'time',
  30. 'datetime',
  31. 'datetimetz',
  32. 'timestamp',
  33. 'zenddate',
  34. 'vardatetime',
  35. 'integer'
  36. );
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function readExtendedMetadata($meta, array &$config)
  41. {
  42. $mapping = $this->_getMapping($meta->name);
  43. if (isset($mapping['fields'])) {
  44. foreach ($mapping['fields'] as $field => $fieldMapping) {
  45. if (isset($fieldMapping['gedmo']['timestampable'])) {
  46. $mappingProperty = $fieldMapping['gedmo']['timestampable'];
  47. if (!$this->isValidField($meta, $field)) {
  48. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  49. }
  50. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  51. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  52. }
  53. if ($mappingProperty['on'] == 'change') {
  54. if (!isset($mappingProperty['field'])) {
  55. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  56. }
  57. $trackedFieldAttribute = $mappingProperty['field'];
  58. $valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
  59. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  60. throw new InvalidMappingException("Timestampable extension does not support multiple value changeset detection yet.");
  61. }
  62. $field = array(
  63. 'field' => $field,
  64. 'trackedField' => $trackedFieldAttribute,
  65. 'value' => $valueAttribute,
  66. );
  67. }
  68. $config[$mappingProperty['on']][] = $field;
  69. }
  70. }
  71. }
  72. }
  73. /**
  74. * {@inheritDoc}
  75. */
  76. protected function _loadMappingFile($file)
  77. {
  78. return \Symfony\Component\Yaml\Yaml::parse($file);
  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. }