summaryrefslogtreecommitdiff
path: root/vendor/doctrine/orm/src/Query/Expr/OrderBy.php
blob: ac9e1601563bcc7d2d3d0883a1055d8d970e9cfb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Query\Expr;

use Stringable;

use function count;
use function implode;

/**
 * Expression class for building DQL Order By parts.
 *
 * @link    www.doctrine-project.org
 */
class OrderBy implements Stringable
{
    protected string $preSeparator  = '';
    protected string $separator     = ', ';
    protected string $postSeparator = '';

    /** @var string[] */
    protected array $allowedClasses = [];

    /** @psalm-var list<string> */
    protected array $parts = [];

    public function __construct(
        string|null $sort = null,
        string|null $order = null,
    ) {
        if ($sort) {
            $this->add($sort, $order);
        }
    }

    public function add(string $sort, string|null $order = null): void
    {
        $order         = ! $order ? 'ASC' : $order;
        $this->parts[] = $sort . ' ' . $order;
    }

    /** @psalm-return 0|positive-int */
    public function count(): int
    {
        return count($this->parts);
    }

    /** @psalm-return list<string> */
    public function getParts(): array
    {
        return $this->parts;
    }

    public function __toString(): string
    {
        return $this->preSeparator . implode($this->separator, $this->parts) . $this->postSeparator;
    }
}