Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| Expires | |
100.00% |
9 / 9 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| __call | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
4 | |||
| get | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Headers\Response; |
| 4 | |
| 5 | use Headers\Response\Exceptions\IntervalMethodNotFoundException; |
| 6 | use InvalidArgumentException; |
| 7 | |
| 8 | /** |
| 9 | * @method self seconds(int $value) |
| 10 | * @method self minutes(int $value) |
| 11 | * @method self hours(int $value) |
| 12 | * @method self days(int $value) |
| 13 | * @method self weeks(int $value) |
| 14 | * @method self months(int $value) |
| 15 | * @method self years(int $value) |
| 16 | */ |
| 17 | class Expires |
| 18 | { |
| 19 | protected array $intervalStrings = []; |
| 20 | |
| 21 | protected const DATETIME_LIST = [ |
| 22 | 'seconds', |
| 23 | 'minutes', |
| 24 | 'hours', |
| 25 | 'days', |
| 26 | 'weeks', |
| 27 | 'months', |
| 28 | 'years' |
| 29 | ]; |
| 30 | |
| 31 | /** |
| 32 | * Obs: |
| 33 | * $method = o nome do método invocado ex: days |
| 34 | * $param[0] = o primeiro parametro capturado pelo __call |
| 35 | * |
| 36 | * Ex: $expires->days(10) |
| 37 | * $method = 'days' |
| 38 | * $params[0] = 10 |
| 39 | */ |
| 40 | public function __call(string $method, array $params): mixed |
| 41 | { |
| 42 | if (false === in_array($method, self::DATETIME_LIST)) { |
| 43 | throw new IntervalMethodNotFoundException("Method '$method' does not exists"); |
| 44 | } |
| 45 | |
| 46 | if (false === is_int($params[0])) { |
| 47 | throw new InvalidArgumentException('Parameter is not int'); |
| 48 | } |
| 49 | |
| 50 | if ($params[0] < 0) { |
| 51 | throw new InvalidArgumentException('Invalid negative parameter'); |
| 52 | } |
| 53 | |
| 54 | $this->intervalStrings[] = $params[0] . ' ' . $method; |
| 55 | return $this; |
| 56 | } |
| 57 | |
| 58 | public function get(): string |
| 59 | { |
| 60 | return implode(' + ', $this->intervalStrings); |
| 61 | } |
| 62 | } |