Yaml.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace Gedmo\Sortable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Gedmo\Exception\InvalidMappingException;
  6. use Doctrine\Common\Persistence\Mapping\ClassMetadata;
  7. /**
  8. * This is a yaml mapping driver for Sortable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Sortable
  11. * extension.
  12. *
  13. * @author Lukas Botsch <lukas.botsch@gmail.com>
  14. * @package Gedmo.Sortable.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 position fields
  28. *
  29. * @var array
  30. */
  31. private $validTypes = array(
  32. 'integer',
  33. 'smallint',
  34. 'bigint'
  35. );
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function validateFullMetadata(ClassMetadata $meta, array $config)
  40. {
  41. if ($config && !isset($config['position'])) {
  42. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  43. }
  44. }
  45. /**
  46. * {@inheritDoc}
  47. */
  48. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  49. {
  50. $mapping = $this->_getMapping($meta->name);
  51. if (isset($mapping['fields'])) {
  52. foreach ($mapping['fields'] as $field => $fieldMapping) {
  53. if (isset($fieldMapping['gedmo'])) {
  54. if (in_array('sortablePosition', $fieldMapping['gedmo'])) {
  55. if (!$this->isValidField($meta, $field)) {
  56. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  57. }
  58. $config['position'] = $field;
  59. }
  60. }
  61. }
  62. $this->readSortableGroups($mapping['fields'], $config);
  63. }
  64. if (isset($mapping['manyToOne'])) {
  65. $this->readSortableGroups($mapping['manyToOne'], $config);
  66. }
  67. if (isset($mapping['manyToMany'])) {
  68. $this->readSortableGroups($mapping['manyToMany'], $config);
  69. }
  70. }
  71. private function readSortableGroups($mapping, array &$config)
  72. {
  73. foreach ($mapping as $field => $fieldMapping) {
  74. if (isset($fieldMapping['gedmo'])) {
  75. if (in_array('sortableGroup', $fieldMapping['gedmo'])) {
  76. if (!isset($config['groups'])) {
  77. $config['groups'] = array();
  78. }
  79. $config['groups'][] = $field;
  80. }
  81. }
  82. }
  83. }
  84. /**
  85. * {@inheritDoc}
  86. */
  87. protected function _loadMappingFile($file)
  88. {
  89. return \Symfony\Component\Yaml\Yaml::parse($file);
  90. }
  91. /**
  92. * Checks if $field type is valid as SortablePosition field
  93. *
  94. * @param ClassMetadata $meta
  95. * @param string $field
  96. * @return boolean
  97. */
  98. protected function isValidField($meta, $field)
  99. {
  100. $mapping = $meta->getFieldMapping($field);
  101. return $mapping && in_array($mapping['type'], $this->validTypes);
  102. }
  103. }