Passed
Branch master (7fff21)
by refat
05:12
created

Date   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 26
dl 0
loc 57
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A isDateBetween() 0 11 5
A isAdate() 0 3 1
A __construct() 0 7 1
A minimum() 0 8 2
A maximum() 0 8 2
1
<?php
2
3
namespace System\Validation;
4
5
use DateTime;
6
7
class Date
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->start = $options->start ?? null;
22
    $this->end = $options->end ?? null;
23
    $this->format = $options->format ?? 'd M Y';
24
    $this->value = $value;
25
    $this->year = DateTime::createFromFormat($this->format, $this->value)->format('Y');
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