Yaml.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. 'zenddate',
  36. 'vardatetime'
  37. );
  38. /**
  39. * {@inheritDoc}
  40. */
  41. public function readExtendedMetadata($meta, array &$config)
  42. {
  43. $mapping = $this->_getMapping($meta->name);
  44. if (isset($mapping['fields'])) {
  45. foreach ($mapping['fields'] as $field => $fieldMapping) {
  46. if (isset($fieldMapping['gedmo']['timestampable'])) {
  47. $mappingProperty = $fieldMapping['gedmo']['timestampable'];
  48. if (!$this->isValidField($meta, $field)) {
  49. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  50. }
  51. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  52. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  53. }
  54. if ($mappingProperty['on'] == 'change') {
  55. if (!isset($mappingProperty['field'])) {
  56. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  57. }
  58. $field = array(
  59. 'field' => $field,
  60. 'trackedField' => $mappingProperty['field'],
  61. 'value' => isset($mappingProperty['value']) ? $mappingProperty['value'] : null,
  62. );
  63. }
  64. $config[$mappingProperty['on']][] = $field;
  65. }
  66. }
  67. }
  68. }
  69. /**
  70. * {@inheritDoc}
  71. */
  72. protected function _loadMappingFile($file)
  73. {
  74. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  75. }
  76. /**
  77. * Checks if $field type is valid
  78. *
  79. * @param object $meta
  80. * @param string $field
  81. * @return boolean
  82. */
  83. protected function isValidField($meta, $field)
  84. {
  85. $mapping = $meta->getFieldMapping($field);
  86. return $mapping && in_array($mapping['type'], $this->validTypes);
  87. }
  88. }