PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/KnowledgeBase/Base/Validator.php

https://github.com/zikula-modules/KnowledgeBase
PHP | 399 lines | 149 code | 31 blank | 219 comment | 17 complexity | 816758d0f8f0efc087d28d6843c20708 MD5 | raw file
  1. <?php
  2. /**
  3. * KnowledgeBase.
  4. *
  5. * @copyright Axel Guckelsberger
  6. * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
  7. * @package KnowledgeBase
  8. * @author Axel Guckelsberger <info@guite.de>.
  9. * @link https://guite.de
  10. * @link http://zikula.org
  11. * @version Generated by ModuleStudio 0.5.4 (http://modulestudio.de) at Wed Jan 04 20:13:48 CET 2012.
  12. */
  13. /**
  14. * Validator class for encapsulating common entity validation methods.
  15. *
  16. * This is the base validation class with general checks.
  17. */
  18. abstract class KnowledgeBase_Base_Validator extends Zikula_AbstractBase
  19. {
  20. /**
  21. * @var Zikula_EntityAccess The entity instance which is treated by this validator.
  22. */
  23. protected $entity = null;
  24. /**
  25. * Constructor.
  26. *
  27. * @param Zikula_EntityAccess $entity The entity to be validated.
  28. */
  29. public function __construct(Zikula_EntityAccess $entity)
  30. {
  31. $this->entity = $entity;
  32. }
  33. /**
  34. * Checks if field value is a valid boolean.
  35. *
  36. * @param string $fieldName The name of the property to be checked
  37. * @return boolean result of this check
  38. */
  39. public function isValidBoolean($fieldName)
  40. {
  41. return (is_bool($this->entity[$fieldName]));
  42. }
  43. /**
  44. * Checks if field value is a valid number.
  45. *
  46. * @param string $fieldName The name of the property to be checked
  47. * @return boolean result of this check
  48. */
  49. public function isValidNumber($fieldName)
  50. {
  51. return (is_numeric($this->entity[$fieldName]));
  52. }
  53. /**
  54. * Checks if field value is a valid integer.
  55. *
  56. * @param string $fieldName The name of the property to be checked
  57. * @return boolean result of this check
  58. */
  59. public function isValidInteger($fieldName)
  60. {
  61. $val = $this->entity[$fieldName];
  62. return ($val == intval($val));
  63. }
  64. /**
  65. * Checks if integer field value is not lower than a given value.
  66. *
  67. * @param string $fieldName The name of the property to be checked
  68. * @param int $value The maximum allowed value
  69. * @return boolean result of this check
  70. */
  71. public function isIntegerNotLowerThan($fieldName, $value)
  72. {
  73. return ($this->isValidInteger($fieldName) && $this->entity[$fieldName] >= $value);
  74. }
  75. /**
  76. * Checks if integer field value is not higher than a given value.
  77. *
  78. * @param string $fieldName The name of the property to be checked
  79. * @param int $value The maximum allowed value
  80. * @return boolean result of this check
  81. */
  82. public function isIntegerNotHigherThan($fieldName, $value)
  83. {
  84. return ($this->isValidInteger($fieldName) && $this->entity[$fieldName] <= $value);
  85. }
  86. /**
  87. * Checks if field value is a valid user id.
  88. *
  89. * @param string $fieldName The name of the property to be checked
  90. * @return boolean result of this check
  91. */
  92. public function isValidUser($fieldName)
  93. {
  94. if (!$this->isValidInteger($fieldName)) {
  95. return false;
  96. }
  97. $uname = UserUtil::getVar('uname', $this->entity[$fieldName]);
  98. return (!is_null($uname) && !empty($uname));
  99. }
  100. /**
  101. * Checks if numeric field value has a value other than 0.
  102. *
  103. * @param string $fieldName The name of the property to be checked
  104. * @return boolean result of this check
  105. */
  106. public function isNumberNotEmpty($fieldName)
  107. {
  108. return $this->entity[$fieldName] != 0;
  109. }
  110. /**
  111. * Checks if string field value has a value other than ''.
  112. *
  113. * @param string $fieldName The name of the property to be checked
  114. * @return boolean result of this check
  115. */
  116. public function isStringNotEmpty($fieldName)
  117. {
  118. return $this->entity[$fieldName] != '';
  119. }
  120. /**
  121. * Checks if numeric field value has a given minimum field length
  122. *
  123. * @param string $fieldName The name of the property to be checked
  124. * @param int $length The minimum length
  125. * @return boolean result of this check
  126. */
  127. public function isNumberNotShorterThan($fieldName, $length)
  128. {
  129. $minValue = pow(10, $length-1);
  130. return ($this->isValidNumber($fieldName) && $this->entity[$fieldName] > $minValue);
  131. }
  132. /**
  133. * Checks if numeric field value does fit into given field length.
  134. *
  135. * @param string $fieldName The name of the property to be checked
  136. * @param int $length The maximum allowed length
  137. * @return boolean result of this check
  138. */
  139. public function isNumberNotLongerThan($fieldName, $length)
  140. {
  141. $maxValue = pow(10, $length);
  142. return ($this->isValidNumber($fieldName) && $this->entity[$fieldName] < $maxValue);
  143. }
  144. /**
  145. * Checks if string field value has a given minimum field length.
  146. *
  147. * @param string $fieldName The name of the property to be checked
  148. * @param int $length The minimum length
  149. * @return boolean result of this check
  150. */
  151. public function isStringNotShorterThan($fieldName, $length)
  152. {
  153. return (strlen($this->entity[$fieldName]) >= $length);
  154. }
  155. /**
  156. * Checks if string field value does fit into given field length.
  157. *
  158. * @param string $fieldName The name of the property to be checked
  159. * @param int $length The maximum allowed length
  160. * @return boolean result of this check
  161. */
  162. public function isStringNotLongerThan($fieldName, $length)
  163. {
  164. return (strlen($this->entity[$fieldName]) <= $length);
  165. }
  166. /**
  167. * Checks if string field value does conform to given fixed field length.
  168. *
  169. * @param string $fieldName The name of the property to be checked
  170. * @param int $length The fixed length
  171. * @return boolean result of this check
  172. */
  173. public function isStringWithFixedLength($fieldName, $length)
  174. {
  175. return (strlen($this->entity[$fieldName]) == $length);
  176. }
  177. /**
  178. * Checks if string field value does not contain a given string.
  179. *
  180. * @param string $fieldName The name of the property to be checked
  181. * @param string $keyword The char or string to search for
  182. * @param boolean $caseSensitive Whether the search should be case sensitive or not (default false)
  183. * @return boolean result of this check
  184. */
  185. public function isStringNotContaining($fieldName, $keyword, $caseSensitive = false)
  186. {
  187. if ($caseSensitive === true) {
  188. return (strstr($this->entity[$fieldName], $keyword) === false);
  189. }
  190. return (stristr($this->entity[$fieldName], $keyword) === false);
  191. }
  192. /**
  193. * Checks if string field value conforms to a given regular expression.
  194. *
  195. * @param string $fieldName The name of the property to be checked
  196. * @param string $expression Regular expression string
  197. * @return boolean result of this check
  198. */
  199. public function isValidRegExp($fieldName, $expression)
  200. {
  201. return preg_match($expression, $this->entity[$fieldName]);
  202. }
  203. /**
  204. * Checks if string field value is a valid language code.
  205. *
  206. * @param string $fieldName The name of the property to be checked
  207. * @param boolean $onlyInstalled Whether to accept only installed languages (default false)
  208. * @return boolean result of this check
  209. */
  210. public function isValidLanguage($fieldName, $onlyInstalled = false)
  211. {
  212. $languageMap = ZLanguage::languageMap();
  213. $result = in_array($this->entity[$fieldName], array_keys($languageMap));
  214. if (!$result || !$onlyInstalled) {
  215. return $result;
  216. }
  217. $available = ZLanguage::getInstalledLanguages();
  218. return in_array($this->entity[$fieldName], $available);
  219. }
  220. /**
  221. * Checks if string field value is a valid country code.
  222. *
  223. * @param string $fieldName The name of the property to be checked
  224. * @return boolean result of this check
  225. */
  226. public function isValidCountry($fieldName)
  227. {
  228. $countryMap = ZLanguage::countryMap();
  229. return in_array($this->entity[$fieldName], array_keys($countryMap));
  230. }
  231. /**
  232. * Checks if string field value is a valid html colour.
  233. *
  234. * @param string $fieldName The name of the property to be checked
  235. * @return boolean result of this check
  236. */
  237. public function isValidHtmlColour($fieldName)
  238. {
  239. $regex = '/^#?(([a-fA-F0-9]{3}){1,2})$/';
  240. return preg_match($regex, $this->entity[$fieldName]);
  241. }
  242. /**
  243. * Checks if field value is a valid email address.
  244. *
  245. * @param string $fieldName The name of the property to be checked
  246. * @return boolean result of this check
  247. */
  248. public function isValidEmail($fieldName)
  249. {
  250. return filter_var($this->entity[$fieldName], FILTER_VALIDATE_EMAIL);
  251. }
  252. /**
  253. * Checks if field value is a valid url.
  254. *
  255. * @param string $fieldName The name of the property to be checked
  256. * @return boolean result of this check
  257. */
  258. public function isValidUrl($fieldName)
  259. {
  260. return filter_var($this->entity[$fieldName], FILTER_VALIDATE_URL);
  261. }
  262. /**
  263. * Checks if field value is a valid DateTime instance.
  264. *
  265. * @param string $fieldName The name of the property to be checked
  266. * @return boolean result of this check
  267. */
  268. public function isValidDateTime($fieldName)
  269. {
  270. return ($this->entity[$fieldName] instanceof DateTime);
  271. }
  272. /**
  273. * Checks if field value has a value in the past.
  274. *
  275. * @param string $fieldName The name of the property to be checked
  276. * @param string $format The date format used for comparison
  277. * @param boolean $mandatory Whether the property is mandatory or not.
  278. * @return boolean result of this check
  279. */
  280. protected function isDateTimeValueInPast($fieldName, $format, $mandatory = true)
  281. {
  282. if ($mandatory === false) {
  283. return true;
  284. }
  285. return ($this->isValidDateTime($fieldName) && $this->entity[$fieldName]->format($format) < date($format));
  286. }
  287. /**
  288. * Checks if field value has a value in the future.
  289. *
  290. * @param string $fieldName The name of the property to be checked
  291. * @param string $format The date format used for comparison
  292. * @param boolean $mandatory Whether the property is mandatory or not.
  293. * @return boolean result of this check
  294. */
  295. protected function isDateTimeValueInFuture($fieldName, $format, $mandatory = true)
  296. {
  297. if ($mandatory === false) {
  298. return true;
  299. }
  300. return ($this->isValidDateTime($fieldName) && $this->entity[$fieldName]->format($format) > date($format));
  301. }
  302. /**
  303. * Checks if field value is a datetime in the past.
  304. *
  305. * @param string $fieldName The name of the property to be checked
  306. * @param boolean $mandatory Whether the property is mandatory or not.
  307. * @return boolean result of this check
  308. */
  309. public function isDateTimeInPast($fieldName, $mandatory = true)
  310. {
  311. return $this->isDateTimeValueInPast($fieldName, 'U', $mandatory);
  312. }
  313. /**
  314. * Checks if field value is a datetime in the future.
  315. *
  316. * @param string $fieldName The name of the property to be checked
  317. * @param boolean $mandatory Whether the property is mandatory or not.
  318. * @return boolean result of this check
  319. */
  320. public function isDateTimeInFuture($fieldName, $mandatory = true)
  321. {
  322. return $this->isDateTimeValueInFuture($fieldName, 'U', $mandatory);
  323. }
  324. /**
  325. * Checks if field value is a date in the past.
  326. *
  327. * @param string $fieldName The name of the property to be checked
  328. * @param boolean $mandatory Whether the property is mandatory or not.
  329. * @return boolean result of this check
  330. */
  331. public function isDateInPast($fieldName, $mandatory = true)
  332. {
  333. return $this->isDateTimeValueInPast($fieldName, 'Ymd', $mandatory);
  334. }
  335. /**
  336. * Checks if field value is a date in the future.
  337. *
  338. * @param string $fieldName The name of the property to be checked
  339. * @param boolean $mandatory Whether the property is mandatory or not.
  340. * @return boolean result of this check
  341. */
  342. public function isDateInFuture($fieldName, $mandatory = true)
  343. {
  344. return $this->isDateTimeValueInFuture($fieldName, 'Ymd', $mandatory);
  345. }
  346. /**
  347. * Checks if field value is a time in the past.
  348. *
  349. * @param string $fieldName The name of the property to be checked
  350. * @param boolean $mandatory Whether the property is mandatory or not.
  351. * @return boolean result of this check
  352. */
  353. public function isTimeInPast($fieldName, $mandatory = true)
  354. {
  355. return $this->isDateTimeValueInPast($fieldName, 'His', $mandatory);
  356. }
  357. /**
  358. * Checks if field value is a time in the future.
  359. *
  360. * @param string $fieldName The name of the property to be checked
  361. * @param boolean $mandatory Whether the property is mandatory or not.
  362. * @return boolean result of this check
  363. */
  364. public function isTimeInFuture($fieldName, $mandatory = true)
  365. {
  366. return $this->isDateTimeValueInFuture($fieldName, 'His', $mandatory);
  367. }
  368. }