Completed
Push — master ( f3cdf4...360009 )
by Beniamin
04:07
created

ExpressionCollection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\QueryBuilder\Expression;
13
14
/**
15
 * @author Beniamin Jonatan Šimko <[email protected]>
16
 */
17
class ExpressionCollection implements ExpressionInterface, \ArrayAccess
18
{
19
    /**
20
     * @var ExpressionInterface[] $expressionList
21
     */
22
    private $expressionList;
23
24
    /**
25
     * @var string $separator
26
     */
27
    private $separator;
28
29
    /**
30
     * @param array  $expressionList
31
     * @param string $separator
32
     */
33 32
    public function __construct(array $expressionList = [], $separator = '')
34
    {
35 32
        $this->expressionList = $expressionList;
36 32
        $this->separator = $separator;
37 32
    }
38
39
    /**
40
     * @return ExpressionInterface[]
41
     */
42
    public function getExpressionList()
43
    {
44
        return $this->expressionList;
45
    }
46
47
    /**
48
     * @param string $separator
49
     *
50
     * @return ExpressionCollection
51
     */
52 5
    public function changeSeparator($separator)
53
    {
54 5
        return new self($this->expressionList, $separator);
55
    }
56
57
    /**
58
     * @param ExpressionInterface $expression
59
     *
60
     * @return $this
61
     */
62
    public function addElement(ExpressionInterface $expression)
63
    {
64
        $this->expressionList[] = $expression;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72 29
    public function compile()
73
    {
74 29
        $elements = [];
75
76 29
        foreach ($this->expressionList as $expression) {
77 29
            $elements[] = $expression->compile();
78 29
        }
79
80 29
        return implode($this->separator, $elements);
81
    }
82
83
    /**
84
     * @return bool
85
     */
86 26
    public function isEmpty()
87
    {
88 26
        return empty($this->expressionList);
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94
    public function offsetExists($offset)
95
    {
96
        return array_key_exists($offset, $this->expressionList);
97
    }
98
99
    /**
100
     * @inheritdoc
101
     */
102
    public function offsetGet($offset)
103
    {
104
        return $this->expressionList[$offset];
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function offsetSet($offset, $value)
111
    {
112
        $this->expressionList[$offset] = $value;
113
114
        return $value;
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function offsetUnset($offset)
121
    {
122
        unset($this->expressionList[$offset]);
123
    }
124
}