Yaml.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. 'text'
  34. );
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function validateFullMetadata(ClassMetadata $meta, array $config)
  39. {
  40. if ($config && !isset($config['fields'])) {
  41. throw new InvalidMappingException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  42. }
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  48. {
  49. $mapping = $this->_getMapping($meta->name);
  50. if (isset($mapping['fields'])) {
  51. foreach ($mapping['fields'] as $field => $fieldMapping) {
  52. if (isset($fieldMapping['gedmo'])) {
  53. if (isset($fieldMapping['gedmo']['sluggable']) || in_array('sluggable', $fieldMapping['gedmo'])) {
  54. if (!$this->isValidField($meta, $field)) {
  55. throw new InvalidMappingException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  56. }
  57. $sluggable = $fieldMapping['gedmo']['sluggable'];
  58. $slugField = (isset($sluggable['slugField'])? $sluggable['slugField']:'slug');
  59. $config['fields'][$slugField][] = array('field' => $field, 'position' => $sluggable['position'], 'slugField' => $slugField);
  60. } elseif (isset($fieldMapping['gedmo']['slug']) || in_array('slug', $fieldMapping['gedmo'])) {
  61. $slug = $fieldMapping['gedmo']['slug'];
  62. if (!$this->isValidField($meta, $field)) {
  63. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  64. }
  65. if (isset($config['slug'])) {
  66. throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  67. }
  68. $config['slugFields'][$field]['slug'] = $field;
  69. $config['slugFields'][$field]['style'] = isset($slug['style']) ?
  70. (string)$slug['style'] : 'default';
  71. $config['slugFields'][$field]['updatable'] = isset($slug['updatable']) ?
  72. (bool)$slug['updatable'] : true;
  73. $config['slugFields'][$field]['unique'] = isset($slug['unique']) ?
  74. (bool)$slug['unique'] : true;
  75. $config['slugFields'][$field]['separator'] = isset($slug['separator']) ?
  76. (string)$slug['separator'] : '-';
  77. }
  78. }
  79. }
  80. }
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. protected function _loadMappingFile($file)
  86. {
  87. return \Symfony\Component\Yaml\Yaml::load($file);
  88. }
  89. /**
  90. * Checks if $field type is valid as Sluggable field
  91. *
  92. * @param ClassMetadata $meta
  93. * @param string $field
  94. * @return boolean
  95. */
  96. protected function isValidField(ClassMetadata $meta, $field)
  97. {
  98. $mapping = $meta->getFieldMapping($field);
  99. return $mapping && in_array($mapping['type'], $this->validTypes);
  100. }
  101. }