Passed
Branch master (8940db)
by Sam
02:38
created

ResourceRecordIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 71.43%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 14
dl 0
loc 52
ccs 15
cts 21
cp 0.7143
rs 10
c 1
b 0
f 0
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getRemainingAsString() 0 9 2
A prev() 0 9 2
A end() 0 4 1
A __construct() 0 3 1
A __toString() 0 3 1
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 20
    public function __construct(string $resourceRecord)
19
    {
20 20
        parent::__construct(explode(Tokens::SPACE, $resourceRecord));
21 20
    }
22
23
    /**
24
     * Return pointer to previous position.
25
     */
26 20
    public function prev(): void
27
    {
28 20
        if (!$this->valid()) {
29
            $this->end();
30
31
            return;
32
        }
33
34 20
        $this->seek((int) $this->key() - 1);
35 20
    }
36
37
    /**
38
     * Set pointer to the end of the array.
39
     */
40
    public function end(): void
41
    {
42
        $lastPos = $this->count() - 1;
43
        $this->seek($lastPos);
44
    }
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 18
    public function getRemainingAsString(): string
52
    {
53 18
        $values = [];
54 18
        while ($this->valid()) {
55 18
            $values[] = $this->current();
56 18
            $this->next();
57
        }
58
59 18
        return implode(Tokens::SPACE, $values);
60
    }
61
62
    /**
63
     * @return string
64
     */
65 2
    public function __toString(): string
66
    {
67 2
        return implode(Tokens::SPACE, $this->getArrayCopy());
68
    }
69
}
70