Completed
Push — 2.0 ( 33bb83...14462a )
by Peter
07:18 queued 16s
created

SelectNew::modify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * This file is part of the Happyr Doctrine Specification package.
6
 *
7
 * (c) Tobias Nyholm <[email protected]>
8
 *     Kacper Gunia <[email protected]>
9
 *     Peter Gribanov <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Happyr\DoctrineSpecification\Query;
16
17
use Doctrine\ORM\QueryBuilder;
18
use Happyr\DoctrineSpecification\Operand\ArgumentToOperandConverter;
19
use Happyr\DoctrineSpecification\Operand\Operand;
20
21
/**
22
 * Using the NEW operator you can construct Data Transfer Objects (DTOs) directly from DQL queries.
23
 */
24
final class SelectNew implements QueryModifier
25
{
26
    /**
27
     * @var string
28
     */
29
    private $class;
30
31
    /**
32
     * @var Operand[]|mixed[]
33
     */
34
    private $arguments;
35
36
    /**
37
     * @param string        $class
38
     * @param Operand|mixed ...$arguments
39
     *
40
     * @phpstan-param class-string $class
41
     */
42
    public function __construct(string $class, ...$arguments)
43
    {
44
        $this->class = $class;
45
        $this->arguments = $arguments;
46
    }
47
48
    /**
49
     * @param QueryBuilder $qb
50
     * @param string       $context
51
     */
52
    public function modify(QueryBuilder $qb, string $context): void
53
    {
54
        $arguments = [];
55
        foreach (ArgumentToOperandConverter::convert($this->arguments) as $argument) {
56
            $arguments[] = $argument->transform($qb, $context);
57
        }
58
59
        $qb->select(sprintf('NEW %s(%s)', $this->class, implode(', ', $arguments)));
60
    }
61
}
62