1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Forms\Field\Props;
/**
* Trait for classes:
* - `\MvcCore\Ext\Forms\Fields\Date`
* - `\MvcCore\Ext\Forms\Fields\DateTime`
* - `\MvcCore\Ext\Forms\Fields\Month`
* - `\MvcCore\Ext\Forms\Fields\Time`
* - `\MvcCore\Ext\Forms\Fields\Week`
* Trait contains properties, getters and setters for
* protected properties `min`, `max` and `step`.
*/
trait MinMaxStepDates {
/**
* Minimum value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` field(s) in `string` value.
* Example string values for date and time fields:
* - `Date => "2017-01-01"` (with `$field->format` = "Y-m-d";`)
* - `Time => "14:00"` (with `$field->format` = "H:i";`)
* - `DateTime => "2017-01-01 14:00"` (with `$field->format` = "Y-m-d H:i";`)
* - `Week => "2017-W01"` (with `$field->format` = "o-\WW";`)
* - `Month => "2017-01"` (with `$field->format` = "Y-m";`)
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-min
* @var \DateTimeInterface|NULL
*/
protected $min = NULL;
/**
* Maximum value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` field(s) in `string` value.
* Example string values for date and time fields:
* - `Date => "2018-06-24"` (with `$field->format` = "Y-m-d";`)
* - `Time => "20:00"` (with `$field->format` = "H:i";`)
* - `DateTime => "2018-06-24 20:00"` (with `$field->format` = "Y-m-d H:i";`)
* - `Week => "2018-W25"` (with `$field->format` = "o-\WW";`)
* - `Month => "2018-06"` (with `$field->format` = "Y-m";`)
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-max
* @var \DateTimeInterface|NULL
*/
protected $max = NULL;
/**
* Step value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` fields, always in `integer`.
* For `Date` and `DateTime` fields, step is `int`, number of days.
* For `Time` fields, step is `int`, number of seconds.
* For `Week` and `Month` fields, step is `int`, number of weeks or months...
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-step
* @var int|NULL
*/
protected $step = NULL;
/**
* Get minimum value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` field(s) in `string` value.
* Example string values for date and time fields:
* - `Date => "2017-01-01"` (with `$field->format` = "Y-m-d";`)
* - `Time => "14:00"` (with `$field->format` = "H:i";`)
* - `DateTime => "2017-01-01 14:00"` (with `$field->format` = "Y-m-d H:i";`)
* - `Week => "2017-W01"` (with `$field->format` = "o-\WW";`)
* - `Month => "2017-01"` (with `$field->format` = "Y-m";`)
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-min
* @param bool $getFormatedString Get value as formatted string by `$this->format`.
* @return \DateTimeInterface|string|NULL
*/
public function GetMin ($getFormatedString = FALSE) {
return $getFormatedString
? $this->min->format($this->format)
: $this->min;
}
/**
* Set minimum value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` field(s) in `string` value.
* Example string values for date and time fields:
* - `Date => "2017-01-01"` (with `$field->format` = "Y-m-d";`)
* - `Time => "14:00"` (with `$field->format` = "H:i";`)
* - `DateTime => "2017-01-01 14:00"` (with `$field->format` = "Y-m-d H:i";`)
* - `Week => "2017-W01"` (with `$field->format` = "o-\WW";`)
* - `Month => "2017-01"` (with `$field->format` = "Y-m";`)
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-min
* @param \DateTimeInterface|string|int $min
* @return \MvcCore\Ext\Forms\Field
*/
public function SetMin ($min) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->min = $this->createDateTimeFromInput($min, TRUE);
return $this;
}
/**
* Get maximum value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` field(s) in `string` value.
* Example string values for date and time fields:
* - `Date => "2018-06-24"` (with `$field->format` = "Y-m-d";`)
* - `Time => "20:00"` (with `$field->format` = "H:i";`)
* - `DateTime => "2018-06-24 20:00"` (with `$field->format` = "Y-m-d H:i";`)
* - `Week => "2018-W25"` (with `$field->format` = "o-\WW";`)
* - `Month => "2018-06"` (with `$field->format` = "Y-m";`)
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-max
* @param bool $getFormatedString Get value as formatted string by `$this->format`.
* @return \DateTimeInterface|string|NULL
*/
public function GetMax ($getFormatedString = FALSE) {
return $getFormatedString
? $this->max->format($this->format)
: $this->max;
}
/**
* Set maximum value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` field(s) in `string` value.
* Example string values for date and time fields:
* - `Date => "2018-06-24"` (with `$field->format` = "Y-m-d";`)
* - `Time => "20:00"` (with `$field->format` = "H:i";`)
* - `DateTime => "2018-06-24 20:00"` (with `$field->format` = "Y-m-d H:i";`)
* - `Week => "2018-W25"` (with `$field->format` = "o-\WW";`)
* - `Month => "2018-06"` (with `$field->format` = "Y-m";`)
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-max
* @param \DateTimeInterface|string|int $max
* @return \MvcCore\Ext\Forms\Field
*/
public function SetMax ($max) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->max = $this->createDateTimeFromInput($max, TRUE);
return $this;
}
/**
* Get step value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` fields, always in `integer`.
* For `Date` and `DateTime` fields, step is `int`, number of days.
* For `Time` fields, step is `int`, number of seconds.
* For `Week` and `Month` fields, step is `int`, number of weeks or months...
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-step
* @return int|NULL
*/
public function GetStep () {
return $this->step;
}
/**
* Set step value for `Date`, `Time`, `DateTime`, `Week`
* and `Month` fields, always in `integer`.
* For `Date` and `DateTime` fields, step is `int`, number of days.
* For `Time` fields, step is `int`, number of seconds.
* For `Week` and `Month` fields, step is `int`, number of weeks or months...
* @see https://www.wufoo.com/html5/date-type/
* @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-step
* @param int $step
* @return \MvcCore\Ext\Forms\Field
*/
public function SetStep ($step) {
/** @var $this \MvcCore\Ext\Forms\Field */
$this->step = $step;
return $this;
}
/**
* Create `\DateTimeInterface` value from given `\DateTimeInterface`
* or from given `int` (UNIX timestamp) or from `string` value
* (formatted by `date()` with `$this->format`) and return it.
* @see http://php.net/manual/en/class.datetime.php
* @param \DateTimeInterface|int|string $inputValue
* @return \DateTimeInterface|NULL
*/
protected function createDateTimeFromInput ($inputValue, $throwException = FALSE) {
$newValue = NULL;
if ($inputValue instanceof \DateTimeInterface) {
$newValue = $inputValue;
} else if (is_int($inputValue)) {
$newValue = new \DateTime();
$newValue->setTimestamp($inputValue);
} else if (is_string($inputValue)) {
$parsedValue = @date_create_from_format($this->format, $inputValue);
if ($parsedValue === FALSE) {
if ($throwException) $this->throwNewInvalidArgumentException(
"Value is not possible to parse into `\DateTimeInterface`:"
." `{$inputValue}` by format: `{$this->format}`."
);
} else {
$newValue = $parsedValue;
}
} else if ($throwException) {
$this->throwNewInvalidArgumentException(
"Value is not possible to convert into `\DateTimeInterface`:"
." `{$inputValue}`. Value has to be formatted date string or UNIX"
." epoch integer."
);
}
return $newValue;
}
}