‪TYPO3CMS  ‪main
ReactionDemand.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ServerRequestInterface;
21 
28 {
29  protected const ‪ORDER_DESCENDING = 'desc';
30  protected const ‪ORDER_ASCENDING = 'asc';
31  protected const ‪DEFAULT_ORDER_FIELD = 'name';
32  protected const ‪ORDER_FIELDS = ['name', 'reaction_type'];
33 
34  protected int ‪$limit = 15;
35 
36  public function ‪__construct(
37  protected int $page = 1,
38  protected string $orderField = self::DEFAULT_ORDER_FIELD,
39  protected string $orderDirection = self::ORDER_ASCENDING,
40  protected string $name = '',
41  protected string $reactionType = ''
42  ) {
43  if (!in_array($orderField, self::ORDER_FIELDS, true)) {
44  $orderField = ‪self::DEFAULT_ORDER_FIELD;
45  }
46  $this->orderField = $orderField;
47  if (!in_array($orderDirection, [self::ORDER_DESCENDING, self::ORDER_ASCENDING], true)) {
48  $orderDirection = ‪self::ORDER_ASCENDING;
49  }
50  $this->orderDirection = $orderDirection;
51  }
52 
53  public static function ‪fromRequest(ServerRequestInterface $request): self
54  {
55  $page = (int)($request->getQueryParams()['page'] ?? $request->getParsedBody()['page'] ?? 1);
56  $orderField = (string)($request->getQueryParams()['orderField'] ?? $request->getParsedBody()['orderField'] ?? ‪self::DEFAULT_ORDER_FIELD);
57  $orderDirection = (string)($request->getQueryParams()['orderDirection'] ?? $request->getParsedBody()['orderDirection'] ?? ‪self::ORDER_ASCENDING);
58  $demand = $request->getQueryParams()['demand'] ?? $request->getParsedBody()['demand'] ?? [];
59  if (!is_array($demand) || $demand === []) {
60  return new self($page, $orderField, $orderDirection);
61  }
62  $name = (string)($demand['name'] ?? '');
63  $reactionType = (string)($demand['reaction_type'] ?? '');
64  return new self($page, $orderField, $orderDirection, $name, $reactionType);
65  }
66 
67  public function ‪getOrderField(): string
68  {
69  return $this->orderField;
70  }
71 
72  public function ‪getOrderDirection(): string
73  {
74  return $this->orderDirection;
75  }
76 
77  public function ‪getDefaultOrderDirection(): string
78  {
80  }
81 
82  public function ‪getReverseOrderDirection(): string
83  {
84  return $this->orderDirection === self::ORDER_ASCENDING ? ‪self::ORDER_DESCENDING : ‪self::ORDER_ASCENDING;
85  }
86 
87  public function ‪getName(): string
88  {
89  return $this->name;
90  }
91 
92  public function ‪hasName(): bool
93  {
94  return $this->name !== '';
95  }
96 
97  public function ‪getReactionType(): string
98  {
99  return $this->reactionType;
100  }
101 
102  public function ‪hasReactionType(): bool
103  {
104  return $this->reactionType !== '';
105  }
106 
107  public function ‪hasConstraints(): bool
108  {
109  return $this->‪hasName()
110  || $this->‪hasReactionType();
111  }
112 
113  public function ‪getPage(): int
114  {
115  return $this->page;
116  }
117 
118  public function ‪getLimit(): int
119  {
120  return ‪$this->limit;
121  }
122 
123  public function ‪getOffset(): int
124  {
125  return ($this->page - 1) * ‪$this->limit;
126  }
127 
128  public function ‪getParameters(): array
129  {
130  $parameters = [];
131  if ($this->‪hasName()) {
132  $parameters['name'] = $this->‪getName();
133  }
134  if ($this->‪hasReactionType()) {
135  $parameters['reaction_type'] = $this->‪getReactionType();
136  }
137  return $parameters;
138  }
139 }
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\$limit
‪int $limit
Definition: ReactionDemand.php:34
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getDefaultOrderDirection
‪getDefaultOrderDirection()
Definition: ReactionDemand.php:77
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\hasName
‪hasName()
Definition: ReactionDemand.php:92
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getPage
‪getPage()
Definition: ReactionDemand.php:113
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getParameters
‪getParameters()
Definition: ReactionDemand.php:128
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\DEFAULT_ORDER_FIELD
‪const DEFAULT_ORDER_FIELD
Definition: ReactionDemand.php:31
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getOrderField
‪getOrderField()
Definition: ReactionDemand.php:67
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getReverseOrderDirection
‪getReverseOrderDirection()
Definition: ReactionDemand.php:82
‪TYPO3\CMS\Reactions\Repository\ReactionDemand
Definition: ReactionDemand.php:28
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getReactionType
‪getReactionType()
Definition: ReactionDemand.php:97
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\fromRequest
‪static fromRequest(ServerRequestInterface $request)
Definition: ReactionDemand.php:53
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getOrderDirection
‪getOrderDirection()
Definition: ReactionDemand.php:72
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getOffset
‪getOffset()
Definition: ReactionDemand.php:123
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getName
‪getName()
Definition: ReactionDemand.php:87
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\hasReactionType
‪hasReactionType()
Definition: ReactionDemand.php:102
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\__construct
‪__construct(protected int $page=1, protected string $orderField=self::DEFAULT_ORDER_FIELD, protected string $orderDirection=self::ORDER_ASCENDING, protected string $name='', protected string $reactionType='')
Definition: ReactionDemand.php:36
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\ORDER_FIELDS
‪const ORDER_FIELDS
Definition: ReactionDemand.php:32
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\ORDER_DESCENDING
‪const ORDER_DESCENDING
Definition: ReactionDemand.php:29
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\getLimit
‪getLimit()
Definition: ReactionDemand.php:118
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: ReactionDemand.php:30
‪TYPO3\CMS\Reactions\Repository
Definition: ReactionDemand.php:18
‪TYPO3\CMS\Reactions\Repository\ReactionDemand\hasConstraints
‪hasConstraints()
Definition: ReactionDemand.php:107