Yaml.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace Gedmo\Timestampable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\ORM\Mapping\ClassMetadataInfo,
  6. Gedmo\Exception\InvalidArgumentException;
  7. /**
  8. * This is a yaml mapping driver for Timestampable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Timestampable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Timestampable.Mapping.Driver
  15. * @subpackage Yaml
  16. * @link http://www.gediminasm.org
  17. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  18. */
  19. class Yaml extends File implements Driver
  20. {
  21. /**
  22. * File extension
  23. * @var string
  24. */
  25. protected $_extension = '.dcm.yml';
  26. /**
  27. * List of types which are valid for timestamp
  28. *
  29. * @var array
  30. */
  31. private $_validTypes = array(
  32. 'date',
  33. 'time',
  34. 'datetime'
  35. );
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  40. {
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  46. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  47. $mapping = $yaml[$meta->name];
  48. if (isset($mapping['fields'])) {
  49. foreach ($mapping['fields'] as $field => $fieldMapping) {
  50. if (isset($fieldMapping['gedmo']['timestampable'])) {
  51. $mappingProperty = $fieldMapping['gedmo']['timestampable'];
  52. if (!$this->_isValidField($meta, $field)) {
  53. throw new InvalidArgumentException("Field - [{$field}] type is not valid and must be 'date', 'datetime' or 'time' in class - {$meta->name}");
  54. }
  55. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  56. throw new InvalidArgumentException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  57. }
  58. if ($mappingProperty['on'] == 'change') {
  59. if (!isset($mappingProperty['field']) || !isset($mappingProperty['value'])) {
  60. throw new InvalidArgumentException("Missing parameters on property - {$field}, field and value must be set on [change] trigger in class - {$meta->name}");
  61. }
  62. $field = array(
  63. 'field' => $field,
  64. 'trackedField' => $mappingProperty['field'],
  65. 'value' => $mappingProperty['value']
  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::load($file);
  79. }
  80. /**
  81. * Checks if $field type is valid
  82. *
  83. * @param ClassMetadataInfo $meta
  84. * @param string $field
  85. * @return boolean
  86. */
  87. protected function _isValidField(ClassMetadataInfo $meta, $field)
  88. {
  89. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  90. }
  91. }