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. Gedmo\Exception\InvalidArgumentException;
  6. /**
  7. * This is a yaml mapping driver for Sluggable
  8. * behavioral extension. Used for extraction of extended
  9. * metadata from yaml specificaly for Sluggable
  10. * extension.
  11. *
  12. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  13. * @package Gedmo.Sluggable.Mapping.Driver
  14. * @subpackage Yaml
  15. * @link http://www.gediminasm.org
  16. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  17. */
  18. class Yaml extends File implements Driver
  19. {
  20. /**
  21. * File extension
  22. * @var string
  23. */
  24. protected $_extension = '.dcm.yml';
  25. /**
  26. * List of types which are valid for slug and sluggable fields
  27. *
  28. * @var array
  29. */
  30. private $_validTypes = array(
  31. 'string'
  32. );
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function validateFullMetadata($meta, array $config)
  37. {
  38. if ($config && !isset($config['fields'])) {
  39. throw new InvalidArgumentException("Unable to find any sluggable fields specified for Sluggable entity - {$meta->name}");
  40. }
  41. }
  42. /**
  43. * {@inheritDoc}
  44. */
  45. public function readExtendedMetadata($meta, array &$config) {
  46. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  47. $mapping = $yaml[$meta->name];
  48. if (isset($mapping['fields'])) {
  49. foreach ($mapping['fields'] as $field => $fieldMapping) {
  50. if (isset($fieldMapping['gedmo'])) {
  51. if (in_array('sluggable', $fieldMapping['gedmo'])) {
  52. if (!$this->_isValidField($meta, $field)) {
  53. throw new InvalidArgumentException("Cannot slug field - [{$field}] type is not valid and must be 'string' in class - {$meta->name}");
  54. }
  55. $config['fields'][] = $field;
  56. } elseif (isset($fieldMapping['gedmo']['slug']) || in_array('slug', $fieldMapping['gedmo'])) {
  57. $slug = $fieldMapping['gedmo']['slug'];
  58. if (!$this->_isValidField($meta, $field)) {
  59. throw new InvalidArgumentException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' in class - {$meta->name}");
  60. }
  61. if (isset($config['slug'])) {
  62. throw new InvalidArgumentException("There cannot be two slug fields: [{$slugField}] and [{$config['slug']}], in class - {$meta->name}.");
  63. }
  64. $config['slug'] = $field;
  65. $config['style'] = isset($slug['style']) ?
  66. (string)$slug['style'] : 'default';
  67. $config['updatable'] = isset($slug['updatable']) ?
  68. (bool)$slug['updatable'] : true;
  69. $config['unique'] = isset($slug['unique']) ?
  70. (bool)$slug['unique'] : true;
  71. $config['separator'] = isset($slug['separator']) ?
  72. (string)$slug['separator'] : '-';
  73. }
  74. }
  75. }
  76. }
  77. }
  78. /**
  79. * {@inheritDoc}
  80. */
  81. protected function _loadMappingFile($file)
  82. {
  83. return \Symfony\Component\Yaml\Yaml::load($file);
  84. }
  85. /**
  86. * Checks if $field type is valid as Sluggable field
  87. *
  88. * @param ClassMetadata $meta
  89. * @param string $field
  90. * @return boolean
  91. */
  92. protected function _isValidField($meta, $field)
  93. {
  94. $mapping = $meta->getFieldMapping($field);
  95. return $mapping && in_array($mapping['type'], $this->_validTypes);
  96. }
  97. }