Overview

Namespaces

  • None
  • PHP
  • Pry
    • Auth
      • Interfaces
    • Config
    • Controller
    • Date
    • Db
    • Feed
      • Abstracts
      • Writers
    • File
      • Decorator
    • Form
      • Element
    • Image
    • Log
      • Writer
    • Net
      • Exception
    • Session
    • Util
    • Validate
      • Validator
    • View

Classes

  • AutoCompleter
  • Checkbox
  • Colorpicker
  • Date
  • DatePicker
  • Email
  • File
  • Hidden
  • Html
  • Ip
  • Mac
  • Multi
  • NumericStepper
  • Password
  • Radio
  • Select
  • Slider
  • Submit
  • Text
  • Textarea
  • Overview
  • Namespace
  • Class
  • Tree
  1: <?php
  2: 
  3: /**
  4:  * Pry Framework
  5:  *
  6:  * LICENSE
  7:  *
  8:  * This source file is subject to the new BSD license that is bundled
  9:  * with this package in the file LICENSE.txt.
 10:  * 
 11:  */
 12: 
 13: namespace Pry\Form\Element;
 14: 
 15: use Pry\Form\Error;
 16: 
 17: /**
 18:  * Element date. Permet de valider différent type de date dans un champs text
 19:  * @category Pry
 20:  * @package Form
 21:  * @subpackage Form_Element
 22:  * @version 1.1.0 
 23:  * @author Olivier ROGER <oroger.fr>
 24:  */
 25: class Date extends Text
 26: {
 27: 
 28:     /**
 29:      * Format de date
 30:      *
 31:      * @var string
 32:      * @access protected
 33:      */
 34:     protected $format;
 35: 
 36:     /**
 37:      * Constructeur. Par défaut format fr
 38:      *
 39:      * @param string $nom
 40:      * @param Form_Form $form
 41:      * @access public
 42:      */
 43:     public function __construct($nom, $form)
 44:     {
 45:         parent::__construct($nom, $form);
 46:         $this->format = 'fr';
 47:     }
 48: 
 49:     /**
 50:      * Défini le format de date
 51:      *
 52:      * @param string $format
 53:      * @access public
 54:      * @return Form_Element_Date
 55:      */
 56:     public function format($format)
 57:     {
 58:         $formatAutorise = array('fr', 'us', 'sql');
 59:         if (in_array($format, $formatAutorise))
 60:             $this->format = $format;
 61:         else
 62:             throw new \InvalidArgumentException('Format : "' . $format . '" de date non reconnu');
 63: 
 64:         return $this;
 65:     }
 66: 
 67:     /**
 68:      * Validation du contenu
 69:      *
 70:      * @param string $value
 71:      * @access public
 72:      * @return boolean
 73:      */
 74:     public function isValid($value)
 75:     {
 76:         if (parent::isValid($value))
 77:         {
 78:             if (!$this->required && empty($value))
 79:                 return true;
 80:             switch ($this->format)
 81:             {
 82:                 case 'fr' :
 83:                     if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $value) > 0 ||
 84:                             preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`', $value) > 0 ||
 85:                             preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`', $value) > 0)
 86:                     {
 87:                         if (strpos($value, ':'))
 88:                         {
 89:                             $datetime = explode(' ', $value);
 90:                             list($jour, $mois, $annee) = explode('/', $datetime[0]);
 91:                             $minutes  = explode(':', $datetime[1]);
 92:                             $heure    = $minutes[0];
 93:                             $min      = (isset($minutes[1])) ? $minutes[1] : '00';
 94:                             $sec      = (isset($minutes[2])) ? $minutes[2] : '00';
 95:                             if (is_null($sec))
 96:                             {
 97:                                 $sec = '00';
 98:                             }
 99:                             if (checkdate($mois, $jour, $annee) && $heure < 24 && $min < 60 && $sec < 60)
100:                                 return true;
101:                         }
102:                         else
103:                         {
104:                             list($jour, $mois, $annee) = explode('/', $value);
105:                             if (checkdate($mois, $jour, $annee))
106:                                 return true;
107:                         }
108:                     }
109:                 case 'us' :
110:                     if (preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2})$`', $value) > 0 ||
111:                             preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}:\d{1,2}$`', $value) > 0 ||
112:                             preg_match('`^\d{1,2}/\d{1,2}/(\d{4}|\d{2}) \d{1,2}:\d{1,2}$`', $value) > 0)
113:                     {
114:                         if (strpos($value, ':'))
115:                         {
116:                             $datetime = explode(' ', $value);
117:                             list($mois, $jour, $annee) = explode('/', $datetime[0]);
118:                             list($heure, $min, $sec) = explode(':', $datetime[1]);
119:                             if (is_null($sec))
120:                             {
121:                                 $sec = '00';
122:                             }
123:                             if (checkdate($mois, $jour, $annee) && $heure < 24 && $min < 60 && $sec < 60)
124:                                 return true;
125:                         }
126:                         else
127:                         {
128:                             list($mois, $jour, $annee) = explode('/', $value);
129:                             if (checkdate($mois, $jour, $annee))
130:                                 return true;
131:                         }
132:                     }
133:                 case 'sql' :
134:                     if (preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2}$`', $value) > 0 ||
135:                             preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}:\d{1,2}$`', $value) > 0 ||
136:                             preg_match('`^(\d{4}|\d{2})-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}$`', $value) > 0)
137:                     {
138:                         if (strpos($value, ':'))
139:                         {
140:                             $datetime = explode(' ', $value);
141:                             list($annee, $mois, $jour) = explode('/', $datetime[0]);
142:                             list($heure, $min, $sec) = explode(':', $datetime[1]);
143:                             if (is_null($sec))
144:                             {
145:                                 $sec = '00';
146:                             }
147:                             if (checkdate($mois, $jour, $annee) && $heure < 24 && $min < 60 && $sec < 60)
148:                                 return true;
149:                         }
150:                         else
151:                         {
152:                             list($annee, $mois, $jour) = explode('/', $value);
153:                             if (checkdate($mois, $jour, $annee))
154:                                 return true;
155:                         }
156:                     }
157: 
158:                 default :
159:                     $this->errorMsg = Error::NOTDATE;
160:                     return false;
161:             }
162:             $this->errorMsg = Error::NOTDATE;
163:             return false;
164:         }
165:     }
166: 
167: }
168: 
169: ?>
Pry API documentation generated by ApiGen 2.8.0