PdoCompositeExpression::__toString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/*
3
 * This file is part of the Borobudur-Cqrs package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Cqrs\ReadModel\Storage\Pdo\Expression;
12
13
use Borobudur\Cqrs\ReadModel\Storage\Finder\Expression\CompositeExpressionInterface;
14
15
/**
16
 * @author      Iqbal Maulana <[email protected]>
17
 * @created     8/18/15
18
 */
19
class PdoCompositeExpression implements CompositeExpressionInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    private $logic;
25
26
    /**
27
     * @var array
28
     */
29
    private $parts = array();
30
31
    /**
32
     * Constructor.
33
     *
34
     * @param string $logical
35
     * @param array  $parts
36
     */
37
    public function __construct($logical, array $parts)
38
    {
39
        $this->logic = $logical;
40
        $this->add($parts);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function add($part)
47
    {
48
        if (is_array($part)) {
49
            foreach ((array) $part as $value) {
50
                $this->add($value);
51
            }
52
53
            return $this;
54
        }
55
56
        if (!empty($part) || ($part instanceof self && $part->count() > 0)) {
57
            $this->parts[] = $part;
58
        }
59
60
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function getLogic()
67
    {
68
        return $this->logic;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function count()
75
    {
76
        return count($this->parts);
77
    }
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    public function process()
83
    {
84
        return sprintf('(%s)', implode(' ' . $this->logic . ' ', $this->parts));
85
    }
86
87
    /**
88
     * Cast this composite expression to string representation.
89
     *
90
     * @return string
91
     */
92
    public function __toString()
93
    {
94
        return (string) $this->process();
95
    }
96
}
97