HavingExpression   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
dl 0
loc 116
rs 10
c 1
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A count() 0 5 1
A init() 0 9 2
A min() 0 5 1
A max() 0 5 1
A __clone() 0 7 2
A sum() 0 5 1
A __construct() 0 5 1
A avg() 0 5 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 HavingExpression.php
33
 *
34
 *  The class for Having Expression
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
use Platine\Database\Query\Expression;
51
use Platine\Database\Query\Having;
52
use Platine\Database\Query\QueryStatement;
53
54
/**
55
 * @class HavingExpression
56
 * @package Platine\Database\Query
57
 */
58
class HavingExpression
59
{
60
    /**
61
     * The Query statement instance
62
     * @var QueryStatement
63
     */
64
    protected QueryStatement $queryStatement;
65
66
    /**
67
     * The Having instance
68
     * @var Having
69
     */
70
    protected Having $having;
71
72
    /**
73
     * @var string|Expression
74
     */
75
    protected string|Expression $column;
76
77
    /**
78
     * @var string
79
     */
80
    protected string $separator;
81
82
    /**
83
     * HavingExpression constructor.
84
     * @param QueryStatement $queryStatement
85
     */
86
    public function __construct(QueryStatement $queryStatement)
87
    {
88
        $this->queryStatement = $queryStatement;
89
90
        $this->having = new Having($queryStatement);
91
    }
92
93
    /**
94
     * @param string|Expression|Closure $column
95
     * @param string $separator
96
     * @return $this
97
     */
98
    public function init(string|Expression|Closure $column, string $separator): self
99
    {
100
        if ($column instanceof Closure) {
101
            $column = Expression::fromClosure($column);
102
        }
103
        $this->column = $column;
104
        $this->separator = $separator;
105
106
        return $this;
107
    }
108
109
    /**
110
     * @param bool $distinct
111
     * @return Having
112
     */
113
    public function count(bool $distinct = false): Having
114
    {
115
        $value = (new Expression())->count($this->column, $distinct);
116
117
        return $this->having->init($value, $this->separator);
118
    }
119
120
    /**
121
     * @param bool $distinct
122
     * @return Having
123
     */
124
    public function avg(bool $distinct = false): Having
125
    {
126
        $value = (new Expression())->avg($this->column, $distinct);
127
128
        return $this->having->init($value, $this->separator);
129
    }
130
131
    /**
132
     * @param bool $distinct
133
     * @return Having
134
     */
135
    public function min(bool $distinct = false): Having
136
    {
137
        $value = (new Expression())->min($this->column, $distinct);
138
139
        return $this->having->init($value, $this->separator);
140
    }
141
142
    /**
143
     * @param bool $distinct
144
     * @return Having
145
     */
146
    public function max(bool $distinct = false): Having
147
    {
148
        $value = (new Expression())->max($this->column, $distinct);
149
150
        return $this->having->init($value, $this->separator);
151
    }
152
153
    /**
154
     * @param bool $distinct
155
     * @return Having
156
     */
157
    public function sum(bool $distinct = false): Having
158
    {
159
        $value = (new Expression())->sum($this->column, $distinct);
160
161
        return $this->having->init($value, $this->separator);
162
    }
163
164
    /**
165
     * @inheritDoc
166
     */
167
    public function __clone(): void
168
    {
169
        if ($this->column instanceof Expression) {
170
            $this->column = clone $this->column;
171
        }
172
        $this->queryStatement = clone $this->queryStatement;
173
        $this->having = new Having($this->queryStatement);
174
    }
175
}
176