Yaml.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. 'int',
  35. );
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function readExtendedMetadata($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 fields
  51. if (empty($slug['fields']) || !is_array($slug['fields'])) {
  52. throw new InvalidMappingException("Slug must contain at least one field for slug generation in class - {$meta->name}");
  53. }
  54. foreach ($slug['fields'] as $slugField) {
  55. if (!$meta->hasField($slugField) || $meta->isInheritedField($slugField)) {
  56. throw new InvalidMappingException("Unable to find slug [{$slugField}] as mapped property in entity - {$meta->name}");
  57. }
  58. if (!$this->isValidField($meta, $slugField)) {
  59. throw new InvalidMappingException("Cannot use field - [{$slugField}] for slug storage, type is not valid and must be 'string' or 'text' in class - {$meta->name}");
  60. }
  61. }
  62. $config['slugs'][$field]['fields'] = $slug['fields'];
  63. $config['slugs'][$field]['slug'] = $field;
  64. $config['slugs'][$field]['style'] = isset($slug['style']) ?
  65. (string)$slug['style'] : 'default';
  66. $config['slugs'][$field]['updatable'] = isset($slug['updatable']) ?
  67. (bool)$slug['updatable'] : true;
  68. $config['slugs'][$field]['unique'] = isset($slug['unique']) ?
  69. (bool)$slug['unique'] : true;
  70. $config['slugs'][$field]['separator'] = isset($slug['separator']) ?
  71. (string)$slug['separator'] : '-';
  72. if (!$meta->isMappedSuperclass && $meta->isIdentifier($field) && !$config['slugs'][$field]['unique']) {
  73. throw new InvalidMappingException("Identifier field - [{$field}] slug must be unique in order to maintain primary key in class - {$meta->name}");
  74. }
  75. }
  76. }
  77. }
  78. }
  79. }
  80. /**
  81. * {@inheritDoc}
  82. */
  83. protected function _loadMappingFile($file)
  84. {
  85. return \Symfony\Component\Yaml\Yaml::parse($file);
  86. }
  87. /**
  88. * Checks if $field type is valid as Sluggable field
  89. *
  90. * @param object $meta
  91. * @param string $field
  92. * @return boolean
  93. */
  94. protected function isValidField($meta, $field)
  95. {
  96. $mapping = $meta->getFieldMapping($field);
  97. return $mapping && in_array($mapping['type'], $this->validTypes);
  98. }
  99. }