Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Date.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Validate/Abstract.php';
26 
34 {
35  const INVALID = 'dateInvalid';
36  const INVALID_DATE = 'dateInvalidDate';
37  const FALSEFORMAT = 'dateFalseFormat';
38 
44  protected $_messageTemplates = array(
45  self::INVALID => "Invalid type given. String, integer, array or Zend_Date expected",
46  self::INVALID_DATE => "'%value%' does not appear to be a valid date",
47  self::FALSEFORMAT => "'%value%' does not fit the date format '%format%'",
48  );
49 
53  protected $_messageVariables = array(
54  'format' => '_format'
55  );
56 
62  protected $_format;
63 
69  protected $_locale;
70 
76  public function __construct($options = array())
77  {
78  if ($options instanceof Zend_Config) {
79  $options = $options->toArray();
80  } else if (!is_array($options)) {
81  $options = func_get_args();
82  $temp['format'] = array_shift($options);
83  if (!empty($options)) {
84  $temp['locale'] = array_shift($options);
85  }
86 
87  $options = $temp;
88  }
89 
90  if (array_key_exists('format', $options)) {
91  $this->setFormat($options['format']);
92  }
93 
94  if (!array_key_exists('locale', $options)) {
95  #require_once 'Zend/Registry.php';
96  if (Zend_Registry::isRegistered('Zend_Locale')) {
97  $options['locale'] = Zend_Registry::get('Zend_Locale');
98  }
99  }
100 
101  if (array_key_exists('locale', $options)) {
102  $this->setLocale($options['locale']);
103  }
104  }
105 
111  public function getLocale()
112  {
113  return $this->_locale;
114  }
115 
122  public function setLocale($locale = null)
123  {
124  #require_once 'Zend/Locale.php';
125  $this->_locale = Zend_Locale::findLocale($locale);
126  return $this;
127  }
128 
134  public function getFormat()
135  {
136  return $this->_format;
137  }
138 
145  public function setFormat($format = null)
146  {
147  $this->_format = $format;
148  return $this;
149  }
150 
161  public function isValid($value)
162  {
163  if (!is_string($value) && !is_int($value) && !is_float($value) &&
164  !is_array($value) && !($value instanceof Zend_Date)) {
165  $this->_error(self::INVALID);
166  return false;
167  }
168 
169  $this->_setValue($value);
170 
171  if (($this->_format !== null) || ($this->_locale !== null) || is_array($value) ||
172  $value instanceof Zend_Date) {
173  #require_once 'Zend/Date.php';
174  if (!Zend_Date::isDate($value, $this->_format, $this->_locale)) {
175  if ($this->_checkFormat($value) === false) {
176  $this->_error(self::FALSEFORMAT);
177  } else {
178  $this->_error(self::INVALID_DATE);
179  }
180  return false;
181  }
182  } else {
183  if (!preg_match('/^\d{4}-\d{2}-\d{2}$/', $value)) {
184  $this->_format = 'yyyy-MM-dd';
185  $this->_error(self::FALSEFORMAT);
186  $this->_format = null;
187  return false;
188  }
189 
190  list($year, $month, $day) = sscanf($value, '%d-%d-%d');
191 
192  if (!checkdate($month, $day, $year)) {
193  $this->_error(self::INVALID_DATE);
194  return false;
195  }
196  }
197 
198  return true;
199  }
200 
207  private function _checkFormat($value)
208  {
209  try {
210  #require_once 'Zend/Locale/Format.php';
211  $parsed = Zend_Locale_Format::getDate($value, array(
212  'date_format' => $this->_format, 'format_type' => 'iso',
213  'fix_date' => false));
214  if (isset($parsed['year']) and ((strpos(strtoupper($this->_format), 'YY') !== false) and
215  (strpos(strtoupper($this->_format), 'YYYY') === false))) {
216  $parsed['year'] = Zend_Date::getFullYear($parsed['year']);
217  }
218  } catch (Exception $e) {
219  // Date can not be parsed
220  return false;
221  }
222 
223  if (((strpos($this->_format, 'Y') !== false) or (strpos($this->_format, 'y') !== false)) and
224  (!isset($parsed['year']))) {
225  // Year expected but not found
226  return false;
227  }
228 
229  if ((strpos($this->_format, 'M') !== false) and (!isset($parsed['month']))) {
230  // Month expected but not found
231  return false;
232  }
233 
234  if ((strpos($this->_format, 'd') !== false) and (!isset($parsed['day']))) {
235  // Day expected but not found
236  return false;
237  }
238 
239  if (((strpos($this->_format, 'H') !== false) or (strpos($this->_format, 'h') !== false)) and
240  (!isset($parsed['hour']))) {
241  // Hour expected but not found
242  return false;
243  }
244 
245  if ((strpos($this->_format, 'm') !== false) and (!isset($parsed['minute']))) {
246  // Minute expected but not found
247  return false;
248  }
249 
250  if ((strpos($this->_format, 's') !== false) and (!isset($parsed['second']))) {
251  // Second expected but not found
252  return false;
253  }
254 
255  // Date fits the format
256  return true;
257  }
258 }
__construct($options=array())
Definition: Date.php:76
const FALSEFORMAT
Definition: Date.php:37
static getDate($date, array $options=array())
Definition: Format.php:1154
const INVALID
Definition: Date.php:35
setFormat($format=null)
Definition: Date.php:145
_error($messageKey, $value=null)
Definition: Abstract.php:284
const INVALID_DATE
Definition: Date.php:36
static isRegistered($index)
Definition: Registry.php:178
$value
Definition: gender.phtml:16
$format
Definition: list.phtml:12
static findLocale($locale=null)
Definition: Locale.php:1740
isValid($value)
Definition: Date.php:161
setLocale($locale=null)
Definition: Date.php:122
static get($index)
Definition: Registry.php:141