Passed
Push — master ( 75ecec...113700 )
by Carlos C
01:14 queued 12s
created

ScalarValues::timestamp()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace PhpCfdi\SatCatalogos\Helpers;
6
7
/** @internal */
8
final class ScalarValues
9
{
10
    /** @var array<string, scalar> */
11
    private $data;
12
13
    /** @param array<string, scalar> $data */
14 139
    public function __construct(array $data)
15
    {
16 139
        $this->data = $data;
17
    }
18
19
    /** @return scalar|null */
20 139
    public function get(string $key)
21
    {
22 139
        return $this->data[$key] ?? null;
23
    }
24
25 139
    public function string(string $key): string
26
    {
27 139
        return (string) $this->get($key);
28
    }
29
30 112
    public function timestamp(string $key): int
31
    {
32 112
        $value = $this->string($key);
33 112
        if ('' === $value) {
34 95
            return 0;
35
        }
36 112
        return strtotime($value) ?: 0;
37
    }
38
39 24
    public function bool(string $key): bool
40
    {
41 24
        return (bool) $this->get($key);
42
    }
43
44 12
    public function int(string $key): int
45
    {
46 12
        return (int) $this->get($key);
47
    }
48
}
49