Anonymous 3D Imageboard http://cyberia.digital/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

208 lines
5.3KB

  1. <?php
  2. namespace Lazer\Classes\Helpers;
  3. use Lazer\Classes\LazerException;
  4. use Lazer\Classes\Relation;
  5. /**
  6. * Validation for tables
  7. *
  8. * @category Helpers
  9. * @author Grzegorz Kuźnik
  10. * @copyright (c) 2013, Grzegorz Kuźnik
  11. * @license http://opensource.org/licenses/MIT The MIT License
  12. * @link https://github.com/Greg0/Lazer-Database GitHub Repository
  13. */
  14. class Validate {
  15. /**
  16. * Name of table
  17. * @var string
  18. */
  19. private $name;
  20. /**
  21. * Table name
  22. * @param string $name
  23. * @return Validate
  24. */
  25. public static function table($name)
  26. {
  27. $validate = new Validate();
  28. $validate->name = $name;
  29. return $validate;
  30. }
  31. /**
  32. * Checking that field type is numeric
  33. * @param string $type
  34. * @return boolean
  35. */
  36. public static function isNumeric($type)
  37. {
  38. $defined = array('integer', 'double');
  39. if (in_array($type, $defined))
  40. {
  41. return TRUE;
  42. }
  43. return FALSE;
  44. }
  45. /**
  46. * Checking that types from array matching with [boolean, integer, string, double]
  47. * @param array $types Indexed array
  48. * @return bool
  49. * @throws LazerException
  50. */
  51. public static function types(array $types)
  52. {
  53. $defined = array('boolean', 'integer', 'string', 'double');
  54. $diff = array_diff($types, $defined);
  55. if (empty($diff))
  56. {
  57. return TRUE;
  58. }
  59. throw new LazerException('Wrong types: "' . implode(', ', $diff) . '". Available "boolean, integer, string, double"');
  60. }
  61. /**
  62. * Delete ID field from arrays
  63. * @param array $fields
  64. * @return array Fields without ID
  65. */
  66. public static function filter(array $fields)
  67. {
  68. if (array_values($fields) === $fields)
  69. {
  70. if (($key = array_search('id', $fields)) !== false)
  71. {
  72. unset($fields[$key]);
  73. }
  74. }
  75. else
  76. {
  77. unset($fields['id']);
  78. }
  79. return $fields;
  80. }
  81. /**
  82. * Change keys and values case to lower
  83. * @param array $array
  84. * @return array
  85. */
  86. public static function arrToLower(array $array)
  87. {
  88. $array = array_change_key_case($array);
  89. $array = array_map('strtolower', $array);
  90. return $array;
  91. }
  92. /**
  93. * Checking that typed fields really exist in table
  94. * @param array $fields Indexed array
  95. * @return boolean
  96. * @throws LazerException If field(s) does not exist
  97. */
  98. public function fields(array $fields)
  99. {
  100. $fields = self::filter($fields);
  101. $diff = array_diff($fields, Config::table($this->name)->fields());
  102. if (empty($diff))
  103. {
  104. return TRUE;
  105. }
  106. throw new LazerException('Field(s) "' . implode(', ', $diff) . '" does not exists in table "' . $this->name . '"');
  107. }
  108. /**
  109. * Checking that typed field really exist in table
  110. * @param string $name
  111. * @return boolean
  112. * @throws LazerException If field does not exist
  113. */
  114. public function field($name)
  115. {
  116. if (in_array($name, Config::table($this->name)->fields()))
  117. {
  118. return TRUE;
  119. }
  120. throw new LazerException('Field ' . $name . ' does not exists in table "' . $this->name . '"');
  121. }
  122. /**
  123. * Checking that Table and Config exists and throw exceptions if not
  124. * @return boolean
  125. * @throws LazerException
  126. */
  127. public function exists()
  128. {
  129. if (!Data::table($this->name)->exists())
  130. throw new LazerException('Table "' . $this->name . '" does not exists');
  131. if (!Config::table($this->name)->exists())
  132. throw new LazerException('Config "' . $this->name . '" does not exists');
  133. return TRUE;
  134. }
  135. /**
  136. * Checking that typed field have correct type of value
  137. * @param string $name
  138. * @param mixed $value
  139. * @return boolean
  140. * @throws LazerException If type is wrong
  141. */
  142. public function type($name, $value)
  143. {
  144. $schema = Config::table($this->name)->schema();
  145. if (array_key_exists($name, $schema) && $schema[$name] == gettype($value))
  146. {
  147. return TRUE;
  148. }
  149. throw new LazerException('Wrong data type');
  150. }
  151. /**
  152. * Checking that relation between tables exists
  153. * @param string $local local table
  154. * @param string $foreign related table
  155. * @return bool relation exists
  156. * @throws LazerException
  157. */
  158. public static function relation($local, $foreign)
  159. {
  160. $relations = Config::table($local)->relations();
  161. if (isset($relations->{$foreign}))
  162. {
  163. return TRUE;
  164. }
  165. throw new LazerException('Relation "' . $local . '" to "' . $foreign . '" doesn\'t exist');
  166. }
  167. /**
  168. * Checking that relation type is correct
  169. * @param string $type
  170. * @return bool relation type
  171. * @throws LazerException Wrong relation type
  172. */
  173. public static function relationType($type)
  174. {
  175. if (in_array($type, Relation::relations()))
  176. {
  177. return true;
  178. }
  179. throw new LazerException('Wrong relation type');
  180. }
  181. }