Yaml.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\ORM\Mapping\ClassMetadataInfo,
  6. Gedmo\Exception\InvalidArgumentException;
  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(ClassMetadataInfo $meta, array $config)
  38. {
  39. if ($config && !isset($config['fields'])) {
  40. throw new InvalidArgumentException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  41. }
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function readExtendedMetadata(ClassMetadataInfo $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 InvalidArgumentException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  55. }
  56. $config['fields'][] = $field;
  57. } elseif (isset($fieldMapping['gedmo']['slug']) || in_array('slug', $fieldMapping['gedmo'])) {
  58. $slug = $fieldMapping['gedmo']['slug'];
  59. if (!$this->_isValidField($meta, $field)) {
  60. throw new InvalidArgumentException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  61. }
  62. if (isset($config['slug'])) {
  63. throw new InvalidArgumentException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  64. }
  65. $config['slug'] = $field;
  66. $config['style'] = isset($slug['style']) ?
  67. (string)$slug['style'] : 'default';
  68. $config['updatable'] = isset($slug['updatable']) ?
  69. (bool)$slug['updatable'] : true;
  70. $config['unique'] = isset($slug['unique']) ?
  71. (bool)$slug['unique'] : true;
  72. $config['separator'] = isset($slug['separator']) ?
  73. (string)$slug['separator'] : '-';
  74. }
  75. }
  76. }
  77. }
  78. }
  79. /**
  80. * {@inheritDoc}
  81. */
  82. protected function _loadMappingFile($file)
  83. {
  84. return \Symfony\Component\Yaml\Yaml::load($file);
  85. }
  86. /**
  87. * Checks if $field type is valid as Sluggable field
  88. *
  89. * @param ClassMetadataInfo $meta
  90. * @param string $field
  91. * @return boolean
  92. */
  93. protected function _isValidField(ClassMetadataInfo $meta, $field)
  94. {
  95. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  96. }
  97. }