Yaml.php 4.2 KB

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