Skip to content

Commit b724809

Browse files
committed
added shorthands for format parameter attributes
1 parent 2b61283 commit b724809

File tree

8 files changed

+141
-95
lines changed

8 files changed

+141
-95
lines changed

app/V1Module/presenters/RegistrationPresenter.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
use App\Helpers\EmailVerificationHelper;
3535
use App\Helpers\RegistrationConfig;
3636
use App\Helpers\InvitationHelper;
37-
use App\Helpers\MetaFormats\Attributes\FormatAttribute;
37+
use App\Helpers\MetaFormats\Attributes\Format;
3838
use App\Helpers\MetaFormats\FormatDefinitions\UserFormat;
3939
use App\Helpers\MetaFormats\Attributes\ParamAttribute;
4040
use App\Security\Roles;
@@ -274,19 +274,7 @@ public function checkCreateInvitation()
274274
* @throws BadRequestException
275275
* @throws InvalidArgumentException
276276
*/
277-
#[Post("email", new VEmail(), "An email that will serve as a login name")]
278-
#[Post("firstName", new VString(2), "First name", required: true)]
279-
#[Post("lastName", new VString(2), "Last name")]
280-
#[Post("instanceId", new VString(1), "Identifier of the instance to register in")]
281-
#[Post("titlesBeforeName", new VString(1), "Titles which is placed before user name", required: false)]
282-
#[Post("titlesAfterName", new VString(1), "Titles which is placed after user name", required: false)]
283-
#[Post(
284-
"groups",
285-
new VArray(),
286-
"List of group IDs in which the user is added right after registration",
287-
required: false,
288-
)]
289-
#[Post("locale", new VString(2, 2), "Language used in the invitation email (en by default).", required: false)]
277+
#[Format(UserFormat::class)]
290278
public function actionCreateInvitation()
291279
{
292280
$req = $this->getRequest();
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Helpers\MetaFormats\Attributes;
4+
5+
use App\Helpers\MetaFormats\Type;
6+
use Attribute;
7+
8+
/**
9+
* Attribute used to annotate format definition properties.
10+
*/
11+
#[Attribute(Attribute::TARGET_PROPERTY)]
12+
class FPath extends FormatParameterAttribute
13+
{
14+
/**
15+
* @param mixed $validators A validator object or an array of validators applied to the request parameter.
16+
* @param string $description The description of the request parameter.
17+
* @param bool $required Whether the request parameter is required.
18+
* @param bool $nullable Whether the request parameter can be null.
19+
*/
20+
public function __construct(
21+
mixed $validators,
22+
string $description = "",
23+
bool $required = true,
24+
bool $nullable = false,
25+
) {
26+
parent::__construct(Type::Path, $validators, $description, $required, $nullable);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Helpers\MetaFormats\Attributes;
4+
5+
use App\Helpers\MetaFormats\Type;
6+
use Attribute;
7+
8+
/**
9+
* Attribute used to annotate format definition properties.
10+
*/
11+
#[Attribute(Attribute::TARGET_PROPERTY)]
12+
class FPost extends FormatParameterAttribute
13+
{
14+
/**
15+
* @param mixed $validators A validator object or an array of validators applied to the request parameter.
16+
* @param string $description The description of the request parameter.
17+
* @param bool $required Whether the request parameter is required.
18+
* @param bool $nullable Whether the request parameter can be null.
19+
*/
20+
public function __construct(
21+
mixed $validators,
22+
string $description = "",
23+
bool $required = true,
24+
bool $nullable = false,
25+
) {
26+
parent::__construct(Type::Post, $validators, $description, $required, $nullable);
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Helpers\MetaFormats\Attributes;
4+
5+
use App\Helpers\MetaFormats\Type;
6+
use Attribute;
7+
8+
/**
9+
* Attribute used to annotate format definition properties.
10+
*/
11+
#[Attribute(Attribute::TARGET_PROPERTY)]
12+
class FQuery extends FormatParameterAttribute
13+
{
14+
/**
15+
* @param mixed $validators A validator object or an array of validators applied to the request parameter.
16+
* @param string $description The description of the request parameter.
17+
* @param bool $required Whether the request parameter is required.
18+
* @param bool $nullable Whether the request parameter can be null.
19+
*/
20+
public function __construct(
21+
mixed $validators,
22+
string $description = "",
23+
bool $required = true,
24+
bool $nullable = false,
25+
) {
26+
parent::__construct(Type::Query, $validators, $description, $required, $nullable);
27+
}
28+
}

app/helpers/MetaFormats/Attributes/FormatAttribute.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Attribute for format definitions and usings.
99
*/
1010
#[Attribute]
11-
class FormatAttribute
11+
class Format
1212
{
1313
public string $class;
1414

app/helpers/MetaFormats/FormatDefinitions/GroupFormat.php

Lines changed: 0 additions & 34 deletions
This file was deleted.

app/helpers/MetaFormats/FormatDefinitions/UserFormat.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,39 @@
22

33
namespace App\Helpers\MetaFormats\FormatDefinitions;
44

5-
use App\Helpers\MetaFormats\Attributes\FormatAttribute;
5+
use App\Helpers\MetaFormats\Attributes\Format;
66
use App\Helpers\MetaFormats\MetaFormat;
77
use App\Helpers\MetaFormats\Attributes\FormatParameterAttribute;
8+
use App\Helpers\MetaFormats\Attributes\FPost;
89
use App\Helpers\MetaFormats\Type;
10+
use App\Helpers\MetaFormats\Validators\VArray;
911
use App\Helpers\MetaFormats\Validators\VEmail;
1012
use App\Helpers\MetaFormats\Validators\VString;
1113

12-
#[FormatAttribute(UserFormat::class)]
14+
#[Format(UserFormat::class)]
1315
class UserFormat extends MetaFormat
1416
{
15-
#[FormatAttribute("email")]
16-
#[FormatParameterAttribute(Type::Post, new VEmail(), "An email that will serve as a login name")]
17-
public string $email;
17+
#[FPost(new VEmail(), "An email that will serve as a login name")]
18+
public ?string $email;
1819

19-
#[FormatParameterAttribute(Type::Post, new VString(), "First name")]
20+
#[FPost(new VString(2), "First name", required: true)]
2021
public string $firstName;
2122

22-
#[FormatParameterAttribute(Type::Post, new VString(), "Last name")]
23-
public string $lastName;
23+
#[FPost(new VString(2), "Last name")]
24+
public ?string $lastName;
2425

25-
#[FormatParameterAttribute(Type::Post, new VString(), "A password for authentication")]
26-
public string $password;
26+
#[FPost(new VString(1), "Identifier of the instance to register in")]
27+
public ?string $instanceId;
2728

28-
#[FormatParameterAttribute(Type::Post, new VString(), "A password confirmation")]
29-
public string $passwordConfirm;
30-
31-
#[FormatParameterAttribute(Type::Post, new VString(), "Identifier of the instance to register in")]
32-
public string $instanceId;
33-
34-
#[FormatParameterAttribute(
35-
Type::Post,
36-
new VString(),
37-
"Titles that are placed before user name",
38-
required: false,
39-
nullable: true
40-
)]
29+
#[FPost(new VString(1), "Titles that are placed before user name", required: false)]
4130
public ?string $titlesBeforeName;
4231

43-
#[FormatParameterAttribute(
44-
Type::Post,
45-
new VString(),
46-
"Titles that are placed after user name",
47-
required: false,
48-
nullable: true
49-
)]
32+
#[FPost(new VString(1), "Titles that are placed after user name", required: false)]
5033
public ?string $titlesAfterName;
34+
35+
#[FPost(new VArray(), "List of group IDs in which the user is added right after registration", required: false)]
36+
public ?array $groups;
37+
38+
#[FPost(new VString(2, 2), "Language used in the invitation email (en by default).", required: false)]
39+
public ?string $locale;
5140
}

app/helpers/MetaFormats/MetaFormatHelper.php

Lines changed: 35 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33
namespace App\Helpers\MetaFormats;
44

55
use App\Exceptions\InternalServerException;
6-
use App\Helpers\MetaFormats\Attributes\FormatAttribute;
6+
use App\Helpers\MetaFormats\Attributes\Format;
77
use App\Helpers\MetaFormats\Attributes\FormatParameterAttribute;
8+
use App\Helpers\MetaFormats\Attributes\FPath;
9+
use App\Helpers\MetaFormats\Attributes\FPost;
10+
use App\Helpers\MetaFormats\Attributes\FQuery;
811
use App\Helpers\MetaFormats\Attributes\Param;
912
use App\Helpers\MetaFormats\Attributes\Path;
1013
use App\Helpers\MetaFormats\Attributes\Post;
@@ -45,15 +48,15 @@ public static function extractMethodCheckedParams(string $className, string $met
4548
}
4649

4750
/**
48-
* Checks whether an entity contains a FormatAttribute and extracts the format if so.
51+
* Checks whether an entity contains a Format attribute and extracts the format if so.
4952
* @param \ReflectionClass|\ReflectionProperty|\ReflectionMethod $reflectionObject A reflection
5053
* object of the entity.
51-
* @return ?string Returns the format or null if no FormatAttribute was present.
54+
* @return ?string Returns the format or null if no Format attribute was present.
5255
*/
5356
public static function extractFormatFromAttribute(
54-
ReflectionClass|ReflectionProperty|ReflectionMethod $reflectionObject
57+
ReflectionClass | ReflectionProperty | ReflectionMethod $reflectionObject
5558
): ?string {
56-
$formatAttributes = $reflectionObject->getAttributes(FormatAttribute::class);
59+
$formatAttributes = $reflectionObject->getAttributes(Format::class);
5760
if (count($formatAttributes) === 0) {
5861
return null;
5962
}
@@ -100,11 +103,33 @@ public static function extractRequestParamData(ReflectionMethod $reflectionMetho
100103
return $data;
101104
}
102105

103-
public static function extractFormatParameterData(ReflectionProperty $reflectionObject): ?RequestParamData
106+
/**
107+
* Finds the format attribute of the property and extracts its data.
108+
* @param \ReflectionProperty $reflectionObject The reflection object of the property.
109+
* @throws \App\Exceptions\InternalServerException Thrown when there is not exactly one format attribute.
110+
* @return RequestParamData Returns the data from the attribute.
111+
*/
112+
public static function extractFormatParameterData(ReflectionProperty $reflectionObject): RequestParamData
104113
{
105-
$requestAttributes = $reflectionObject->getAttributes(FormatParameterAttribute::class);
106-
if (count($requestAttributes) === 0) {
107-
return null;
114+
// find all property attributes
115+
$longAttributes = $reflectionObject->getAttributes(FormatParameterAttribute::class);
116+
$pathAttribues = $reflectionObject->getAttributes(FPath::class);
117+
$queryAttributes = $reflectionObject->getAttributes(FQuery::class);
118+
$postAttributes = $reflectionObject->getAttributes(FPost::class);
119+
$requestAttributes = array_merge($longAttributes, $pathAttribues, $queryAttributes, $postAttributes);
120+
121+
// there should be only one attribute
122+
if (count($requestAttributes) == 0) {
123+
throw new InternalServerException(
124+
"The field {$reflectionObject->name} of "
125+
. "class {$reflectionObject->class} does not have a property attribute."
126+
);
127+
}
128+
if (count($requestAttributes) > 1) {
129+
throw new InternalServerException(
130+
"The field {$reflectionObject->name} of "
131+
. "class {$reflectionObject->class} has more than one attribute."
132+
);
108133
}
109134

110135
$requestAttribute = $requestAttributes[0]->newInstance();
@@ -142,18 +167,12 @@ public static function debugGetAttributes(
142167
*/
143168
public static function createNameToFieldDefinitionsMap(string $className): array
144169
{
145-
$class = new ReflectionClass($className);
170+
$class = new ReflectionClass(objectOrClass: $className);
146171
$fields = get_class_vars($className);
147172
$formats = [];
148173
foreach ($fields as $fieldName => $value) {
149174
$field = $class->getProperty($fieldName);
150175
$requestParamData = self::extractFormatParameterData($field);
151-
if ($requestParamData === null) {
152-
throw new InternalServerException(
153-
"The field $fieldName of class $className does not have a RequestAttribute."
154-
);
155-
}
156-
157176
$formats[$fieldName] = $requestParamData;
158177
}
159178

0 commit comments

Comments
 (0)