Yaml.php 4.9 KB

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