ValidateTrait.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. <?php
  2. namespace ZN\Validation;
  3. trait ValidateTrait
  4. {
  5. //----------------------------------------------------------------------------------------------------
  6. //
  7. // Yazar : Ozan UYKUN <ozanbote@windowslive.com> | <ozanbote@gmail.com>
  8. // Site : www.zntr.net
  9. // Lisans : The MIT License
  10. // Telif Hakkı: Copyright (c) 2012-2016, zntr.net
  11. //
  12. //----------------------------------------------------------------------------------------------------
  13. /* Settings Değişkeni
  14. *
  15. * Ayarlar bilgisini tutar.
  16. *
  17. */
  18. protected $settings = [];
  19. //----------------------------------------------------------------------------------------------------
  20. // name()
  21. //----------------------------------------------------------------------------------------------------
  22. //
  23. // @param string $name
  24. //
  25. //----------------------------------------------------------------------------------------------------
  26. public function name($name = '')
  27. {
  28. $this->settings['name'] = $name;
  29. return $this;
  30. }
  31. //----------------------------------------------------------------------------------------------------
  32. // method()
  33. //----------------------------------------------------------------------------------------------------
  34. //
  35. // @param string $method
  36. //
  37. //----------------------------------------------------------------------------------------------------
  38. public function method($method = 'post')
  39. {
  40. $this->settings['method'] = $method;
  41. return $this;
  42. }
  43. //----------------------------------------------------------------------------------------------------
  44. // value()
  45. //----------------------------------------------------------------------------------------------------
  46. //
  47. // @param string $value
  48. //
  49. //----------------------------------------------------------------------------------------------------
  50. public function value($value = '')
  51. {
  52. $this->settings['value'] = $value;
  53. return $this;
  54. }
  55. //----------------------------------------------------------------------------------------------------
  56. // required()
  57. //----------------------------------------------------------------------------------------------------
  58. //
  59. // @param void
  60. //
  61. //----------------------------------------------------------------------------------------------------
  62. public function required()
  63. {
  64. $this->settings['config'][] = 'required';
  65. return $this;
  66. }
  67. //----------------------------------------------------------------------------------------------------
  68. // numeric()
  69. //----------------------------------------------------------------------------------------------------
  70. //
  71. // @param void
  72. //
  73. //----------------------------------------------------------------------------------------------------
  74. public function numeric()
  75. {
  76. $this->settings['config'][] = 'numeric';
  77. return $this;
  78. }
  79. //----------------------------------------------------------------------------------------------------
  80. // match()
  81. //----------------------------------------------------------------------------------------------------
  82. //
  83. // @param string $match
  84. //
  85. //----------------------------------------------------------------------------------------------------
  86. public function match($match = '')
  87. {
  88. $this->settings['config']['match'] = $match;
  89. return $this;
  90. }
  91. //----------------------------------------------------------------------------------------------------
  92. // matchPassword()
  93. //----------------------------------------------------------------------------------------------------
  94. //
  95. // @param string $match
  96. //
  97. //----------------------------------------------------------------------------------------------------
  98. public function matchPassword($match = '')
  99. {
  100. $this->settings['config']['matchPassword'] = $match;
  101. return $this;
  102. }
  103. //----------------------------------------------------------------------------------------------------
  104. // oldPassword()
  105. //----------------------------------------------------------------------------------------------------
  106. //
  107. // @param string $oldPassword
  108. //
  109. //----------------------------------------------------------------------------------------------------
  110. public function oldPassword($oldPassword = '')
  111. {
  112. $this->settings['config']['oldPassword'] = $oldPassword;
  113. return $this;
  114. }
  115. //----------------------------------------------------------------------------------------------------
  116. // compare()
  117. //----------------------------------------------------------------------------------------------------
  118. //
  119. // @param numeric $min
  120. // @param numeric $max
  121. //
  122. //----------------------------------------------------------------------------------------------------
  123. public function compare($min = '', $max = '')
  124. {
  125. $this->settings['config']['minchar'] = $min;
  126. $this->settings['config']['maxchar'] = $max;
  127. return $this;
  128. }
  129. //----------------------------------------------------------------------------------------------------
  130. // validate()
  131. //----------------------------------------------------------------------------------------------------
  132. //
  133. // @param args
  134. //
  135. //----------------------------------------------------------------------------------------------------
  136. public function validate(...$args)
  137. {
  138. $this->settings['validate'] = $args;
  139. return $this;
  140. }
  141. //----------------------------------------------------------------------------------------------------
  142. // secure()
  143. //----------------------------------------------------------------------------------------------------
  144. //
  145. // @param args
  146. //
  147. //----------------------------------------------------------------------------------------------------
  148. public function secure(...$args)
  149. {
  150. $this->settings['secure'] = $args;
  151. return $this;
  152. }
  153. //----------------------------------------------------------------------------------------------------
  154. // pattern()
  155. //----------------------------------------------------------------------------------------------------
  156. //
  157. // @param string $pattern
  158. // @param string $char
  159. //
  160. //----------------------------------------------------------------------------------------------------
  161. public function pattern($pattern = '', $char = '')
  162. {
  163. $this->settings['config']['pattern'] = presuffix($pattern).$char;
  164. return $this;
  165. }
  166. //----------------------------------------------------------------------------------------------------
  167. // phone()
  168. //----------------------------------------------------------------------------------------------------
  169. //
  170. // @param string $design
  171. //
  172. //----------------------------------------------------------------------------------------------------
  173. public function phone($design = '')
  174. {
  175. if( empty($design) )
  176. {
  177. $this->settings['config'][] = 'phone';
  178. }
  179. else
  180. {
  181. $this->settings['config']['phone'] = $design;
  182. }
  183. return $this;
  184. }
  185. //----------------------------------------------------------------------------------------------------
  186. // alpha()
  187. //----------------------------------------------------------------------------------------------------
  188. //
  189. // @param void
  190. //
  191. //----------------------------------------------------------------------------------------------------
  192. public function alpha()
  193. {
  194. $this->settings['config'][] = 'alpha';
  195. return $this;
  196. }
  197. //----------------------------------------------------------------------------------------------------
  198. // alnum()
  199. //----------------------------------------------------------------------------------------------------
  200. //
  201. // @param void
  202. //
  203. //----------------------------------------------------------------------------------------------------
  204. public function alnum()
  205. {
  206. $this->settings['config'][] = 'alnum';
  207. return $this;
  208. }
  209. //----------------------------------------------------------------------------------------------------
  210. // captcha()
  211. //----------------------------------------------------------------------------------------------------
  212. //
  213. // @param string $captcha
  214. //
  215. //----------------------------------------------------------------------------------------------------
  216. public function captcha($captcha = '')
  217. {
  218. $this->settings['config']['captcha'] = $captcha;
  219. return $this;
  220. }
  221. /******************************************************************************************
  222. * IDENDITY *
  223. *******************************************************************************************
  224. | Genel Kullanım: Kimlik numarası kontrolü. |
  225. | |
  226. | Parametreler: Tek parametresi vardır. |
  227. | 1. numeric var @no => Kontrol edilecek kimlik numarası bilgisi. |
  228. | |
  229. | Örnek Kullanım: identity(123213); // Çıktı: true veya false |
  230. | |
  231. ******************************************************************************************/
  232. public function identity($no = '')
  233. {
  234. if( ! is_numeric($no) )
  235. {
  236. return \Errors::set('Error', 'numericParameter', 'no');
  237. }
  238. $numone = ($no[0] + $no[2] + $no[4] + $no[6] + $no[8]) * 7;
  239. $numtwo = $no[1] + $no[3] + $no[5] + $no[7];
  240. $result = $numone - $numtwo;
  241. $tenth = $result%10;
  242. $total = ($no[0] + $no[1] + $no[2] + $no[3] + $no[4] + $no[5] + $no[6] + $no[7] + $no[8] + $no[9]);
  243. $elewenth = $total%10;
  244. if($no[0] == 0)
  245. {
  246. return false;
  247. }
  248. elseif(strlen($no) != 11)
  249. {
  250. return false;
  251. }
  252. elseif($no[9] != $tenth)
  253. {
  254. return false;
  255. }
  256. elseif($no[10] != $elewenth)
  257. {
  258. return false;
  259. }
  260. else
  261. {
  262. return true;
  263. }
  264. }
  265. /******************************************************************************************
  266. * E-MAIL *
  267. *******************************************************************************************
  268. | Genel Kullanım: E-posta kontrolü kontrolü. |
  269. | |
  270. | Parametreler: Tek parametresi vardır. |
  271. | 1. numeric var @data => Kontrol edilecek e-posta bilgisi. |
  272. | |
  273. | Örnek Kullanım: email('bilgi@zntr.net'); // Çıktı: true veya false |
  274. | |
  275. ******************************************************************************************/
  276. public function email($data = '')
  277. {
  278. if( ! is_string($data) )
  279. {
  280. return \Errors::set('Error', 'stringParameter', 'data');
  281. }
  282. if( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $data) )
  283. {
  284. return false;
  285. }
  286. else
  287. {
  288. return true;
  289. }
  290. }
  291. /******************************************************************************************
  292. * URL *
  293. *******************************************************************************************
  294. | Genel Kullanım: URL adres kontrolü kontrolü. |
  295. | |
  296. | Parametreler: Tek parametresi vardır. |
  297. | 1. numeric var @data => Kontrol edilecek url adres bilgisi. |
  298. | |
  299. | Örnek Kullanım: url('zntr.net'); // Çıktı: true veya false |
  300. | |
  301. ******************************************************************************************/
  302. public function url($data = '')
  303. {
  304. if( ! is_string($data) )
  305. {
  306. return \Errors::set('Error', 'stringParameter', 'data');
  307. }
  308. if( ! preg_match('#^(\w+:)?//#i', $data) )
  309. {
  310. return false;
  311. }
  312. else
  313. {
  314. return true;
  315. }
  316. }
  317. /******************************************************************************************
  318. * SPECIAL CHAR *
  319. *******************************************************************************************
  320. | Genel Kullanım: Özel karakter kontrolü kontrolü. |
  321. | |
  322. | Parametreler: Tek parametresi vardır. |
  323. | 1. numeric var @data => Kontrol edilecek metin bilgisi. |
  324. | |
  325. | Örnek Kullanım: specialChar('zntr.net'); // Çıktı: true veya false |
  326. | |
  327. ******************************************************************************************/
  328. public function specialChar($data = '')
  329. {
  330. if( ! is_scalar($data) )
  331. {
  332. return \Errors::set('Error', 'stringParameter', 'data');
  333. }
  334. if( ! preg_match('#[!\'^\#\\\+\$%&\/\(\)\[\]\{\}=\|\-\?:\.\,;_ĞÜŞİÖÇğüşıöç]+#', $data) )
  335. {
  336. return false;
  337. }
  338. else
  339. {
  340. return true;
  341. }
  342. }
  343. /******************************************************************************************
  344. * MAXCHAR *
  345. *******************************************************************************************
  346. | Genel Kullanım: Maksimum karakter kontrolü kontrolü. |
  347. | |
  348. | Parametreler: 2 parametresi vardır. |
  349. | 1. numeric var @data => Kontrol edilecek metin bilgisi. |
  350. | 2. numeric var @char => Maksimum karakter sayısı. |
  351. | |
  352. | Örnek Kullanım: maxchar('zntr.net', 10); // Çıktı: true veya false |
  353. | |
  354. ******************************************************************************************/
  355. public function maxchar($data = '', $char = '')
  356. {
  357. if( ! is_scalar($data) )
  358. {
  359. return \Errors::set('Error', 'stringParameter', 'data');
  360. }
  361. if( ! is_numeric($char) )
  362. {
  363. return \Errors::set('Error', 'numericParameter', 'char');
  364. }
  365. if( strlen($data) <= $char )
  366. {
  367. return true;
  368. }
  369. else
  370. {
  371. return false;
  372. }
  373. }
  374. /******************************************************************************************
  375. * MINCHAR *
  376. *******************************************************************************************
  377. | Genel Kullanım: Minimum karakter kontrolü kontrolü. |
  378. | |
  379. | Parametreler: 2 parametresi vardır. |
  380. | 1. numeric var @data => Kontrol edilecek metin bilgisi. |
  381. | 2. numeric var @char => Minimum karakter sayısı. |
  382. | |
  383. | Örnek Kullanım: minchar('zntr.net', 5); // Çıktı: true veya false |
  384. | |
  385. ******************************************************************************************/
  386. public function minchar($data = '', $char = '')
  387. {
  388. if( ! is_scalar($data) )
  389. {
  390. return \Errors::set('Error', 'stringParameter', 'data');
  391. }
  392. if( ! is_numeric($char) )
  393. {
  394. return \Errors::set('Error', 'numericParameter', 'char');
  395. }
  396. if( strlen($data) >= $char )
  397. {
  398. return true;
  399. }
  400. else
  401. {
  402. return false;
  403. }
  404. }
  405. }