Passed
Push — master ( c1e68f...f0bd78 )
by Sam
01:33 queued 10s
created

ResourceRecordIterator::prev()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Badcow DNS Library.
7
 *
8
 * (c) Samuel Williams <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Badcow\DNS\Parser;
15
16
class ResourceRecordIterator extends \ArrayIterator
17
{
18 28
    public function __construct(string $resourceRecord)
19
    {
20 28
        parent::__construct(explode(Tokens::SPACE, $resourceRecord));
21 28
    }
22
23
    /**
24
     * Return pointer to previous position.
25
     */
26 26
    public function prev(): void
27
    {
28 26
        if (!$this->valid()) {
29 1
            $this->end();
30
31 1
            return;
32
        }
33
34 26
        $this->seek((int) $this->key() - 1);
35 26
    }
36
37
    /**
38
     * Set pointer to the end of the array.
39
     */
40 1
    public function end(): void
41
    {
42 1
        $lastPos = $this->count() - 1;
43 1
        $this->seek($lastPos);
44 1
    }
45
46
    /**
47
     * Get all the remaining values of an iterator as an array. This will move the pointer to the end of the array.
48
     *
49
     * @return string
50
     */
51 24
    public function getRemainingAsString(): string
52
    {
53 24
        $values = [];
54 24
        while ($this->valid()) {
55 24
            $values[] = $this->current();
56 24
            $this->next();
57
        }
58
59 24
        return implode(Tokens::SPACE, $values);
60
    }
61
62
    /**
63
     * @return string
64
     */
65 3
    public function __toString(): string
66
    {
67 3
        return implode(Tokens::SPACE, $this->getArrayCopy());
68
    }
69
}
70