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
  7. @copyright   2006 Laurent Jouanneau
  8. @link        http://www.jelix.org
  9. @licence     http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public Licence, see LICENCE file
  10. */
  11.  
  12. /**
  13.  *
  14.  * @package     jelix
  15.  * @subpackage  utils
  16.  */
  17. abstract class jDatatype {
  18.  
  19.     protected $hasFacetsfalse;
  20.     protected $facets = array();
  21.  
  22.     function __construct(){
  23.     }
  24.  
  25.     /**
  26.     * call it to add restriction on possible values
  27.     * @param string $type 
  28.     * @param string $value 
  29.     */
  30.     public function addFacet($type,$value=null){
  31.  
  32.         if(in_array($type$this->facets)){
  33.             $this->hasFacets = true;
  34.             $this->_addFacet($type,$value);
  35.         }
  36.     }
  37.  
  38.  
  39.     /**
  40.     * get a restriction value
  41.     * @param string $type 
  42.     * @return mixed  the value
  43.     * @since 1.0
  44.     */
  45.     public function getFacet($type){
  46.         if(in_array($type$this->facets)){
  47.             return $this->$type;
  48.         }
  49.         return null;
  50.     }
  51.  
  52.     protected function _addFacet($type,$value){
  53.         $this->$type $value;
  54.     }
  55.  
  56.     /**
  57.     * verify a value : it should correspond to the datatype
  58.     * @param string   $value 
  59.     * @return boolean true if the value is ok
  60.     */
  61.     public function check($value){
  62.         return true;
  63.     }
  64. }
  65.  
  66. /**
  67.  * Datatype String.
  68.  *
  69.  * Possible facets are: 'length','minLength','maxLength', 'pattern'
  70.  * @package     jelix
  71.  * @subpackage  utils
  72.  */
  73. class jDatatypeString extends jDatatype {
  74.     protected $length=null;
  75.     protected $minLength=null;
  76.     protected $maxLength=null;
  77.     protected $pattern=null;
  78.     protected $facets = array('length','minLength','maxLength''pattern');
  79.  
  80.     public function check($value){
  81.         if($this->hasFacets){
  82.             $len iconv_strlen($value$GLOBALS['gJConfig']->charset);
  83.             if($this->length !== null && $len != $this->length)
  84.                 return false;
  85.             if($this->minLength !== null && $len $this->minLength)
  86.                 return false;
  87.             if($this->maxLength !== null && $len $this->maxLength)
  88.                 return false;
  89.             if($this->pattern !== null && !preg_match($this->pattern,$value))
  90.                 return false;
  91.         }
  92.         return true;
  93.     }
  94. }
  95.  
  96. /**
  97.  * Datatype BoolĂ©en
  98.  * @package     jelix
  99.  * @subpackage  utils
  100.  */
  101. class jDatatypeBoolean extends jDatatype {
  102.     public function check($valuereturn jFilter::isBool($value)}
  103. }
  104.  
  105. /**
  106.  * Datatype Decimal
  107.  *
  108.  * Possible facets are: 'maxValue', 'minValue'
  109.  * @package     jelix
  110.  * @subpackage  utils
  111.  */
  112. class jDatatypeDecimal extends jDatatype {
  113.     // xxxx.yyyyy
  114.     protected $maxValue=null;
  115.     protected $minValue=null;
  116.     protected $facets = array('maxValue''minValue');
  117.     public function check($valuereturn jFilter::isFloat($value$this->minValue$this->maxValue)}
  118.     protected function _addFacet($type,$value){
  119.         if($type == 'maxValue' || $type == 'minValue'){
  120.             $this->$type floatval($value);
  121.         }
  122.     }
  123. }
  124.  
  125. /**
  126.  * Datatype Integer
  127.  * @package     jelix
  128.  * @subpackage  utils
  129.  */
  130. class jDatatypeInteger extends jDatatypeDecimal {
  131.     public function check($valuereturn jFilter::isInt($value$this->minValue$this->maxValue)}
  132.     protected function _addFacet($type,$value){
  133.         if($type == 'maxValue' || $type == 'minValue'){
  134.             $this->$type intval($value);
  135.         }
  136.     }
  137. }
  138.  
  139.  
  140. /**
  141.  * Datatype Hexa
  142.  * @package     jelix
  143.  * @subpackage  utils
  144.  */
  145. class jDatatypeHexadecimal extends jDatatypeDecimal {
  146.     public function check($value{
  147.         if(substr($value,0,2!= '0x'$value='0x'.$value;
  148.         return jFilter::isHexInt($value$this->minValue$this->maxValue);
  149.     }
  150.     protected function _addFacet($type,$value){
  151.         if($type == 'maxValue' || $type == 'minValue'){
  152.             $this->$type intval($value,16);
  153.         }
  154.     }
  155. }
  156.  
  157.  
  158. /**
  159.  * Datatype datetime
  160.  *
  161.  * Possible facets are: 'maxValue', 'minValue'
  162.  * @package     jelix
  163.  * @subpackage  utils
  164.  */
  165. class jDatatypeDateTime extends jDatatype {
  166.     protected $facets = array('maxValue''minValue');
  167.     protected $maxValue;
  168.     protected $minValue;
  169.     private $dt;
  170.     protected $format=21;
  171.     public function check($value{
  172.         $this->dt new jDateTime();
  173.         if(!$this->dt->setFromString($value,$this->format)) return false;
  174.         if($this->maxValue !== null){
  175.             if($this->dt->compareTo($this->maxValue!= -1return false;
  176.         }
  177.         if($this->minValue !== null){
  178.             if($this->dt->compareTo($this->minValue!= 1return false;
  179.         }
  180.         return true;
  181.     }
  182.  
  183.     protected function _addFacet($type,$value){
  184.         if($type == 'maxValue' || $type == 'minValue'){
  185.             $this->$type new jDateTime();
  186.             $this->$type->setFromString($value,$this->format);
  187.         }else{
  188.             parent::_addFacet($type,$value);
  189.         }
  190.     }
  191.     /**
  192.      * @since 1.0
  193.      */
  194.     public function getFormat(return $this->format}
  195. }
  196.  
  197. /**
  198.  * Datatype time
  199.  * @package     jelix
  200.  * @subpackage  utils
  201.  */
  202. class jDatatypeTime extends jDatatypeDateTime {
  203.     protected $format=22;
  204. }
  205. /**
  206.  * Datatype date
  207.  * @package     jelix
  208.  * @subpackage  utils
  209.  */
  210. class jDatatypeDate extends jDatatypeDateTime {
  211.     protected $format=20;
  212. }
  213.  
  214. /**
  215.  * Datatype localedatetime
  216.  * @package     jelix
  217.  * @subpackage  utils
  218.  */
  219. class jDatatypeLocaleDateTime extends jDatatypeDateTime {
  220.     protected $format=11;
  221. }
  222.  
  223. /**
  224.  * Datatype localedate
  225.  * @package     jelix
  226.  * @subpackage  utils
  227.  */
  228. class jDatatypeLocaleDate extends jDatatypeDateTime {
  229.     protected $format=10;
  230. }
  231.  
  232. /**
  233.  * Datatype localetime
  234.  * @package     jelix
  235.  * @subpackage  utils
  236.  */
  237. class jDatatypeLocaleTime extends jDatatypeDateTime {
  238.     protected $format=12;
  239. }
  240.  
  241.  
  242. /**
  243.  * Datatype url
  244.  *
  245.  * Possible facets are: 'schemeRequired','hostRequired','pathRequired', 'queryRequired'.
  246.  * all are booleans.
  247.  * @package     jelix
  248.  * @subpackage  utils
  249.  */
  250. class jDatatypeUrl extends jDatatype {
  251.     protected $schemeRequired=null;
  252.     protected $hostRequired=null;
  253.     protected $pathRequired=null;
  254.     protected $queryRequired=null;
  255.  
  256.     protected $facets = array('schemeRequired','hostRequired','pathRequired''queryRequired');
  257.  
  258.     public function check($value){
  259.         return jFilter::isUrl($value$this->schemeRequired$this->hostRequired$this->pathRequired$this->queryRequired);
  260.     }
  261. }
  262.  
  263. /**
  264.  * Datatype ipv4
  265.  * @package     jelix
  266.  * @subpackage  utils
  267.  */
  268. class jDatatypeIPv4 extends jDatatype {
  269.     public function check($value){
  270.         return jFilter::isIPv4($value);
  271.     }
  272. }
  273.  
  274. /**
  275.  * Datatype ipv6
  276.  * @package     jelix
  277.  * @subpackage  utils
  278.  */
  279. class jDatatypeIPv6 extends jDatatype {
  280.     public function check($value){
  281.         return jFilter::isIPv6($value);
  282.     }
  283. }
  284.  
  285. /**
  286.  * Datatype mail
  287.  * @package     jelix
  288.  * @subpackage  utils
  289.  */
  290. class jDatatypeEmail extends jDatatype {
  291.     public function check($value){
  292.         return jFilter::isEmail($value);
  293.     }
  294. }
  295.  
  296.  
  297. ?>

Documentation generated on Wed, 07 Sep 2011 13:47:06 +0200 by phpDocumentor 1.4.3