Yaml.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  14. */
  15. class Yaml extends File implements Driver
  16. {
  17. /**
  18. * File extension
  19. * @var string
  20. */
  21. protected $_extension = '.dcm.yml';
  22. /**
  23. * List of types which are valid for slug and sluggable fields
  24. *
  25. * @var array
  26. */
  27. private $validTypes = array(
  28. 'string',
  29. 'text',
  30. 'integer',
  31. 'int',
  32. );
  33. /**
  34. * {@inheritDoc}
  35. */
  36. public function readExtendedMetadata($meta, array &$config)
  37. {
  38. $mapping = $this->_getMapping($meta->name);
  39. if (isset($mapping['fields'])) {
  40. foreach ($mapping['fields'] as $field => $fieldMapping) {
  41. if (isset($fieldMapping['gedmo'])) {
  42. if (isset($fieldMapping['gedmo']['slug'])) {
  43. $slug = $fieldMapping['gedmo']['slug'];
  44. if (!$this->isValidField($meta, $field)) {
  45. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  46. }
  47. // process slug handlers
  48. $handlers = array();
  49. if (isset($slug['handlers'])) {
  50. foreach ($slug['handlers'] as $handlerClass => $options) {
  51. if (!strlen($handlerClass)) {
  52. throw new InvalidMappingException("SlugHandler class: {$handlerClass} should be a valid class name in entity - {$meta->name}");
  53. }
  54. $handlers[$handlerClass] = $options;
  55. $handlerClass::validate($handlers[$handlerClass], $meta);
  56. }
  57. }
  58. // process slug fields
  59. if (empty($slug['fields']) || !is_array($slug['fields'])) {
  60. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  61. }
  62. foreach ($slug['fields'] as $slugField) {
  63. if (!$meta->hasField($slugField)) {
  64. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  65. }
  66. if (!$this->isValidField($meta, $slugField)) {
  67. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  68. }
  69. }
  70. $config['slugs'][$field]['fields'] = $slug['fields'];
  71. $config['slugs'][$field]['handlers'] = $handlers;
  72. $config['slugs'][$field]['slug'] = $field;
  73. $config['slugs'][$field]['style'] = isset($slug['style']) ?
  74. (string)$slug['style'] : 'default';
  75. $config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
  76. (bool)$slug['updatable'] : true;
  77. $config['slugs'][$field]['unique'] = isset($slug['unique']) ?
  78. (bool)$slug['unique'] : true;
  79. $config['slugs'][$field]['unique_base'] = isset($slug['unique_base']) ?
  80. $slug['unique_base'] : null;
  81. $config['slugs'][$field]['separator'] = isset($slug['separator']) ?
  82. (string)$slug['separator'] : '-';
  83. $config['slugs'][$field]['prefix'] = isset($slug['prefix']) ?
  84. (string)$slug['prefix'] : '';
  85. $config['slugs'][$field]['suffix'] = isset($slug['suffix']) ?
  86. (string)$slug['suffix'] : '';
  87. if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  88. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  89. }
  90. $ubase = $config['slugs'][$field]['unique_base'];
  91. if ($config['slugs'][$field]['unique'] === false && $ubase) {
  92. throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
  93. }
  94. if ($ubase && !$this->isValidField($meta, $ubase) && !$meta->hasAssociation($ubase)) {
  95. throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
  96. }
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. protected function _loadMappingFile($file)
  106. {
  107. return \Symfony\Component\Yaml\Yaml::parse($file);
  108. }
  109. /**
  110. * Checks if $field type is valid as Sluggable field
  111. *
  112. * @param object $meta
  113. * @param string $field
  114. * @return boolean
  115. */
  116. protected function isValidField($meta, $field)
  117. {
  118. $mapping = $meta->getFieldMapping($field);
  119. return $mapping && in_array($mapping['type'], $this->validTypes);
  120. }
  121. }