CompositeBuilder   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 87
ccs 20
cts 21
cp 0.9524
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
A __toString() 0 6 2
A getType() 0 3 1
A addPart() 0 6 4
A addParts() 0 7 2
A count() 0 3 1
1
<?php
2
3
/**
4
 * Bluz Framework Component
5
 *
6
 * @copyright Bluz PHP Team
7
 * @link      https://github.com/bluzphp/framework
8
 */
9
10
declare(strict_types=1);
11
12
namespace Bluz\Db\Query;
13
14
/**
15
 * Class Expression Builder
16
 *
17
 * @package Bluz\Db\Query
18
 */
19
class CompositeBuilder implements \Countable
20
{
21
    /**
22
     * @var string type AND|OR
23
     */
24
    private $type;
25
26
    /**
27
     * @var array parts of the composite expression
28
     */
29
    private $parts = [];
30
31
    /**
32
     * Constructor
33
     *
34
     * @param array  $parts parts of the composite expression
35
     * @param string $type  AND|OR
36
     */
37 3
    public function __construct(array $parts = [], string $type = 'AND')
38
    {
39 3
        $type = strtoupper($type);
40 3
        $this->type = $type === 'OR' ? 'OR' : 'AND';
41 3
        $this->addParts($parts);
42 3
    }
43
44
    /**
45
     * Adds a set of expressions to composite expression
46
     *
47
     * @param  array $parts
48
     *
49
     * @return CompositeBuilder
50
     */
51 3
    public function addParts($parts): CompositeBuilder
52
    {
53 3
        foreach ($parts as $part) {
54 3
            $this->addPart($part);
55
        }
56
57 3
        return $this;
58
    }
59
60
    /**
61
     * Adds an expression to composite expression
62
     *
63
     * @param  mixed $part
64
     *
65
     * @return CompositeBuilder
66
     */
67 3
    public function addPart($part): CompositeBuilder
68
    {
69 3
        if (!empty($part) || ($part instanceof self && $part->count() > 0)) {
70 3
            $this->parts[] = $part;
71
        }
72 3
        return $this;
73
    }
74
75
    /**
76
     * Return type of this composite
77
     *
78
     * @return string
79
     */
80 3
    public function getType(): string
81
    {
82 3
        return $this->type;
83
    }
84
85
    /**
86
     * Retrieves the amount of expressions on composite expression.
87
     *
88
     * @return integer
89
     */
90 3
    public function count(): int
91
    {
92 3
        return count($this->parts);
93
    }
94
95
    /**
96
     * Retrieve the string representation of this composite expression.
97
     *
98
     * @return string
99
     */
100 3
    public function __toString()
101
    {
102 3
        if ($this->count() === 1) {
103
            return (string) $this->parts[0];
104
        }
105 3
        return '(' . implode(') ' . $this->type . ' (', $this->parts) . ')';
106
    }
107
}
108