Yaml.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\Common\Persistence\Mapping\ClassMetadata,
  6. Gedmo\Exception\InvalidMappingException;
  7. /**
  8. * This is a yaml mapping driver for Sluggable
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Sluggable
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Sluggable.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 slug and sluggable fields
  28. *
  29. * @var array
  30. */
  31. private $validTypes = array(
  32. 'string'
  33. );
  34. /**
  35. * {@inheritDoc}
  36. */
  37. public function validateFullMetadata(ClassMetadata $meta, array $config)
  38. {
  39. if (!isset($config['fields'])) {
  40. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  41. }
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function readExtendedMetadata(ClassMetadata $meta, array &$config) {
  47. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  48. $mapping = $yaml[$meta->name];
  49. if (isset($mapping['fields'])) {
  50. foreach ($mapping['fields'] as $field => $fieldMapping) {
  51. if (isset($fieldMapping['gedmo'])) {
  52. if (in_array('sluggable', $fieldMapping['gedmo'])) {
  53. if (!$this->isValidField($meta, $field)) {
  54. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  55. }
  56. $sluggable = $fieldMapping['gedmo'][array_search('sluggable', $fieldMapping['gedmo'])];
  57. $config['fields'][] = array('field' => $field, 'position' => $sluggable['position']);
  58. } elseif (isset($fieldMapping['gedmo']['slug']) || in_array('slug', $fieldMapping['gedmo'])) {
  59. $slug = $fieldMapping['gedmo']['slug'];
  60. if (!$this->isValidField($meta, $field)) {
  61. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  62. }
  63. if (isset($config['slug'])) {
  64. throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  65. }
  66. $config['slug'] = $field;
  67. $config['style'] = isset($slug['style']) ?
  68. (string)$slug['style'] : 'default';
  69. $config['updatable'] = isset($slug['updatable']) ?
  70. (bool)$slug['updatable'] : true;
  71. $config['unique'] = isset($slug['unique']) ?
  72. (bool)$slug['unique'] : true;
  73. $config['separator'] = isset($slug['separator']) ?
  74. (string)$slug['separator'] : '-';
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. protected function _loadMappingFile($file)
  84. {
  85. return \Symfony\Component\Yaml\Yaml::load($file);
  86. }
  87. /**
  88. * Checks if $field type is valid as Sluggable field
  89. *
  90. * @param ClassMetadata $meta
  91. * @param string $field
  92. * @return boolean
  93. */
  94. protected function isValidField(ClassMetadata $meta, $field)
  95. {
  96. $mapping = $meta->getFieldMapping($field);
  97. return $mapping && in_array($mapping['type'], $this->validTypes);
  98. }
  99. }