Yaml.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\Timestampable\Mapping\MappingException;
  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. * (non-PHPdoc)
  38. * @see Gedmo\Mapping.Driver::validateFullMetadata()
  39. */
  40. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  41. {
  42. }
  43. /**
  44. * (non-PHPdoc)
  45. * @see Gedmo\Mapping.Driver::readExtendedMetadata()
  46. */
  47. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  48. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  49. $mapping = $yaml[$meta->name];
  50. if (isset($mapping['fields'])) {
  51. foreach ($mapping['fields'] as $field => $fieldMapping) {
  52. if (isset($fieldMapping['gedmo']['timestampable'])) {
  53. $mappingProperty = $fieldMapping['gedmo']['timestampable'];
  54. if (!$this->_isValidField($meta, $field)) {
  55. throw MappingException::notValidFieldType($field, $meta->name);
  56. }
  57. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  58. throw MappingException::triggerTypeInvalid($field, $meta->name);
  59. }
  60. if ($mappingProperty['on'] == 'change') {
  61. if (!isset($mappingProperty['field']) || !isset($mappingProperty['value'])) {
  62. throw MappingException::parametersMissing($field, $meta->name);
  63. }
  64. $field = array(
  65. 'field' => $field,
  66. 'trackedField' => $mappingProperty['field'],
  67. 'value' => $mappingProperty['value']
  68. );
  69. }
  70. $config[$mappingProperty['on']][] = $field;
  71. }
  72. }
  73. }
  74. }
  75. /**
  76. * (non-PHPdoc)
  77. * @see Gedmo\Mapping\Driver.File::_loadMappingFile()
  78. */
  79. protected function _loadMappingFile($file)
  80. {
  81. return \Symfony\Component\Yaml\Yaml::load($file);
  82. }
  83. /**
  84. * Checks if $field type is valid
  85. *
  86. * @param ClassMetadataInfo $meta
  87. * @param string $field
  88. * @return boolean
  89. */
  90. protected function _isValidField(ClassMetadataInfo $meta, $field)
  91. {
  92. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  93. }
  94. }