Yaml.php 3.3 KB

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