Completed
Push — master ( 2391a8...1d9387 )
by Garrett
02:10
created

Chunks::offsetUnset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace StringObject\Decorator;
4
5
use StringObject\StrObj;
6
7
class Chunks implements \ArrayAccess, \Countable, \Iterator
8
{
9
    private $strobj;
10
    private $length;
11
    private $ending;
12
    private $index = 0;
13
14
    public function __construct(StrObj $strobj, $length = 76, $ending = "\r\n")
15
    {
16
        $this->strobj = $strobj;
17
        $this->length = $length;
18
        $this->ending = $ending;
19
    }
20
21
    public function count()
22
    {
23
        return \ceil($this->strobj->length() / ($this->length + 0.0));
24
    }
25
26
    public function current()
27
    {
28
        return $this->offsetGet($this->index);
29
    }
30
31
    public function key()
32
    {
33
        return $this->index;
34
    }
35
36
    public function next()
37
    {
38
        $this->index++;
39
    }
40
41
    public function rewind()
42
    {
43
        $this->index = 0;
44
    }
45
46
    public function valid()
47
    {
48
        return ($this->index * $this->length < $this->strobj->length());
49
    }
50
51
    public function offsetExists($index)
52
    {
53
        $index = (int) $index;
54
        return ($index >= 0 && $index * $this->length < $this->strobj->length());
55
    }
56
57
    public function offsetGet($index)
58
    {
59
        $offset = $index * $this->length;
60
        return $this->strobj->substr(
61
            $offset,
62
            \min($offset + $this->length, $this->strobj->length() - $offset)
63
        )->append($this->ending);
64
    }
65
66
    public function offsetSet($offset, $value)
67
    {
68
        throw new \LogicException('Cannot assign ' . $value . ' to immutable StrObj adapter instance at index ' . $offset);
69
    }
70
71
    public function offsetUnset($offset)
72
    {
73
        throw new \LogicException('Cannot unset index ' . $offset . ' on immutable StrObj adapter instance');
74
    }
75
}
76