Validator.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace Gedmo\Tree\Mapping;
  3. use Gedmo\Exception\InvalidMappingException;
  4. /**
  5. * This is a validator for all mapping drivers for Tree
  6. * behavioral extension, containing methods to validate
  7. * mapping information
  8. *
  9. * @author Gustavo Falco <comfortablynumb84@gmail.com>
  10. * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
  11. * @author <rocco@roccosportal.com>
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. */
  14. class Validator
  15. {
  16. /**
  17. * List of types which are valid for tree fields
  18. *
  19. * @var array
  20. */
  21. private $validTypes = array(
  22. 'integer',
  23. 'smallint',
  24. 'bigint',
  25. 'int'
  26. );
  27. /**
  28. * List of types which are valid for the path (materialized path strategy)
  29. *
  30. * @var array
  31. */
  32. private $validPathTypes = array(
  33. 'string',
  34. 'text'
  35. );
  36. /**
  37. * List of types which are valid for the path source (materialized path strategy)
  38. *
  39. * @var array
  40. */
  41. private $validPathSourceTypes = array(
  42. 'id',
  43. 'integer',
  44. 'smallint',
  45. 'bigint',
  46. 'string',
  47. 'int',
  48. 'float'
  49. );
  50. /**
  51. * List of types which are valid for the path hash (materialized path strategy)
  52. *
  53. * @var array
  54. */
  55. private $validPathHashTypes = array(
  56. 'string'
  57. );
  58. /**
  59. * List of types which are valid for the path source (materialized path strategy)
  60. *
  61. * @var array
  62. */
  63. private $validRootTypes = array(
  64. 'integer',
  65. 'smallint',
  66. 'bigint',
  67. 'int',
  68. 'string'
  69. );
  70. /**
  71. * Checks if $field type is valid
  72. *
  73. * @param object $meta
  74. * @param string $field
  75. * @return boolean
  76. */
  77. public function isValidField($meta, $field)
  78. {
  79. $mapping = $meta->getFieldMapping($field);
  80. return $mapping && in_array($mapping['type'], $this->validTypes);
  81. }
  82. /**
  83. * Checks if $field type is valid for Path field
  84. *
  85. * @param object $meta
  86. * @param string $field
  87. * @return boolean
  88. */
  89. public function isValidFieldForPath($meta, $field)
  90. {
  91. $mapping = $meta->getFieldMapping($field);
  92. return $mapping && in_array($mapping['type'], $this->validPathTypes);
  93. }
  94. /**
  95. * Checks if $field type is valid for PathSource field
  96. *
  97. * @param object $meta
  98. * @param string $field
  99. * @return boolean
  100. */
  101. public function isValidFieldForPathSource($meta, $field)
  102. {
  103. $mapping = $meta->getFieldMapping($field);
  104. return $mapping && in_array($mapping['type'], $this->validPathSourceTypes);
  105. }
  106. /**
  107. * Checks if $field type is valid for PathHash field
  108. *
  109. * @param object $meta
  110. * @param string $field
  111. * @return boolean
  112. */
  113. public function isValidFieldForPathHash($meta, $field)
  114. {
  115. $mapping = $meta->getFieldMapping($field);
  116. return $mapping && in_array($mapping['type'], $this->validPathHashTypes);
  117. }
  118. /**
  119. * Checks if $field type is valid for LockTime field
  120. *
  121. * @param object $meta
  122. * @param string $field
  123. * @return boolean
  124. */
  125. public function isValidFieldForLockTime($meta, $field)
  126. {
  127. $mapping = $meta->getFieldMapping($field);
  128. return $mapping && ($mapping['type'] === 'date' || $mapping['type'] === 'datetime' || $mapping['type'] === 'timestamp');
  129. }
  130. /**
  131. * Checks if $field type is valid for Root field
  132. *
  133. * @param object $meta
  134. * @param string $field
  135. * @return boolean
  136. */
  137. public function isValidFieldForRoot($meta, $field)
  138. {
  139. $mapping = $meta->getFieldMapping($field);
  140. return $mapping && in_array($mapping['type'], $this->validRootTypes);
  141. }
  142. /**
  143. * Validates metadata for nested type tree
  144. *
  145. * @param object $meta
  146. * @param array $config
  147. * @throws InvalidMappingException
  148. * @return void
  149. */
  150. public function validateNestedTreeMetadata($meta, array $config)
  151. {
  152. $missingFields = array();
  153. if (!isset($config['parent'])) {
  154. $missingFields[] = 'ancestor';
  155. }
  156. if (!isset($config['left'])) {
  157. $missingFields[] = 'left';
  158. }
  159. if (!isset($config['right'])) {
  160. $missingFields[] = 'right';
  161. }
  162. if ($missingFields) {
  163. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  164. }
  165. }
  166. /**
  167. * Validates metadata for closure type tree
  168. *
  169. * @param object $meta
  170. * @param array $config
  171. * @throws InvalidMappingException
  172. * @return void
  173. */
  174. public function validateClosureTreeMetadata($meta, array $config)
  175. {
  176. $missingFields = array();
  177. if (!isset($config['parent'])) {
  178. $missingFields[] = 'ancestor';
  179. }
  180. if (!isset($config['closure'])) {
  181. $missingFields[] = 'closure class';
  182. }
  183. if ($missingFields) {
  184. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  185. }
  186. }
  187. /**
  188. * Validates metadata for materialized path type tree
  189. *
  190. * @param object $meta
  191. * @param array $config
  192. * @throws InvalidMappingException
  193. * @return void
  194. */
  195. public function validateMaterializedPathTreeMetadata($meta, array $config)
  196. {
  197. $missingFields = array();
  198. if (!isset($config['parent'])) {
  199. $missingFields[] = 'ancestor';
  200. }
  201. if (!isset($config['path'])) {
  202. $missingFields[] = 'path';
  203. }
  204. if (!isset($config['path_source'])) {
  205. $missingFields[] = 'path_source';
  206. }
  207. if ($missingFields) {
  208. throw new InvalidMappingException("Missing properties: " . implode(', ', $missingFields) . " in class - {$meta->name}");
  209. }
  210. }
  211. }