Date.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\lib\validation\impl;
12 
14 
15 /**
16  * Date validates against the specified date format.
17  *
18  * Configuration examples:
19  * @code
20  * // validate against default format (Y-m-d)
21  * date
22  *
23  * // validate against j-M-Y format (e.g. 15-Feb-2009)
24  * date:{"format":"j-M-Y"}
25  * @endcode
26  *
27  * @author ingo herwig <ingo@wemove.com>
28  */
29 class Date implements ValidateType {
30 
31  const DEFAULT_FORMAT = 'Y-m-d';
32 
33  /**
34  * @see ValidateType::validate
35  * $options is an associative array with keys 'format' (optional)
36  */
37  public function validate($value, $options=null, $context=null) {
38  $format = isset($options['format']) ? $options['format'] : self::DEFAULT_FORMAT;
39  return strlen($value) === 0 || (\DateTime::createFromFormat($format, $value) !== false);
40  }
41 }
42 ?>
ValidateType defines the interface for all validator classes.
Date validates against the specified date format.
Definition: Date.php:29
validate($value, $options=null, $context=null)
Definition: Date.php:37