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