HavingStatement   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 82
rs 10
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A addCondition() 0 15 4
A __clone() 0 4 1
A orHaving() 0 3 1
A having() 0 3 1
A __construct() 0 4 1
A getQueryStatement() 0 3 1
1
<?php
2
3
/**
4
 * Platine Database
5
 *
6
 * Platine Database is the abstraction layer using PDO with support of query and schema builder
7
 *
8
 * This content is released under the MIT License (MIT)
9
 *
10
 * Copyright (c) 2020 Platine Database
11
 *
12
 * Permission is hereby granted, free of charge, to any person obtaining a copy
13
 * of this software and associated documentation files (the "Software"), to deal
14
 * in the Software without restriction, including without limitation the rights
15
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
 * copies of the Software, and to permit persons to whom the Software is
17
 * furnished to do so, subject to the following conditions:
18
 *
19
 * The above copyright notice and this permission notice shall be included in all
20
 * copies or substantial portions of the Software.
21
 *
22
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
 * SOFTWARE.
29
 */
30
31
/**
32
 *  @file HavingStatement.php
33
 *
34
 *  The class for Having query statement
35
 *
36
 *  @package    Platine\Database\Query
37
 *  @author Platine Developers Team
38
 *  @copyright  Copyright (c) 2020
39
 *  @license    http://opensource.org/licenses/MIT  MIT License
40
 *  @link   https://www.platine-php.com
41
 *  @version 1.0.0
42
 *  @filesource
43
 */
44
45
declare(strict_types=1);
46
47
namespace Platine\Database\Query;
48
49
use Closure;
50
51
/**
52
 * @class HavingStatement
53
 * @package Platine\Database\Query
54
 */
55
class HavingStatement
56
{
57
    /**
58
     * The Query statement instance
59
     * @var QueryStatement
60
     */
61
    protected QueryStatement $queryStatement;
62
63
    /**
64
     * The instance of the HavingExpression
65
     * @var HavingExpression
66
     */
67
    protected HavingExpression $expression;
68
69
    /**
70
     * HavingStatement constructor.
71
     * @param QueryStatement|null $queryStatement
72
     */
73
    public function __construct(?QueryStatement $queryStatement = null)
74
    {
75
        $this->queryStatement = $queryStatement ?? new QueryStatement();
76
        $this->expression = new HavingExpression($this->queryStatement);
77
    }
78
79
    /**
80
     * @return QueryStatement
81
     */
82
    public function getQueryStatement(): QueryStatement
83
    {
84
        return $this->queryStatement;
85
    }
86
87
    /**
88
     * @param string|Expression|Closure $column
89
     * @param Closure|null $value
90
     * @return $this
91
     */
92
    public function having(string|Expression|Closure $column, ?Closure $value = null): self
93
    {
94
        return $this->addCondition($column, $value, 'AND');
95
    }
96
97
    /**
98
     * @param string|Expression|Closure $column
99
     * @param Closure|null $value
100
     * @return $this
101
     */
102
    public function orHaving(string|Expression|Closure $column, ?Closure $value = null): self
103
    {
104
        return $this->addCondition($column, $value, 'OR');
105
    }
106
107
    /**
108
     * @inheritDoc
109
     */
110
    public function __clone(): void
111
    {
112
        $this->queryStatement = clone $this->queryStatement;
113
        $this->expression = new HavingExpression($this->queryStatement);
114
    }
115
116
    /**
117
     * @param string|Expression|Closure $column
118
     * @param Closure|null $value
119
     * @param string $separator
120
     * @return $this
121
     */
122
    protected function addCondition(
123
        string|Expression|Closure $column,
124
        ?Closure $value = null,
125
        string $separator = 'AND'
126
    ): self {
127
        if (($column instanceof Closure) && $value === null) {
128
            $this->queryStatement->addHavingGroup($column, $separator);
129
        } else {
130
            $expr = $this->expression->init($column, $separator);
131
            if ($value) {
132
                $value($expr);
133
            }
134
        }
135
136
        return $this;
137
    }
138
}
139