Yaml.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Gedmo\Tree\Mapping\Driver;
  3. use Gedmo\Mapping\Driver\File,
  4. Gedmo\Mapping\Driver,
  5. Doctrine\ORM\Mapping\ClassMetadataInfo,
  6. Gedmo\Tree\Mapping\MappingException;
  7. /**
  8. * This is a yaml mapping driver for Tree
  9. * behavioral extension. Used for extraction of extended
  10. * metadata from yaml specificaly for Tree
  11. * extension.
  12. *
  13. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  14. * @package Gedmo.Tree.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 timestamp
  28. *
  29. * @var array
  30. */
  31. private $_validTypes = array(
  32. 'integer',
  33. 'smallint',
  34. 'bigint'
  35. );
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function validateFullMetadata(ClassMetadataInfo $meta, array $config)
  40. {
  41. if ($config) {
  42. $missingFields = array();
  43. if (!isset($config['parent'])) {
  44. $missingFields[] = 'ancestor';
  45. }
  46. if (!isset($config['left'])) {
  47. $missingFields[] = 'left';
  48. }
  49. if (!isset($config['right'])) {
  50. $missingFields[] = 'right';
  51. }
  52. if ($missingFields) {
  53. throw MappingException::missingMetaProperties($missingFields, $meta->name);
  54. }
  55. }
  56. }
  57. /**
  58. * {@inheritDoc}
  59. */
  60. public function readExtendedMetadata(ClassMetadataInfo $meta, array &$config) {
  61. $yaml = $this->_loadMappingFile($this->_findMappingFile($meta->name));
  62. $mapping = $yaml[$meta->name];
  63. if (isset($mapping['fields'])) {
  64. foreach ($mapping['fields'] as $field => $fieldMapping) {
  65. if (isset($fieldMapping['gedmo'])) {
  66. if (in_array('treeLeft', $fieldMapping['gedmo'])) {
  67. if (!$this->_isValidField($meta, $field)) {
  68. throw MappingException::notValidFieldType($field, $meta->name);
  69. }
  70. $config['left'] = $field;
  71. } elseif (in_array('treeRight', $fieldMapping['gedmo'])) {
  72. if (!$this->_isValidField($meta, $field)) {
  73. throw MappingException::notValidFieldType($field, $meta->name);
  74. }
  75. $config['right'] = $field;
  76. } elseif (in_array('treeLevel', $fieldMapping['gedmo'])) {
  77. if (!$this->_isValidField($meta, $field)) {
  78. throw MappingException::notValidFieldType($field, $meta->name);
  79. }
  80. $config['level'] = $field;
  81. }
  82. }
  83. }
  84. }
  85. if (isset($mapping['manyToOne'])) {
  86. foreach ($mapping['manyToOne'] as $field => $relationMapping) {
  87. if (isset($relationMapping['gedmo'])) {
  88. if (in_array('treeParent', $relationMapping['gedmo'])) {
  89. if ($relationMapping['targetEntity'] != $meta->name) {
  90. throw MappingException::parentFieldNotMappedOrRelated($field, $meta->name);
  91. }
  92. $config['parent'] = $field;
  93. }
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * {@inheritDoc}
  100. */
  101. protected function _loadMappingFile($file)
  102. {
  103. return \Symfony\Component\Yaml\Yaml::load($file);
  104. }
  105. /**
  106. * Checks if $field type is valid
  107. *
  108. * @param ClassMetadataInfo $meta
  109. * @param string $field
  110. * @return boolean
  111. */
  112. protected function _isValidField(ClassMetadataInfo $meta, $field)
  113. {
  114. return in_array($meta->getTypeOfField($field), $this->_validTypes);
  115. }
  116. }