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