1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace System\Validation; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
|
7
|
|
|
class DateV |
8
|
|
|
{ |
9
|
|
|
private $start; |
10
|
|
|
private $end; |
11
|
|
|
private $format; |
12
|
|
|
private $value; |
13
|
|
|
private $year; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Constructor |
17
|
|
|
* |
18
|
|
|
*/ |
19
|
|
|
public function __construct($value, $options = []) |
20
|
|
|
{ |
21
|
|
|
$this->format = $options->format ?? 'd M Y'; |
22
|
|
|
$this->value = $value; |
23
|
|
|
$this->start = $options->start ?? null; |
24
|
|
|
$this->end = $options->end ?? null; |
25
|
|
|
$this->year = ($this->isAdate()) ? DateTime::createFromFormat($this->format, $this->value)->format('Y') : null; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function isAdate() |
29
|
|
|
{ |
30
|
|
|
return DateTime::createFromFormat($this->format, $this->value); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function isDateBetween($start = null, $end = null) |
34
|
|
|
{ |
35
|
|
|
if (!$this->start || !$this->end) { |
36
|
|
|
$this->start = $start; |
37
|
|
|
$this->end = $end; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($this->year < $this->start || $this->year > $this->end) { |
41
|
|
|
return false; |
42
|
|
|
} |
43
|
|
|
return true; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function minimum($start = null) |
47
|
|
|
{ |
48
|
|
|
$this->start = $this->start || $start; |
49
|
|
|
|
50
|
|
|
if ($this->year < $this->start) { |
51
|
|
|
return false; |
52
|
|
|
} |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function maximum($end = null) |
57
|
|
|
{ |
58
|
|
|
$this->end = $this->end || $end; |
59
|
|
|
|
60
|
|
|
if ($this->year > $this->end) { |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
return true; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function dateMethods($options) |
67
|
|
|
{ |
68
|
|
|
$method = null; |
69
|
|
|
$msg = null; |
70
|
|
|
|
71
|
|
|
if (isset($options->start) && isset($options->end)) { |
72
|
|
|
$method = 'isDateBetween'; |
73
|
|
|
$msg = 'this field must be between ' . $options->start . ' and ' . $options->end; |
74
|
|
|
} elseif (isset($options->start)) { |
75
|
|
|
$method = 'minimum'; |
76
|
|
|
$msg = 'the date can\'t be under ' . $options->start; |
77
|
|
|
} elseif (isset($options->end)) { |
78
|
|
|
$method = 'maximum'; |
79
|
|
|
$msg = 'the date can\'t be above ' . $options->end; |
80
|
|
|
} |
81
|
|
|
return array('method' => $method, 'msg' => $msg); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|