SearchDemand::getEndDate()   A
last analyzed

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
use TYPO3\CMS\Core\Utility\GeneralUtility;
16
17
class SearchDemand
18
{
19
    protected string $search = '';
20
    protected string $fields = '';
21
    protected ?DateTime $startDate = null;
22
    protected ?DateTime $endDate = null;
23
24
    public function getSearch(): string
25
    {
26
        return $this->search;
27
    }
28
29
    public function setSearch(string $search): void
30
    {
31
        $this->search = $search;
32
    }
33
34
    public function getFields(): string
35
    {
36
        return $this->fields;
37
    }
38
39
    public function setFields(string $fields): void
40
    {
41
        $this->fields = $fields;
42
    }
43
44
    public function getStartDate(): ?DateTime
45
    {
46
        return $this->startDate;
47
    }
48
49
    public function setStartDate(?DateTime $startDate): void
50
    {
51
        $this->startDate = $startDate;
52
    }
53
54
    public function getEndDate(): ?DateTime
55
    {
56
        return $this->endDate;
57
    }
58
59
    public function setEndDate(?DateTime $endDate): void
60
    {
61
        $this->endDate = $endDate;
62
    }
63
64
    /**
65
     * Returns if the demand object has at least one search property set
66
     */
67
    public function getHasQuery(): bool
68
    {
69
        return $this->search !== '' || $this->startDate !== null || $this->endDate !== null;
70
    }
71
72
    public function toArray(): array
73
    {
74
        return [
75
            'search' => $this->search,
76
            'fields' => $this->fields,
77
            'startDate' => $this->startDate ? $this->startDate->format(DateTime::RFC3339) : null,
78
            'endDate' => $this->endDate ? $this->endDate->format(DateTime::RFC3339) : null,
79
        ];
80
    }
81
82
    public static function fromArray(array $data): self
83
    {
84
        $demand = GeneralUtility::makeInstance(SearchDemand::class);
85
        $demand->setSearch($data['search'] ?? '');
86
        $demand->setFields($data['fields'] ?? '');
87
        if (isset($data['startDate'])) {
88
            $startDate = DateTime::createFromFormat(DateTime::RFC3339, (string)$data['startDate']);
89
            $startDate = $startDate !== false ? $startDate : null;
90
            $demand->setStartDate($startDate);
91
        }
92
        if (isset($data['endDate'])) {
93
            $endDate = DateTime::createFromFormat(DateTime::RFC3339, (string)$data['endDate']);
94
            $endDate = $endDate !== false ? $endDate : null;
95
            $demand->setEndDate($endDate);
96
        }
97
98
        return $demand;
99
    }
100
}
101