DateTraits::addDateFrom()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
3
namespace Infinitypaul\NairaExchangeRates\Traits;
4
5
use Infinitypaul\NairaExchangeRates\Exceptions\Exceptions;
6
7
trait DateTraits
8
{
9
    // Regular Expression for the date:
10
    private $dateRegExp = '/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/';
11
12
    // Date from which to request historic rates:
13
    private $dateFrom;
14
15
    // Date to which to request historic rates:
16
    private $dateTo;
17
18
    // Get the "from" date:
19
    public function getDateFrom()
20
    {
21
        return $this->dateFrom;
22
    }
23
24
    // Get the "to" date:
25
    public function getDateTo()
26
    {
27
        return $this->dateTo;
28
    }
29
30
    // Add a date-from:
31
    public function addDateFrom(string $from)
32
    {
33
        if ($this->validateDateFormat($from)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->validateDateFormat($from) of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
34
            $this->dateFrom = $from;
35
36
            // Return object to preserve method-chaining:
37
            return $this;
38
        }
39
40
        throw Exceptions::create('format.invalid_date');
41
    }
42
43
    // Add a date-to:
44
    public function addDateTo(string $to)
45
    {
46
        if ($this->validateDateFormat($to)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->validateDateFormat($to) of type integer|false is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
47
            $this->dateTo = $to;
48
49
            // Return object to preserve method-chaining:
50
            return $this;
51
        }
52
53
        throw Exceptions::create('format.invalid_date');
54
    }
55
56
    // Validate a date is in the correct format:
57
    private function validateDateFormat(string $date = null)
58
    {
59
        if (! is_null($date)) {
60
            return preg_match($this->dateRegExp, $date);
61
        }
62
63
        return false;
64
    }
65
}
66