Row::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Tleckie\Csv;
4
5
use function current;
6
use function key;
7
use function next;
8
use function reset;
9
10
/**
11
 * Class Row
12
 *
13
 * @package Tleckie\Csv
14
 * @author  Teodoro Leckie Westberg <[email protected]>
15
 */
16
class Row implements RowInterface
17
{
18
    /** @var array */
19
    private array $data;
20
21
    /**
22
     * Row constructor.
23
     *
24
     * @param array $data
25
     */
26
    public function __construct(array $data = [])
27
    {
28
        $this->data = $data;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34
    public function rewind(): void
35
    {
36
        reset($this->data);
37
    }
38
39
    /**
40
     * @inheritdoc
41
     */
42
    public function current(): string
43
    {
44
        return current($this->data);
45
    }
46
47
    /**
48
     * @inheritdoc
49
     */
50
    public function key(): int
51
    {
52
        return key($this->data);
53
    }
54
55
    /**
56
     * @inheritdoc
57
     */
58
    public function next(): bool
59
    {
60
        return next($this->data);
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function valid(): bool
67
    {
68
        return key($this->data) !== null;
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function toArray(): array
75
    {
76
        return $this->data;
77
    }
78
79
    /**
80
     * @inheritdoc
81
     */
82
    public function reverse(): RowInterface
83
    {
84
        return new Row(array_reverse($this->data));
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function byIndex(mixed $index): mixed
91
    {
92
        return $this->data[$index] ?? null;
93
    }
94
95
    /**
96
     * @inheritdoc
97
     */
98
    public function hasIndex(mixed $index): bool
99
    {
100
        return isset($this->data[$index]);
101
    }
102
103
    /**
104
     * @inheritdoc
105
     */
106
    public function removeByIndex(mixed $index): RowInterface
107
    {
108
        $data = $this->data;
109
110
        unset($data[$index]);
111
112
        return new Row($data);
113
    }
114
115
    /**
116
     * @inheritdoc
117
     */
118
    public function removeFirst(): RowInterface
119
    {
120
        $data = array_slice($this->data, 1, null, true);
121
122
        return new Row($data);
123
    }
124
125
    /**
126
     * @inheritdoc
127
     */
128
    public function removeLast(): RowInterface
129
    {
130
        $data = $this->data;
131
132
        array_pop($data);
133
134
        return new Row($data);
135
    }
136
137
    /**
138
     * @inheritdoc
139
     */
140
    public function count(): int
141
    {
142
        return count($this->data);
143
    }
144
}
145