Completed
Branch 2.0 (acba87)
by Vermeulen
02:20
created

AbstractList::generate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 3
nop 0
dl 0
loc 13
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql\Queries\Parts;
4
5
use \Iterator;
6
7
abstract class AbstractList extends AbstractPart implements Iterator
8
{
9
    /**
10
     * @var array $list List of item into the list
11
     */
12
    protected $list = [];
13
    
14
    /**
15
     * @var integer $position Iterator cursor position
16
     */
17
    protected $position = 0;
18
    
19
    /**
20
     * @var string $separator The separator to use between items during
21
     * the call to generate()
22
     */
23
    protected $separator = '';
24
    
25
    /**
26
     * Getter accessor to property list
27
     * 
28
     * @return array
29
     */
30
    public function getList(): array
31
    {
32
        return $this->list;
33
    }
34
35
    /**
36
     * Getter accessor to property position
37
     * 
38
     * @return integer
39
     */
40
    public function getPosition(): int
41
    {
42
        return $this->position;
43
    }
44
45
    /**
46
     * Getter accessor to property separator
47
     * 
48
     * @return string
49
     */
50
    public function getSeparator(): string
51
    {
52
        return $this->separator;
53
    }
54
    
55
    /**
56
     * Setter accessor to property partPrefix
57
     * 
58
     * @param string $prefix
59
     * 
60
     * @return $this
61
     */
62
    public function setPartPrefix(string $prefix): self
63
    {
64
        $this->partPrefix = $prefix;
65
        return $this;
66
    }
67
    
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function current()
72
    {
73
        return $this->list[$this->position];
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function key(): int
80
    {
81
        return $this->position;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function next()
88
    {
89
        ++$this->position;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function rewind()
96
    {
97
        $this->position = 0;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function valid(): bool
104
    {
105
        return isset($this->list[$this->position]);
106
    }
107
    
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public function generate(): string
112
    {
113
        $sqlPart = '';
114
        
115
        foreach ($this->list as $index => $expr) {
116
            if ($index > 0) {
117
                $sqlPart .= $this->separator;
118
            }
119
            
120
            $sqlPart .= $expr;
121
        }
122
        
123
        return $sqlPart;
124
    }
125
}
126