Yaml.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace Gedmo\Sluggable\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File;
  4. use Gedmo\Mapping\Driver;
  5. use 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 specifically 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. 'datetime',
  33. 'citext',
  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. $this->buildFieldConfiguration($field, $fieldMapping, $meta, $config);
  44. }
  45. }
  46. if (isset($mapping['attributeOverride'])) {
  47. foreach ($mapping['attributeOverride'] as $field => $overrideMapping) {
  48. $this->buildFieldConfiguration($field, $overrideMapping, $meta, $config);
  49. }
  50. }
  51. }
  52. /**
  53. * {@inheritDoc}
  54. */
  55. protected function _loadMappingFile($file)
  56. {
  57. return \Symfony\Component\Yaml\Yaml::parse(file_get_contents($file));
  58. }
  59. /**
  60. * Checks if $field type is valid as Sluggable field
  61. *
  62. * @param object $meta
  63. * @param string $field
  64. *
  65. * @return boolean
  66. */
  67. protected function isValidField($meta, $field)
  68. {
  69. $mapping = $meta->getFieldMapping($field);
  70. return $mapping && in_array($mapping['type'], $this->validTypes);
  71. }
  72. private function buildFieldConfiguration($field, array $fieldMapping, $meta, array &$config)
  73. {
  74. if (isset($fieldMapping['gedmo'])) {
  75. if (isset($fieldMapping['gedmo']['slug'])) {
  76. $slug = $fieldMapping['gedmo']['slug'];
  77. if (!$this->isValidField($meta, $field)) {
  78. throw new InvalidMappingException("Cannot use field - [{$field}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  79. }
  80. // process slug handlers
  81. $handlers = array();
  82. if (isset($slug['handlers'])) {
  83. foreach ($slug['handlers'] as $handlerClass => $options) {
  84. if (!strlen($handlerClass)) {
  85. throw new InvalidMappingException("SlugHandler class: {$handlerClass} should be a valid class name in entity - {$meta->name}");
  86. }
  87. $handlers[$handlerClass] = $options;
  88. $handlerClass::validate($handlers[$handlerClass], $meta);
  89. }
  90. }
  91. // process slug fields
  92. if (empty($slug['fields']) || !is_array($slug['fields'])) {
  93. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  94. }
  95. foreach ($slug['fields'] as $slugField) {
  96. if (!$meta->hasField($slugField)) {
  97. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  98. }
  99. if (!$this->isValidField($meta, $slugField)) {
  100. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  101. }
  102. }
  103. $config['slugs'][$field]['fields'] = $slug['fields'];
  104. $config['slugs'][$field]['handlers'] = $handlers;
  105. $config['slugs'][$field]['slug'] = $field;
  106. $config['slugs'][$field]['style'] = isset($slug['style']) ?
  107. (string) $slug['style'] : 'default';
  108. $config['slugs'][$field]['dateFormat'] = isset($slug['dateFormat']) ?
  109. (string) $slug['dateFormat'] : 'Y-m-d-H:i';
  110. $config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
  111. (bool) $slug['updatable'] : true;
  112. $config['slugs'][$field]['unique'] = isset($slug['unique']) ?
  113. (bool) $slug['unique'] : true;
  114. $config['slugs'][$field]['unique_base'] = isset($slug['unique_base']) ?
  115. $slug['unique_base'] : null;
  116. $config['slugs'][$field]['separator'] = isset($slug['separator']) ?
  117. (string) $slug['separator'] : '-';
  118. $config['slugs'][$field]['prefix'] = isset($slug['prefix']) ?
  119. (string) $slug['prefix'] : '';
  120. $config['slugs'][$field]['suffix'] = isset($slug['suffix']) ?
  121. (string) $slug['suffix'] : '';
  122. if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  123. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  124. }
  125. $ubase = $config['slugs'][$field]['unique_base'];
  126. if ($config['slugs'][$field]['unique'] === false && $ubase) {
  127. throw new InvalidMappingException("Slug annotation [unique_base] can not be set if unique is unset or 'false'");
  128. }
  129. if ($ubase && !$meta->hasField($ubase) && !$meta->hasAssociation($ubase)) {
  130. throw new InvalidMappingException("Unable to find [{$ubase}] as mapped property in entity - {$meta->name}");
  131. }
  132. }
  133. }
  134. }
  135. }