PageRenderTime 27ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/jelix/utils/jDatatype.class.php

https://bitbucket.org/jelix/jelix-trunk/
PHP | 356 lines | 186 code | 31 blank | 139 comment | 47 complexity | d87a4bf8991100effac0aa681d4719cb MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, BSD-3-Clause, JSON, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * @package jelix
  4. * @subpackage utils
  5. * @author Laurent Jouanneau
  6. * @contributor Julien Issler, Hadrien Lanneau
  7. * @copyright 2006-2009 Laurent Jouanneau
  8. * @copyright 2008 Julien Issler, 2011 Hadrien Lanneau
  9. * @link http://www.jelix.org
  10. * @licence http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  11. */
  12. #ifnot ENABLE_PHP_JELIX
  13. /**
  14. * interface for datatypes which can filter value
  15. * @package jelix
  16. * @subpackage utils
  17. * @since 1.1
  18. */
  19. interface jIFilteredDatatype {
  20. /**
  21. * return the value on which filters are applied
  22. * should be call after a call of check() method
  23. */
  24. public function getFilteredValue();
  25. }
  26. #endif
  27. /**
  28. *
  29. * @package jelix
  30. * @subpackage utils
  31. */
  32. abstract class jDatatype {
  33. protected $hasFacets= false;
  34. protected $facets = array();
  35. /**
  36. * call it to add restriction on possible values
  37. * @param string $type
  38. * @param string $value
  39. */
  40. public function addFacet($type,$value=null){
  41. if(in_array($type, $this->facets)){
  42. $this->hasFacets = true;
  43. $this->_addFacet($type,$value);
  44. }
  45. }
  46. /**
  47. * get a restriction value
  48. * @param string $type
  49. * @return mixed the value
  50. * @since 1.0
  51. */
  52. public function getFacet($type){
  53. if(in_array($type, $this->facets)){
  54. return $this->$type;
  55. }
  56. return null;
  57. }
  58. protected function _addFacet($type,$value){
  59. $this->$type = $value;
  60. }
  61. /**
  62. * verify a value : it should correspond to the datatype
  63. * @param string $value
  64. * @return boolean true if the value is ok
  65. */
  66. public function check($value){
  67. return true;
  68. }
  69. }
  70. /**
  71. * Datatype String.
  72. *
  73. * Possible facets are: 'length','minLength','maxLength', 'pattern'
  74. * @package jelix
  75. * @subpackage utils
  76. */
  77. class jDatatypeString extends jDatatype {
  78. protected $length=null;
  79. protected $minLength=null;
  80. protected $maxLength=null;
  81. protected $pattern=null;
  82. protected $facets = array('length','minLength','maxLength', 'pattern');
  83. public function check($value){
  84. if($this->hasFacets){
  85. $len = iconv_strlen(
  86. trim(preg_replace( '@\s+@', ' ', $value)),
  87. $GLOBALS['gJConfig']->charset
  88. );
  89. if($this->length !== null && $len != $this->length)
  90. return false;
  91. if($this->minLength !== null && $len < $this->minLength)
  92. return false;
  93. if($this->maxLength !== null && $len > $this->maxLength)
  94. return false;
  95. if($this->pattern !== null && !preg_match($this->pattern,$value))
  96. return false;
  97. }
  98. return true;
  99. }
  100. }
  101. /**
  102. * Datatype HTML String.
  103. *
  104. * Possible facets are: 'length','minLength','maxLength'
  105. * @package jelix
  106. * @subpackage utils
  107. * @since 1.1
  108. */
  109. class jDatatypeHtml extends jDatatype implements jIFilteredDatatype {
  110. protected $length=null;
  111. protected $minLength=null;
  112. protected $maxLength=null;
  113. protected $facets = array('length','minLength','maxLength');
  114. public $outputXhtml = false;
  115. public $fromWysiwyg = false;
  116. protected $newValue;
  117. public function __construct($aOutputXhtml = false, $fromWysiwyg = false) {
  118. $this->outputXhtml = $aOutputXhtml;
  119. $this->fromWysiwyg = $fromWysiwyg;
  120. }
  121. public function check($value){
  122. if($this->hasFacets){
  123. if ($this->fromWysiwyg)
  124. $len = iconv_strlen(strip_tags($value,'<img><img/><object><embed><video><video/><svg>'), $GLOBALS['gJConfig']->charset);
  125. else
  126. $len = iconv_strlen($value, $GLOBALS['gJConfig']->charset);
  127. if($this->length !== null && $len != $this->length)
  128. return false;
  129. if($this->minLength !== null && $len < $this->minLength)
  130. return false;
  131. if($this->maxLength !== null && $len > $this->maxLength)
  132. return false;
  133. }
  134. $this->newValue = jFilter::cleanHtml($value, $this->outputXhtml);
  135. return is_string($this->newValue);
  136. }
  137. public function getFilteredValue() {
  138. return $this->newValue;
  139. }
  140. }
  141. /**
  142. * Datatype Bool?各n
  143. * @package jelix
  144. * @subpackage utils
  145. */
  146. class jDatatypeBoolean extends jDatatype {
  147. public function check($value) { return jFilter::isBool($value); }
  148. }
  149. /**
  150. * Datatype Decimal
  151. *
  152. * Possible facets are: 'maxValue', 'minValue'
  153. * @package jelix
  154. * @subpackage utils
  155. */
  156. class jDatatypeDecimal extends jDatatype {
  157. // xxxx.yyyyy
  158. protected $maxValue=null;
  159. protected $minValue=null;
  160. protected $facets = array('maxValue', 'minValue');
  161. public function check($value) { return jFilter::isFloat($value, $this->minValue, $this->maxValue); }
  162. protected function _addFacet($type,$value){
  163. if($type == 'maxValue' || $type == 'minValue'){
  164. $this->$type = floatval($value);
  165. }
  166. }
  167. }
  168. /**
  169. * Datatype Integer
  170. * @package jelix
  171. * @subpackage utils
  172. */
  173. class jDatatypeInteger extends jDatatypeDecimal {
  174. public function check($value) { return jFilter::isInt($value, $this->minValue, $this->maxValue); }
  175. protected function _addFacet($type,$value){
  176. if($type == 'maxValue' || $type == 'minValue'){
  177. $this->$type = intval($value);
  178. }
  179. }
  180. }
  181. /**
  182. * Datatype Hexa
  183. * @package jelix
  184. * @subpackage utils
  185. */
  186. class jDatatypeHexadecimal extends jDatatypeDecimal {
  187. public function check($value) {
  188. if(substr($value,0,2) != '0x') $value='0x'.$value;
  189. return jFilter::isHexInt($value, $this->minValue, $this->maxValue);
  190. }
  191. protected function _addFacet($type,$value){
  192. if($type == 'maxValue' || $type == 'minValue'){
  193. $this->$type = intval($value,16);
  194. }
  195. }
  196. }
  197. /**
  198. * Datatype datetime
  199. *
  200. * Possible facets are: 'maxValue', 'minValue'
  201. * @package jelix
  202. * @subpackage utils
  203. */
  204. class jDatatypeDateTime extends jDatatype {
  205. protected $facets = array('maxValue', 'minValue');
  206. protected $maxValue;
  207. protected $minValue;
  208. private $dt;
  209. protected $format=21;
  210. protected $_date_format = 'Y-m-d H:i:s';
  211. public function check($value) {
  212. $this->dt = new jDateTime();
  213. if(!$this->dt->setFromString($value,$this->format)) return false;
  214. if($this->maxValue !== null){
  215. if($this->dt->compareTo($this->maxValue) == 1) return false;
  216. }
  217. if($this->minValue !== null){
  218. if($this->dt->compareTo($this->minValue) == -1) return false;
  219. }
  220. return true;
  221. }
  222. protected function _addFacet($type,$value){
  223. if($type == 'maxValue' || $type == 'minValue'){
  224. if(!preg_match('#^\d{4}-\d{2}-\d{2} (\d{2}:\d{2}(:\d{2})?)?$#',$value))
  225. $value = date($this->_date_format,strtotime($value));
  226. $this->$type = new jDateTime();
  227. $this->$type->setFromString($value,$this->format);
  228. }
  229. else
  230. parent::_addFacet($type,$value);
  231. }
  232. /**
  233. * @since 1.0
  234. */
  235. public function getFormat() { return $this->format; }
  236. }
  237. /**
  238. * Datatype time
  239. * @package jelix
  240. * @subpackage utils
  241. */
  242. class jDatatypeTime extends jDatatypeDateTime {
  243. protected $format=22;
  244. }
  245. /**
  246. * Datatype date
  247. * @package jelix
  248. * @subpackage utils
  249. */
  250. class jDatatypeDate extends jDatatypeDateTime {
  251. protected $format=20;
  252. protected $_date_format = 'Y-m-d';
  253. }
  254. /**
  255. * Datatype localedatetime
  256. * @package jelix
  257. * @subpackage utils
  258. */
  259. class jDatatypeLocaleDateTime extends jDatatypeDateTime {
  260. protected $format=11;
  261. }
  262. /**
  263. * Datatype localedate
  264. * @package jelix
  265. * @subpackage utils
  266. */
  267. class jDatatypeLocaleDate extends jDatatypeDateTime {
  268. protected $format=10;
  269. }
  270. /**
  271. * Datatype localetime
  272. * @package jelix
  273. * @subpackage utils
  274. */
  275. class jDatatypeLocaleTime extends jDatatypeDateTime {
  276. protected $format=12;
  277. }
  278. /**
  279. * Datatype url
  280. *
  281. * Possible facets are: 'schemeRequired','hostRequired','pathRequired', 'queryRequired'.
  282. * all are booleans.
  283. * @package jelix
  284. * @subpackage utils
  285. */
  286. class jDatatypeUrl extends jDatatype {
  287. protected $schemeRequired=true;
  288. protected $hostRequired=true;
  289. protected $pathRequired=null;
  290. protected $queryRequired=null;
  291. protected $facets = array('schemeRequired','hostRequired','pathRequired', 'queryRequired');
  292. public function check($value){
  293. return jFilter::isUrl($value, $this->schemeRequired, $this->hostRequired, $this->pathRequired, $this->queryRequired);
  294. }
  295. }
  296. /**
  297. * Datatype ipv4
  298. * @package jelix
  299. * @subpackage utils
  300. */
  301. class jDatatypeIPv4 extends jDatatype {
  302. public function check($value){
  303. return jFilter::isIPv4($value);
  304. }
  305. }
  306. /**
  307. * Datatype ipv6
  308. * @package jelix
  309. * @subpackage utils
  310. */
  311. class jDatatypeIPv6 extends jDatatype {
  312. public function check($value){
  313. return jFilter::isIPv6($value);
  314. }
  315. }
  316. /**
  317. * Datatype mail
  318. * @package jelix
  319. * @subpackage utils
  320. */
  321. class jDatatypeEmail extends jDatatype {
  322. public function check($value){
  323. return jFilter::isEmail($value);
  324. }
  325. }