Source for file jDatatype.class.php

Documentation is available at jDatatype.class.php

  1. <?php
  2. /**
  3. @package     jelix
  4. @subpackage  utils
  5. @author      Laurent Jouanneau
  6. @contributor Julien Issler, Hadrien Lanneau
  7. @copyright   2006-2012 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. /**
  13.  * interface for datatypes which can filter value
  14.  * @package     jelix
  15.  * @subpackage  utils
  16.  * @since 1.1
  17.  */
  18. interface jIFilteredDatatype {
  19.     /**
  20.      * return the value on which filters are applied
  21.      * should be call after a call of check() method
  22.      */
  23.     public function getFilteredValue();
  24. }
  25. /**
  26.  *
  27.  * @package     jelix
  28.  * @subpackage  utils
  29.  */
  30. abstract class jDatatype {
  31.  
  32.     protected $hasFacetsfalse;
  33.     protected $facets = array();
  34.  
  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.     /**
  48.     * get a restriction value
  49.     * @param string $type 
  50.     * @return mixed  the value
  51.     * @since 1.0
  52.     */
  53.     public function getFacet($type){
  54.         if(in_array($type$this->facets)){
  55.             return $this->$type;
  56.         }
  57.         return null;
  58.     }
  59.  
  60.     protected function _addFacet($type,$value){
  61.         $this->$type $value;
  62.     }
  63.  
  64.     /**
  65.     * verify a value : it should correspond to the datatype
  66.     * @param string   $value 
  67.     * @return boolean true if the value is ok
  68.     */
  69.     public function check($value){
  70.         return true;
  71.     }
  72.  
  73.     /**
  74.      * says if the value can contain only whitespaces
  75.      * @return boolean 
  76.      * @since 1.2.7
  77.      */
  78.     public function allowWhitespace({
  79.         return false;
  80.     }
  81. }
  82.  
  83. /**
  84.  * Datatype String.
  85.  *
  86.  * Possible facets are: 'length','minLength','maxLength', 'pattern'
  87.  * @package     jelix
  88.  * @subpackage  utils
  89.  */
  90. class jDatatypeString extends jDatatype {
  91.     protected $length=null;
  92.     protected $minLength=null;
  93.     protected $maxLength=null;
  94.     protected $pattern=null;
  95.     protected $facets = array('length','minLength','maxLength''pattern');
  96.  
  97.     public function check($value){
  98.         if($this->hasFacets){
  99.             $len iconv_strlen(
  100.                 trim(preg_replace'@\s+@'' '$value)),
  101.                 jApp::config()->charset
  102.             );
  103.             if($this->length !== null && $len != $this->length)
  104.                 return false;
  105.             if($this->minLength !== null && $len $this->minLength)
  106.                 return false;
  107.             if($this->maxLength !== null && $len $this->maxLength)
  108.                 return false;
  109.             if($this->pattern !== null && !preg_match($this->pattern,$value))
  110.                 return false;
  111.         }
  112.         return true;
  113.     }
  114.  
  115.     public function allowWhitespace({
  116.         return true;
  117.     }
  118. }
  119.  
  120. /**
  121.  * Datatype HTML String.
  122.  *
  123.  * Possible facets are: 'length','minLength','maxLength'
  124.  * @package     jelix
  125.  * @subpackage  utils
  126.  * @since 1.1
  127.  */
  128. class jDatatypeHtml extends jDatatype implements jIFilteredDatatype {
  129.     protected $length=null;
  130.     protected $minLength=null;
  131.     protected $maxLength=null;
  132.     protected $facets = array('length','minLength','maxLength');
  133.     public $outputXhtml = false;
  134.     public $fromWysiwyg = false;
  135.  
  136.     protected $newValue;
  137.  
  138.     public function __construct($aOutputXhtml false$fromWysiwyg false{
  139.         $this->outputXhtml = $aOutputXhtml;
  140.         $this->fromWysiwyg = $fromWysiwyg;
  141.     }
  142.  
  143.     public function check($value){
  144.         if($this->hasFacets){
  145.             if ($this->fromWysiwyg)
  146.                 $len iconv_strlen(strip_tags($value,'<img><img/><object><embed><video><video/><svg>')jApp::config()->charset);
  147.             else
  148.                 $len iconv_strlen($valuejApp::config()->charset);
  149.             if($this->length !== null && $len != $this->length)
  150.                 return false;
  151.             if($this->minLength !== null && $len $this->minLength)
  152.                 return false;
  153.             if($this->maxLength !== null && $len $this->maxLength)
  154.                 return false;
  155.         }
  156.         $this->newValue = jFilter::cleanHtml($value$this->outputXhtml);
  157.         return is_string($this->newValue);
  158.     }
  159.  
  160.     public function getFilteredValue({
  161.         return $this->newValue;
  162.     }
  163.  
  164.     public function allowWhitespace({
  165.         return true;
  166.     }
  167. }
  168.  
  169. /**
  170.  * Datatype BoolĂ©en
  171.  * @package     jelix
  172.  * @subpackage  utils
  173.  */
  174. class jDatatypeBoolean extends jDatatype {
  175.     public function check($valuereturn jFilter::isBool($value)}
  176. }
  177.  
  178. /**
  179.  * Datatype Decimal
  180.  *
  181.  * Possible facets are: 'maxValue', 'minValue'
  182.  * @package     jelix
  183.  * @subpackage  utils
  184.  */
  185. class jDatatypeDecimal extends jDatatype {
  186.     // xxxx.yyyyy
  187.     protected $maxValue=null;
  188.     protected $minValue=null;
  189.     protected $facets = array('maxValue''minValue');
  190.     public function check($valuereturn jFilter::isFloat($value$this->minValue$this->maxValue)}
  191.     protected function _addFacet($type,$value){
  192.         if($type == 'maxValue' || $type == 'minValue'){
  193.             $this->$type floatval($value);
  194.         }
  195.     }
  196. }
  197.  
  198. /**
  199.  * Datatype Integer
  200.  * @package     jelix
  201.  * @subpackage  utils
  202.  */
  203. class jDatatypeInteger extends jDatatypeDecimal {
  204.     public function check($valuereturn jFilter::isInt($value$this->minValue$this->maxValue)}
  205.     protected function _addFacet($type,$value){
  206.         if($type == 'maxValue' || $type == 'minValue'){
  207.             $this->$type intval($value);
  208.         }
  209.     }
  210. }
  211.  
  212.  
  213. /**
  214.  * Datatype Hexa
  215.  * @package     jelix
  216.  * @subpackage  utils
  217.  */
  218. class jDatatypeHexadecimal extends jDatatypeDecimal {
  219.     public function check($value{
  220.         if(substr($value,0,2!= '0x'$value='0x'.$value;
  221.         return jFilter::isHexInt($value$this->minValue$this->maxValue);
  222.     }
  223.     protected function _addFacet($type,$value){
  224.         if($type == 'maxValue' || $type == 'minValue'){
  225.             $this->$type intval($value,16);
  226.         }
  227.     }
  228. }
  229.  
  230.  
  231. /**
  232.  * Datatype datetime
  233.  *
  234.  * Possible facets are: 'maxValue', 'minValue'
  235.  * @package     jelix
  236.  * @subpackage  utils
  237.  */
  238. class jDatatypeDateTime extends jDatatype {
  239.     protected $facets = array('maxValue''minValue');
  240.     protected $maxValue;
  241.     protected $minValue;
  242.     private $dt;
  243.     protected $format=21;
  244.     protected $_date_format = 'Y-m-d H:i:s';
  245.     public function check($value{
  246.         $this->dt new jDateTime();
  247.         if(!$this->dt->setFromString($value,$this->format)) return false;
  248.         if($this->maxValue !== null){
  249.             if($this->dt->compareTo($this->maxValue== 1return false;
  250.         }
  251.         if($this->minValue !== null){
  252.             if($this->dt->compareTo($this->minValue== -1return false;
  253.         }
  254.         return true;
  255.     }
  256.  
  257.     protected function _addFacet($type,$value){
  258.         if($type == 'maxValue' || $type == 'minValue'){
  259.             if(!preg_match('#^\d{4}-\d{2}-\d{2}( \d{2}:\d{2}(:\d{2})?)?$#',$value))
  260.                 $value date($this->_date_format,strtotime($value));
  261.             $this->$type new jDateTime();
  262.             $this->$type->setFromString($value,$this->format);
  263.         }
  264.         else
  265.             parent::_addFacet($type,$value);
  266.     }
  267.     /**
  268.      * @since 1.0
  269.      */
  270.     public function getFormat(return $this->format}
  271. }
  272.  
  273. /**
  274.  * Datatype time
  275.  * @package     jelix
  276.  * @subpackage  utils
  277.  */
  278. class jDatatypeTime extends jDatatypeDateTime {
  279.     protected $format=22;
  280. }
  281. /**
  282.  * Datatype date
  283.  * @package     jelix
  284.  * @subpackage  utils
  285.  */
  286. class jDatatypeDate extends jDatatypeDateTime {
  287.     protected $format=20;
  288.     protected $_date_format = 'Y-m-d';
  289. }
  290.  
  291. /**
  292.  * Datatype localedatetime
  293.  * @package     jelix
  294.  * @subpackage  utils
  295.  */
  296. class jDatatypeLocaleDateTime extends jDatatypeDateTime {
  297.     protected $format=11;
  298. }
  299.  
  300. /**
  301.  * Datatype localedate
  302.  * @package     jelix
  303.  * @subpackage  utils
  304.  */
  305. class jDatatypeLocaleDate extends jDatatypeDateTime {
  306.     protected $format=10;
  307. }
  308.  
  309. /**
  310.  * Datatype localetime
  311.  * @package     jelix
  312.  * @subpackage  utils
  313.  */
  314. class jDatatypeLocaleTime extends jDatatypeDateTime {
  315.     protected $format=12;
  316. }
  317.  
  318.  
  319. /**
  320.  * Datatype localetime
  321.  * @package     jelix
  322.  * @subpackage  utils
  323.  * @author dhughuet, time short
  324.  */
  325. class jDatatypeLocaleTimeShort extends jDatatypeDateTime {
  326.     protected $format=14;
  327. }
  328.  
  329.  
  330. /**
  331.  * Datatype url
  332.  *
  333.  * Possible facets are: 'schemeRequired','hostRequired','pathRequired', 'queryRequired'.
  334.  * all are booleans.
  335.  * @package     jelix
  336.  * @subpackage  utils
  337.  */
  338. class jDatatypeUrl extends jDatatype {
  339.     protected $schemeRequired=true;
  340.     protected $hostRequired=true;
  341.     protected $pathRequired=null;
  342.     protected $queryRequired=null;
  343.  
  344.     protected $facets = array('schemeRequired','hostRequired','pathRequired''queryRequired');
  345.  
  346.     public function check($value){
  347.         return jFilter::isUrl($value$this->schemeRequired$this->hostRequired$this->pathRequired$this->queryRequired);
  348.     }
  349. }
  350.  
  351. /**
  352.  * Datatype ipv4
  353.  * @package     jelix
  354.  * @subpackage  utils
  355.  */
  356. class jDatatypeIPv4 extends jDatatype {
  357.     public function check($value){
  358.         return jFilter::isIPv4($value);
  359.     }
  360. }
  361.  
  362. /**
  363.  * Datatype ipv6
  364.  * @package     jelix
  365.  * @subpackage  utils
  366.  */
  367. class jDatatypeIPv6 extends jDatatype {
  368.     public function check($value){
  369.         return jFilter::isIPv6($value);
  370.     }
  371. }
  372.  
  373. /**
  374.  * Datatype mail
  375.  * @package     jelix
  376.  * @subpackage  utils
  377.  */
  378. class jDatatypeEmail extends jDatatype {
  379.     public function check($value){
  380.         return jFilter::isEmail($value);
  381.     }
  382. }

Documentation generated on Wed, 04 Jan 2017 22:53:30 +0100 by phpDocumentor 1.4.3