Passed
Pull Request — master (#67)
by Sergei
13:04
created

ArrayReader::integerOrNullByPath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 4
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Arrays;
6
7
use Closure;
8
9
final class ArrayReader
10
{
11
    private array $array;
12
13
    private bool $convertEmptyToNull = true;
14
15
    public function __construct(array $array)
16
    {
17
        $this->array = $array;
18
    }
19
20
    public function withConvertEmptyToNull(): self
21
    {
22
        $new = clone $this;
23
        $new->convertEmptyToNull = true;
24
        return $new;
25
    }
26
27
    public function withNotConvertEmptyToNull(): self
28
    {
29
        $new = clone $this;
30
        $new->convertEmptyToNull = false;
31
        return $new;
32
    }
33
34
    /**
35
     * @param string|int|float|Closure|array $key
36
     * @param string $default
37
     * @return string
38
     */
39
    public function string($key, string $default = ''): string
40
    {
41
        return (string)$this->value($key, $default);
42
    }
43
44
    /**
45
     * @param string|int|float|Closure|array $path
46
     * @param string $default
47
     * @param string $delimiter
48
     * @return string
49
     */
50
    public function stringByPath($path, string $default = '', string $delimiter = '.'): string
51
    {
52
        return (string)$this->valueByPath($path, $default, $delimiter);
53
    }
54
55
    /**
56
     * @param string|int|float|Closure|array $key
57
     * @param string|null $default
58
     * @return string|null
59
     */
60
    public function stringOrNull($key, ?string $default = null): ?string
61
    {
62
        $value = $this->value($key, $default);
63
        return $value === null ? null : (string)$value;
64
    }
65
66
    /**
67
     * @param string|int|float|Closure|array $path
68
     * @param string|null $default
69
     * @param string $delimiter
70
     * @return string|null
71
     */
72
    public function stringOrNullByPath($path, string $default = null, string $delimiter = '.'): ?string
73
    {
74
        $value = $this->valueByPath($path, $default, $delimiter);
75
        return $value === null ? null : (string)$value;
76
    }
77
78
    /**
79
     * @param string|int|float|Closure|array $key
80
     * @param int $default
81
     * @return int
82
     */
83
    public function integer($key, int $default = 0): int
84
    {
85
        return (int)$this->value($key, $default);
86
    }
87
88
    /**
89
     * @param string|int|float|Closure|array $path
90
     * @param int $default
91
     * @param string $delimiter
92
     * @return int
93
     */
94
    public function integerByPath($path, int $default = 0, string $delimiter = '.'): int
95
    {
96
        return (int)$this->valueByPath($path, $default, $delimiter);
97
    }
98
99
    /**
100
     * @param string|int|float|Closure|array $key
101
     * @param int|null $default
102
     * @return int|null
103
     */
104
    public function integerOrNull($key, ?int $default = null): ?int
105
    {
106
        $value = $this->value($key, $default);
107
        return $value === null ? null : (int)$value;
108
    }
109
110
    /**
111
     * @param string|int|float|Closure|array $path
112
     * @param int|null $default
113
     * @param string $delimiter
114
     * @return int|null
115
     */
116
    public function integerOrNullByPath($path, ?int $default = null, string $delimiter = '.'): ?int
117
    {
118
        $value = $this->valueByPath($path, $default, $delimiter);
119
        return $value === null ? null : (int)$value;
120
    }
121
122
    /**
123
     * @param string|int|float|Closure|array $key
124
     * @param mixed $default
125
     * @return mixed
126
     */
127
    public function value($key, $default = null)
128
    {
129
        return $this->prepareValue(
130
            ArrayHelper::getValue($this->array, $key, $default)
131
        );
132
    }
133
134
    /**
135
     * @param string|int|float|Closure|array $path
136
     * @param mixed $default
137
     * @param string $delimiter
138
     * @return mixed
139
     */
140
    public function valueByPath($path, $default = null, string $delimiter = '.')
141
    {
142
        return $this->prepareValue(
143
            ArrayHelper::getValueByPath($this->array, $path, $default, $delimiter)
144
        );
145
    }
146
147
    /**
148
     * @param mixed $value
149
     * @return mixed
150
     */
151
    private function prepareValue($value)
152
    {
153
        if ($this->convertEmptyToNull && empty($value)) {
154
            return null;
155
        }
156
        return $value;
157
    }
158
}
159