Completed
Pull Request — master (#524)
by thomas
71:45
created

OperationContainer::__construct()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 1
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace BitWasp\Bitcoin\Script\Path;
4
5
use BitWasp\Bitcoin\Script\Parser\Operation;
6
use BitWasp\Bitcoin\Script\ScriptFactory;
7
8
class OperationContainer implements \ArrayAccess, \Countable
9
{
10
    /**
11
     * @var Operation[]
12
     */
13
    private $container;
14
15
    /**
16
     * @var int
17
     */
18
    private $count;
19
20
    /**
21
     * OperationContainer constructor.
22
     * @param array $operations
23
     */
24
    public function __construct(array $operations = [])
25
    {
26
        foreach ($operations as $operation) {
27
            if (!($operation instanceof Operation)) {
28
                throw new \InvalidArgumentException("Invalid argument - array of Operations required");
29
            }
30
        }
31
32
        $this->container = $operations;
33
        $this->count = count($operations);
34
    }
35
36
    /**
37
     * @return \BitWasp\Bitcoin\Script\ScriptInterface
38
     */
39
    public function makeScript()
40
    {
41
        return ScriptFactory::fromOperations($this->container);
42
    }
43
44
    /**
45
     * @return bool
46
     */
47
    public function isLoneLogicalOp()
48
    {
49
        return $this->count === 1 && $this->container[0]->isLogical();
50
    }
51
52
    /**
53
     * @return array|Operation[]
54
     */
55
    public function all()
56
    {
57
        return $this->container;
58
    }
59
60
    /**
61
     * @return int
62
     */
63
    public function count()
64
    {
65
        return $this->count;
66
    }
67
68
    /**
69
     * @param int $offset
70
     * @param Operation $value
71
     */
72
    public function offsetSet($offset, $value)
73
    {
74
        throw new \RuntimeException("Not implemented");
75
    }
76
77
    /**
78
     * @param int $offset
79
     * @return bool
80
     */
81
    public function offsetExists($offset)
82
    {
83
        return isset($this->container[$offset]);
84
    }
85
86
    /**
87
     * @param int $offset
88
     */
89
    public function offsetUnset($offset)
90
    {
91
        unset($this->container[$offset]);
92
    }
93
94
    /**
95
     * @param int $offset
96
     * @return Operation|null
97
     */
98
    public function offsetGet($offset)
99
    {
100
        return isset($this->container[$offset]) ? $this->container[$offset] : null;
101
    }
102
}
103