Yaml.php 4.7 KB

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