Sort::jsonSerialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace BenTools\OpenCubes\Component\Sort\Model;
4
5
use Psr\Http\Message\UriInterface;
6
use function BenTools\OpenCubes\stringify_uri;
7
8
final class Sort implements \JsonSerializable
9
{
10
    /**
11
     * @var string
12
     */
13
    private $field;
14
15
    /**
16
     * @var string
17
     */
18
    private $direction;
19
20
    /**
21
     * @var bool
22
     */
23
    private $applied;
24
25
    /**
26
     * @var UriInterface
27
     */
28
    private $toggleUri;
29
30
    /**
31
     * Sort constructor.
32
     * @param string            $field
33
     * @param string|null       $direction
34
     * @param bool              $applied
35
     * @param UriInterface|null $uri
36
     */
37
    public function __construct(string $field, ?string $direction = null, bool $applied = true, UriInterface $uri = null)
38
    {
39
        $this->field = $field;
40
        $this->direction = $direction;
41
        $this->applied = $applied;
42
        $this->toggleUri = $uri;
43
    }
44
45
    /**
46
     * @return string
47
     */
48
    public function getField(): string
49
    {
50
        return $this->field;
51
    }
52
53
    /**
54
     * @return string
55
     */
56
    public function getDirection(): ?string
57
    {
58
        return $this->direction;
59
    }
60
61
    /**
62
     * @return bool
63
     */
64
    public function isApplied(): bool
65
    {
66
        return $this->applied;
67
    }
68
69
    /**
70
     * @return UriInterface
71
     */
72
    public function getToggleUri(): ?UriInterface
73
    {
74
        return $this->toggleUri;
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function jsonSerialize(): array
81
    {
82
        $output = [
83
            'field' => $this->field,
84
            'direction' => $this->direction,
85
            'is_applied' => $this->isApplied(),
86
        ];
87
88
        if ($this->isApplied()) {
89
            $output['unset_link'] = stringify_uri($this->getToggleUri());
90
        } else {
91
            $output['link'] = stringify_uri($this->getToggleUri());
92
        }
93
94
        return $output;
95
    }
96
}
97