Yaml.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 (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'][array_search('sluggable', $fieldMapping['gedmo'])];
  58. <<<<<<< HEAD
  59. $config['fields'][] = array('field' => $field, 'position' => $sluggable['position']);
  60. =======
  61. $slugField = isset($sluggable['slugField'])? $sluggable['slugField']:'slug';
  62. $config['fields'][$slugField][] = array('field' => $field, 'position' => $sluggable['position'], 'slugField' => $slugField);
  63. >>>>>>> a6dd4fd... Fixed coding standard problems
  64. } elseif (isset($fieldMapping['gedmo']['slug']) || in_array('slug', $fieldMapping['gedmo'])) {
  65. $slug = $fieldMapping['gedmo']['slug'];
  66. if (!$this->isValidField($meta, $field)) {
  67. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  68. }
  69. if (isset($config['slug'])) {
  70. throw new InvalidMappingException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  71. }
  72. $config['slug'] = $field;
  73. $config['style'] = isset($slug['style']) ?
  74. (string)$slug['style'] : 'default';
  75. $config['updatable'] = isset($slug['updatable']) ?
  76. (bool)$slug['updatable'] : true;
  77. $config['unique'] = isset($slug['unique']) ?
  78. (bool)$slug['unique'] : true;
  79. $config['separator'] = isset($slug['separator']) ?
  80. (string)$slug['separator'] : '-';
  81. }
  82. }
  83. }
  84. }
  85. }
  86. /**
  87. * {@inheritDoc}
  88. */
  89. protected function _loadMappingFile($file)
  90. {
  91. return \Symfony\Component\Yaml\Yaml::load($file);
  92. }
  93. /**
  94. * Checks if $field type is valid as Sluggable field
  95. *
  96. * @param ClassMetadata $meta
  97. * @param string $field
  98. * @return boolean
  99. */
  100. protected function isValidField(ClassMetadata $meta, $field)
  101. {
  102. $mapping = $meta->getFieldMapping($field);
  103. return $mapping && in_array($mapping['type'], $this->validTypes);
  104. }
  105. }