Issues (590)

src/Query/Expression/Aggregate.php (10 issues)

1
<?php
2
3
namespace Bdf\Prime\Query\Expression;
4
5
use Bdf\Prime\Query\CompilableClause;
6
use Bdf\Prime\Query\Compiler\CompilerInterface;
7
use Bdf\Prime\Query\Compiler\QuoteCompilerInterface;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
10
/**
11
 * Aggregation expressions
12
 * Can be use on Query::select() method
13
 *
14
 * Usage:
15
 * <code>
16
 * User::select([
17
 *     'firstId' => Aggregate::min(id),
18
 *     'lastId' => Aggregate::max(id),
19
 *     'count' => Aggregate::count(id),
20
 * ]);
21
 * </code>
22
 *
23
 * @implements ExpressionInterface<CompilableClause&\Bdf\Prime\Query\Contract\Compilable, CompilerInterface&QuoteCompilerInterface>
24
 */
25
abstract class Aggregate implements ExpressionInterface
26
{
27
    /**
28
     * @var string
29
     */
30
    private $attribute;
31
32
    /**
33
     * Max constructor.
34
     *
35
     * @param string $attribute The attribute name
36
     */
37 6
    public function __construct(string $attribute)
38
    {
39 6
        $this->attribute = $attribute;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     *
45
     * @param CompilerInterface&QuoteCompilerInterface $compiler
46
     */
47 6
    final public function build(CompilableClause $query, object $compiler)
48
    {
49 6
        $attribute = $this->attribute;
50
51 6
        if ($attribute !== '*') {
52 5
            $attribute = $compiler->quoteIdentifier($query, $query->preprocessor()->field($attribute));
53
        }
54
55 6
        return $this->expression($compiler->platform()->grammar(), $attribute);
56
    }
57
58
    /**
59
     * Get the aggregate expression
60
     *
61
     * @param AbstractPlatform $platform The database platform
62
     * @param string $attribute The attribute to aggregate
63
     *
64
     * @return string
65
     */
66
    abstract protected function expression(AbstractPlatform $platform, string $attribute): string;
67
68
    /**
69
     * Perform MIN() aggregation function
70
     *
71
     * @param string $attribute The attribute to aggregate
72
     *
73
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
74
     */
75 1
    public static function min(string $attribute): self
76
    {
77 1
        return new class ($attribute) extends Aggregate {
78
            protected function expression(AbstractPlatform $platform, string $attribute): string
79
            {
80 1
                return $platform->getMinExpression($attribute);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getMinExpression() has been deprecated: Use MIN() in SQL instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
                return /** @scrutinizer ignore-deprecated */ $platform->getMinExpression($attribute);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
81
            }
82 1
        };
83
    }
84
85
    /**
86
     * Perform MAX() aggregation function
87
     *
88
     * @param string $attribute The attribute to aggregate
89
     *
90
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
91
     */
92 1
    public static function max(string $attribute): self
93
    {
94 1
        return new class ($attribute) extends Aggregate {
95
            protected function expression(AbstractPlatform $platform, string $attribute): string
96
            {
97 1
                return $platform->getMaxExpression($attribute);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getMaxExpression() has been deprecated: Use MAX() in SQL instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

97
                return /** @scrutinizer ignore-deprecated */ $platform->getMaxExpression($attribute);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
98
            }
99 1
        };
100
    }
101
102
    /**
103
     * Perform AVG() aggregation function
104
     *
105
     * @param string $attribute The attribute to aggregate
106
     *
107
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
108
     */
109 1
    public static function avg(string $attribute): self
110
    {
111 1
        return new class ($attribute) extends Aggregate {
112
            protected function expression(AbstractPlatform $platform, string $attribute): string
113
            {
114 1
                return $platform->getAvgExpression($attribute);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getAvgExpression() has been deprecated: Use AVG() in SQL instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

114
                return /** @scrutinizer ignore-deprecated */ $platform->getAvgExpression($attribute);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
115
            }
116 1
        };
117
    }
118
119
    /**
120
     * Perform COUNT() aggregation function
121
     *
122
     * @param string $attribute The attribute to aggregate
123
     *
124
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
125
     */
126 2
    public static function count(string $attribute = '*'): self
127
    {
128 2
        return new class ($attribute) extends Aggregate {
129
            protected function expression(AbstractPlatform $platform, string $attribute): string
130
            {
131 2
                return $platform->getCountExpression($attribute);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...m::getCountExpression() has been deprecated: Use COUNT() in SQL instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

131
                return /** @scrutinizer ignore-deprecated */ $platform->getCountExpression($attribute);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
132
            }
133 2
        };
134
    }
135
136
    /**
137
     * Perform SUM() aggregation function
138
     *
139
     * @param string $attribute The attribute to aggregate
140
     *
141
     * @return self
0 ignored issues
show
Documentation Bug introduced by
The doc comment self at position 0 could not be parsed: 'self' is only available from within classes.
Loading history...
142
     */
143 1
    public static function sum(string $attribute): self
144
    {
145 1
        return new class ($attribute) extends Aggregate {
146
            protected function expression(AbstractPlatform $platform, string $attribute): string
147
            {
148 1
                return $platform->getSumExpression($attribute);
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Platforms\...orm::getSumExpression() has been deprecated: Use SUM() in SQL instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

148
                return /** @scrutinizer ignore-deprecated */ $platform->getSumExpression($attribute);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
149
            }
150 1
        };
151
    }
152
}
153