Yaml.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 readExtendedMetadata(ClassMetadata $meta, array &$config)
  40. {
  41. $mapping = $this->_getMapping($meta->name);
  42. if (isset($mapping['fields'])) {
  43. foreach ($mapping['fields'] as $field => $fieldMapping) {
  44. if (isset($fieldMapping['gedmo'])) {
  45. if (in_array('sortablePosition', $fieldMapping['gedmo'])) {
  46. if (!$this->isValidField($meta, $field)) {
  47. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  48. }
  49. $config['position'] = $field;
  50. }
  51. }
  52. }
  53. $this->readSortableGroups($mapping['fields'], $config);
  54. }
  55. if (isset($mapping['manyToOne'])) {
  56. $this->readSortableGroups($mapping['manyToOne'], $config);
  57. }
  58. if (isset($mapping['manyToMany'])) {
  59. $this->readSortableGroups($mapping['manyToMany'], $config);
  60. }
  61. if (!$meta->isMappedSuperclass && $config) {
  62. if (!isset($config['position'])) {
  63. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  64. }
  65. }
  66. }
  67. private function readSortableGroups($mapping, array &$config)
  68. {
  69. foreach ($mapping as $field => $fieldMapping) {
  70. if (isset($fieldMapping['gedmo'])) {
  71. if (in_array('sortableGroup', $fieldMapping['gedmo'])) {
  72. if (!isset($config['groups'])) {
  73. $config['groups'] = array();
  74. }
  75. $config['groups'][] = $field;
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. protected function _loadMappingFile($file)
  84. {
  85. return \Symfony\Component\Yaml\Yaml::parse($file);
  86. }
  87. /**
  88. * Checks if $field type is valid as SortablePosition field
  89. *
  90. * @param ClassMetadata $meta
  91. * @param string $field
  92. * @return boolean
  93. */
  94. protected function isValidField($meta, $field)
  95. {
  96. $mapping = $meta->getFieldMapping($field);
  97. return $mapping && in_array($mapping['type'], $this->validTypes);
  98. }
  99. }