Passed
Pull Request — master (#14)
by Carlos C
02:25
created

ScalarValues   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 39
ccs 15
cts 15
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A int() 0 3 1
A get() 0 3 1
A bool() 0 3 1
A __construct() 0 3 1
A string() 0 3 1
A timestamp() 0 7 3
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