Yaml.php 5.2 KB

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