Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Form / Element / DateTime.php
blob8ed1a430d8924753a407df29280a1e3a9c00e75f
1 <?php
2 /**
3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
8 */
10 namespace Zend\Form\Element;
12 use DateInterval;
13 use DateTime as PhpDateTime;
14 use Zend\Form\Element;
15 use Zend\InputFilter\InputProviderInterface;
16 use Zend\Validator\Date as DateValidator;
17 use Zend\Validator\DateStep as DateStepValidator;
18 use Zend\Validator\GreaterThan as GreaterThanValidator;
19 use Zend\Validator\LessThan as LessThanValidator;
21 class DateTime extends Element implements InputProviderInterface
23 const DATETIME_FORMAT = 'Y-m-d\TH:iP';
25 /**
26 * Seed attributes
28 * @var array
30 protected $attributes = array(
31 'type' => 'datetime',
34 /**
35 * A valid format string accepted by date()
37 * @var string
39 protected $format = self::DATETIME_FORMAT;
41 /**
42 * @var array
44 protected $validators;
46 /**
47 * Accepted options for DateTime:
48 * - format: A \DateTime compatible string
50 * @param array|\Traversable $options
51 * @return DateTime
53 public function setOptions($options)
55 parent::setOptions($options);
57 if (isset($this->options['format'])) {
58 $this->setFormat($this->options['format']);
61 return $this;
64 /**
65 * Retrieve the element value
67 * If the value is a DateTime object, and $returnFormattedValue is true
68 * (the default), we return the string
69 * representation using the currently registered format.
71 * If $returnFormattedValue is false, the original value will be
72 * returned, regardless of type.
74 * @param bool $returnFormattedValue
75 * @return mixed
77 public function getValue($returnFormattedValue = true)
79 $value = parent::getValue();
80 if (!$value instanceof PhpDateTime || !$returnFormattedValue) {
81 return $value;
83 $format = $this->getFormat();
84 return $value->format($format);
87 /**
88 * Set value for format
90 * @param string $format
91 * @return DateTime
93 public function setFormat($format)
95 $this->format = (string) $format;
96 return $this;
99 /**
100 * Retrieve the DateTime format to use for the value
102 * @return string
104 public function getFormat()
106 return $this->format;
110 * Get validators
112 * @return array
114 protected function getValidators()
116 if ($this->validators) {
117 return $this->validators;
120 $validators = array();
121 $validators[] = $this->getDateValidator();
123 if (isset($this->attributes['min'])) {
124 $validators[] = new GreaterThanValidator(array(
125 'min' => $this->attributes['min'],
126 'inclusive' => true,
129 if (isset($this->attributes['max'])) {
130 $validators[] = new LessThanValidator(array(
131 'max' => $this->attributes['max'],
132 'inclusive' => true,
135 if (!isset($this->attributes['step'])
136 || 'any' !== $this->attributes['step']
138 $validators[] = $this->getStepValidator();
141 $this->validators = $validators;
142 return $this->validators;
146 * Retrieves a Date Validator configured for a DateTime Input type
148 * @return DateTime
150 protected function getDateValidator()
152 return new DateValidator(array('format' => $this->format));
156 * Retrieves a DateStep Validator configured for a DateTime Input type
158 * @return DateTime
160 protected function getStepValidator()
162 $format = $this->getFormat();
163 $stepValue = (isset($this->attributes['step']))
164 ? $this->attributes['step'] : 1; // Minutes
166 $baseValue = (isset($this->attributes['min']))
167 ? $this->attributes['min'] : date($format, 0);
169 return new DateStepValidator(array(
170 'format' => $format,
171 'baseValue' => $baseValue,
172 'step' => new DateInterval("PT{$stepValue}M"),
177 * Provide default input rules for this element
179 * Attaches default validators for the datetime input.
181 * @return array
183 public function getInputSpecification()
185 return array(
186 'name' => $this->getName(),
187 'required' => true,
188 'filters' => array(
189 array('name' => 'Zend\Filter\StringTrim'),
191 'validators' => $this->getValidators(),