Yaml.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Gedmo\Blameable\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 Blameable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specifically for Blameable
  10. * extension.
  11. *
  12. * @author David Buchmann <mail@davidbu.ch>
  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 blameable
  24. *
  25. * @var array
  26. */
  27. private $validTypes = array(
  28. 'one',
  29. 'string',
  30. 'int',
  31. );
  32. /**
  33. * {@inheritDoc}
  34. */
  35. public function readExtendedMetadata($meta, array &$config)
  36. {
  37. $mapping = $this->_getMapping($meta->name);
  38. if (isset($mapping['fields'])) {
  39. foreach ($mapping['fields'] as $field => $fieldMapping) {
  40. if (isset($fieldMapping['gedmo']['blameable'])) {
  41. $mappingProperty = $fieldMapping['gedmo']['blameable'];
  42. if (!$this->isValidField($meta, $field)) {
  43. throw new InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a reference in class - {$meta->name}");
  44. }
  45. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  46. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  47. }
  48. if ($mappingProperty['on'] == 'change') {
  49. if (!isset($mappingProperty['field'])) {
  50. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  51. }
  52. $trackedFieldAttribute = $mappingProperty['field'];
  53. $valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
  54. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  55. throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
  56. }
  57. $field = array(
  58. 'field' => $field,
  59. 'trackedField' => $trackedFieldAttribute,
  60. 'value' => $valueAttribute,
  61. );
  62. }
  63. $config[$mappingProperty['on']][] = $field;
  64. }
  65. }
  66. }
  67. if (isset($mapping['manyToOne'])) {
  68. foreach ($mapping['manyToOne'] as $field => $fieldMapping) {
  69. if (isset($fieldMapping['gedmo']['blameable'])) {
  70. $mappingProperty = $fieldMapping['gedmo']['blameable'];
  71. if (! $meta->isSingleValuedAssociation($field)) {
  72. throw new InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$meta->name}");
  73. }
  74. if (!isset($mappingProperty['on']) || !in_array($mappingProperty['on'], array('update', 'create', 'change'))) {
  75. throw new InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$meta->name}");
  76. }
  77. if ($mappingProperty['on'] == 'change') {
  78. if (!isset($mappingProperty['field'])) {
  79. throw new InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$meta->name}");
  80. }
  81. $trackedFieldAttribute = $mappingProperty['field'];
  82. $valueAttribute = isset($mappingProperty['value']) ? $mappingProperty['value'] : null;
  83. if (is_array($trackedFieldAttribute) && null !== $valueAttribute) {
  84. throw new InvalidMappingException("Blameable extension does not support multiple value changeset detection yet.");
  85. }
  86. $field = array(
  87. 'field' => $field,
  88. 'trackedField' => $trackedFieldAttribute,
  89. 'value' => $valueAttribute,
  90. );
  91. }
  92. $config[$mappingProperty['on']][] = $field;
  93. }
  94. }
  95. }
  96. }
  97. /**
  98. * {@inheritDoc}
  99. */
  100. protected function _loadMappingFile($file)
  101. {
  102. return \Symfony\Component\Yaml\Yaml::parse($file);
  103. }
  104. /**
  105. * Checks if $field type is valid
  106. *
  107. * @param \Doctrine\ODM\MongoDB\Mapping\ClassMetadata $meta
  108. * @param string $field
  109. * @return boolean
  110. */
  111. protected function isValidField($meta, $field)
  112. {
  113. $mapping = $meta->getFieldMapping($field);
  114. return $mapping && in_array($mapping['type'], $this->validTypes);
  115. }
  116. }