Passed
Push — typo3_11 ( 01c1e2...2a0816 )
by Torben
13:32
created

SearchDemand::getSearch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Extension "sf_event_mgt" for TYPO3 CMS.
7
 *
8
 * For the full copyright and license information, please read the
9
 * LICENSE.txt file that was distributed with this source code.
10
 */
11
12
namespace DERHANSEN\SfEventMgt\Domain\Model\Dto;
13
14
use DateTime;
15
16
/**
17
 * Search demand
18
 */
19
class SearchDemand
20
{
21
    protected string $search = '';
22
    protected string $fields = '';
23
    protected ?DateTime $startDate = null;
24
    protected ?DateTime $endDate = null;
25
26
    /**
27
     * @return string
28
     */
29
    public function getSearch(): string
30
    {
31
        return $this->search;
32
    }
33
34
    /**
35
     * @param string $search
36
     */
37
    public function setSearch(string $search): void
38
    {
39
        $this->search = $search;
40
    }
41
42
    /**
43
     * @return string
44
     */
45
    public function getFields(): string
46
    {
47
        return $this->fields;
48
    }
49
50
    /**
51
     * @param string $fields
52
     */
53
    public function setFields(string $fields): void
54
    {
55
        $this->fields = $fields;
56
    }
57
58
    /**
59
     * @return DateTime|null
60
     */
61
    public function getStartDate(): ?DateTime
62
    {
63
        return $this->startDate;
64
    }
65
66
    /**
67
     * @param DateTime|null $startDate
68
     */
69
    public function setStartDate(?DateTime $startDate): void
70
    {
71
        $this->startDate = $startDate;
72
    }
73
74
    /**
75
     * @return DateTime|null
76
     */
77
    public function getEndDate(): ?DateTime
78
    {
79
        return $this->endDate;
80
    }
81
82
    /**
83
     * @param DateTime|null $endDate
84
     */
85
    public function setEndDate(?DateTime $endDate): void
86
    {
87
        $this->endDate = $endDate;
88
    }
89
90
    /**
91
     * Returns if the demand object has at least one search property set
92
     *
93
     * @return bool
94
     */
95
    public function getHasQuery(): bool
96
    {
97
        return $this->search !== '' || $this->startDate !== null || $this->endDate !== null;
98
    }
99
100
    public function toArray(): array
101
    {
102
        return [
103
            'search' => $this->search,
104
            'fields' => $this->fields,
105
            'startDate' => $this->startDate ? $this->startDate->format(DateTime::RFC3339) : null,
106
            'endDate' => $this->endDate ? $this->endDate->format(DateTime::RFC3339) : null,
107
        ];
108
    }
109
110
    public static function fromArray(array $data): self
111
    {
112
        $demand = new SearchDemand();
113
        $demand->setSearch($data['search'] ?? '');
114
        $demand->setFields($data['fields'] ?? '');
115
        $demand->setStartDate(
116
            $data['startDate'] ? DateTime::createFromFormat(DateTime::RFC3339, (string)$data['startDate']) : null
117
        );
118
        $demand->setEndDate(
119
            $data['endDate'] ? DateTime::createFromFormat(DateTime::RFC3339, (string)$data['endDate']) : null
120
        );
121
122
        return $demand;
123
    }
124
}
125