Yaml.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. /**
  41. * {@inheritDoc}
  42. */
  43. public function readExtendedMetadata(ClassMetadata $meta, array &$config)
  44. {
  45. $mapping = $this->_getMapping($meta->name);
  46. if (isset($mapping['fields'])) {
  47. foreach ($mapping['fields'] as $field => $fieldMapping) {
  48. if (isset($fieldMapping['gedmo'])) {
  49. if (isset($fieldMapping['gedmo']['slug'])) {
  50. $slug = $fieldMapping['gedmo']['slug'];
  51. if (!$this->isValidField($meta, $field)) {
  52. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  53. }
  54. // process slug handlers
  55. $handlers = array();
  56. if (isset($slug['handlers'])) {
  57. foreach ($slug['handlers'] as $handlerClass => $options) {
  58. if (!strlen($handlerClass)) {
  59. throw new InvalidMappingException("SlugHandler class: {$handlerClass} should be a valid class name in entity - {$meta->name}");
  60. }
  61. $handlers[$handlerClass] = $options;
  62. $handlerClass::validate($handlers[$handlerClass], $meta);
  63. }
  64. }
  65. // process slug fields
  66. if (empty($slug['fields']) || !is_array($slug['fields'])) {
  67. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  68. }
  69. foreach ($slug['fields'] as $slugField) {
  70. if (!$meta->hasField($slugField) || $meta->isInheritedField($slugField)) {
  71. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  72. }
  73. if (!$this->isValidField($meta, $slugField)) {
  74. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  75. }
  76. }
  77. $config['slugs'][$field]['fields'] = $slug['fields'];
  78. $config['slugs'][$field]['handlers'] = $handlers;
  79. $config['slugs'][$field]['slug'] = $field;
  80. $config['slugs'][$field]['style'] = isset($slug['style']) ?
  81. (string)$slug['style'] : 'default';
  82. $config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
  83. (bool)$slug['updatable'] : true;
  84. $config['slugs'][$field]['unique'] = isset($slug['unique']) ?
  85. (bool)$slug['unique'] : true;
  86. $config['slugs'][$field]['separator'] = isset($slug['separator']) ?
  87. (string)$slug['separator'] : '-';
  88. }
  89. }
  90. }
  91. }
  92. }
  93. /**
  94. * {@inheritDoc}
  95. */
  96. protected function _loadMappingFile($file)
  97. {
  98. return \Symfony\Component\Yaml\Yaml::load($file);
  99. }
  100. /**
  101. * Checks if $field type is valid as Sluggable field
  102. *
  103. * @param ClassMetadata $meta
  104. * @param string $field
  105. * @return boolean
  106. */
  107. protected function isValidField(ClassMetadata $meta, $field)
  108. {
  109. $mapping = $meta->getFieldMapping($field);
  110. return $mapping && in_array($mapping['type'], $this->validTypes);
  111. }
  112. }