Yaml.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace Gedmo\Sortable\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 Sortable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specificaly for Sortable
  10. * extension.
  11. *
  12. * @author Lukas Botsch <lukas.botsch@gmail.com>
  13. * @package Gedmo.Sortable.Mapping.Driver
  14. * @subpackage Yaml
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Yaml extends File implements Driver
  19. {
  20. /**
  21. * File extension
  22. * @var string
  23. */
  24. protected $_extension = '.dcm.yml';
  25. /**
  26. * List of types which are valid for position fields
  27. *
  28. * @var array
  29. */
  30. private $validTypes = array(
  31. 'integer',
  32. 'smallint',
  33. 'bigint'
  34. );
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function readExtendedMetadata($meta, array &$config)
  39. {
  40. $mapping = $this->_getMapping($meta->name);
  41. if (isset($mapping['fields'])) {
  42. foreach ($mapping['fields'] as $field => $fieldMapping) {
  43. if (isset($fieldMapping['gedmo'])) {
  44. if (in_array('sortablePosition', $fieldMapping['gedmo'])) {
  45. if (!$this->isValidField($meta, $field)) {
  46. throw new InvalidMappingException("Sortable position field - [{$field}] type is not valid and must be 'integer' in class - {$meta->name}");
  47. }
  48. $config['position'] = $field;
  49. }
  50. }
  51. }
  52. $this->readSortableGroups($mapping['fields'], $config);
  53. }
  54. if (isset($mapping['manyToOne'])) {
  55. $this->readSortableGroups($mapping['manyToOne'], $config);
  56. }
  57. if (isset($mapping['manyToMany'])) {
  58. $this->readSortableGroups($mapping['manyToMany'], $config);
  59. }
  60. if (!$meta->isMappedSuperclass && $config) {
  61. if (!isset($config['position'])) {
  62. throw new InvalidMappingException("Missing property: 'position' in class - {$meta->name}");
  63. }
  64. }
  65. }
  66. private function readSortableGroups($mapping, array &$config)
  67. {
  68. foreach ($mapping as $field => $fieldMapping) {
  69. if (isset($fieldMapping['gedmo'])) {
  70. if (in_array('sortableGroup', $fieldMapping['gedmo'])) {
  71. if (!isset($config['groups'])) {
  72. $config['groups'] = array();
  73. }
  74. $config['groups'][] = $field;
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. protected function _loadMappingFile($file)
  83. {
  84. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  85. }
  86. /**
  87. * Checks if $field type is valid as SortablePosition field
  88. *
  89. * @param object $meta
  90. * @param string $field
  91. * @return boolean
  92. */
  93. protected function isValidField($meta, $field)
  94. {
  95. $mapping = $meta->getFieldMapping($field);
  96. return $mapping && in_array($mapping['type'], $this->validTypes);
  97. }
  98. }