Skip to content

Commit d34456a

Browse files
committed
added response format
1 parent 991dff7 commit d34456a

File tree

7 files changed

+193
-3
lines changed

7 files changed

+193
-3
lines changed

app/V1Module/presenters/GroupsPresenter.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use App\Helpers\MetaFormats\Attributes\Post;
66
use App\Helpers\MetaFormats\Attributes\Query;
77
use App\Helpers\MetaFormats\Attributes\Path;
8+
use App\Helpers\MetaFormats\Attributes\ResponseFormat;
9+
use App\Helpers\MetaFormats\FormatDefinitions\GroupFormat;
810
use App\Helpers\MetaFormats\Validators\VArray;
911
use App\Helpers\MetaFormats\Validators\VBool;
1012
use App\Helpers\MetaFormats\Validators\VInt;
@@ -404,6 +406,7 @@ public function checkUpdateGroup(string $id)
404406
#[Post("pointsLimit", new VInt(), "A minimum of (absolute) points needed to pass the course", required: false)]
405407
#[Post("localizedTexts", new VArray(), "Localized names and descriptions")]
406408
#[Path("id", new VUuid(), "An identifier of the updated group", required: true)]
409+
#[ResponseFormat(GroupFormat::class)]
407410
public function actionUpdateGroup(string $id)
408411
{
409412
$req = $this->getRequest();
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace App\Helpers\MetaFormats\Attributes;
4+
5+
use Attribute;
6+
7+
/**
8+
* Attribute defining response format on endpoints.
9+
*/
10+
#[Attribute]
11+
class ResponseFormat
12+
{
13+
public string $class;
14+
15+
public function __construct(string $class)
16+
{
17+
$this->class = $class;
18+
}
19+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
<?php
2+
3+
namespace App\Helpers\MetaFormats\FormatDefinitions;
4+
5+
use App\Helpers\MetaFormats\Attributes\Format;
6+
use App\Helpers\MetaFormats\MetaFormat;
7+
use App\Helpers\MetaFormats\Attributes\FPost;
8+
use App\Helpers\MetaFormats\Validators\VArray;
9+
use App\Helpers\MetaFormats\Validators\VBool;
10+
use App\Helpers\MetaFormats\Validators\VEmail;
11+
use App\Helpers\MetaFormats\Validators\VMixed;
12+
use App\Helpers\MetaFormats\Validators\VString;
13+
use App\Helpers\MetaFormats\Validators\VUuid;
14+
use ArrayAccess;
15+
16+
/**
17+
* Format definition used by the RegistrationPresenter::actionCreateInvitation endpoint.
18+
*/
19+
#[Format(GroupFormat::class)]
20+
class GroupFormat extends MetaFormat// implements ArrayAccess
21+
{
22+
#[FPost(new VUuid(), "An identifier of the group")]
23+
public string $id;
24+
25+
#[FPost(
26+
new VString(),
27+
"An informative, human readable identifier of the group",
28+
required: false,
29+
nullable: true,
30+
)]
31+
public ?string $externalId;
32+
33+
#[FPost(
34+
new VBool(),
35+
"Whether the group is organizational (no assignments nor students).",
36+
required: false,
37+
)]
38+
public ?bool $organizational;
39+
40+
#[FPost(new VBool(), "Whether the group is an exam group.", required: false)]
41+
public ?bool $exam;
42+
43+
#[FPost(new VBool(), "Whether the group is archived", required: false)]
44+
public ?bool $archived;
45+
46+
#[FPost(new VBool(), "Should the group be visible to all student?")]
47+
public ?bool $public;
48+
49+
#[FPost(new VBool(), "Whether the group was explicitly marked as archived")]
50+
public ?bool $directlyArchived;
51+
52+
#[FPost(new VArray(), "Localized names and descriptions", required: false)]
53+
public ?array $localizedTexts;
54+
55+
#[FPost(new VArray(new VUuid()), "IDs of users which are explicitly listed as direct admins of this group")]
56+
public ?array $primaryAdminsIds;
57+
58+
#[FPost(
59+
new VUuid(),
60+
"Identifier of the parent group (absent for a top-level group)",
61+
required: false,
62+
)]
63+
public ?string $parentGroupId;
64+
65+
#[FPost(new VArray(new VUuid()), "Identifications of groups in descending order.")]
66+
public ?array $parentGroupsIds;
67+
68+
#[FPost(new VArray(new VUuid()), "Identifications of child groups.")]
69+
public ?array $childGroups;
70+
71+
#[FPost(new VMixed(), "")]
72+
public mixed $privateData;
73+
74+
#[FPost(new VArray(), "")]
75+
public ?array $permissionHints;
76+
77+
78+
// public function offsetExists(mixed $offset): bool
79+
// {
80+
// return isset($this->$offset);
81+
// }
82+
83+
// /**
84+
// * Offset to retrieve
85+
// * @param mixed $offset The offset to retrieve.
86+
// * @return mixed Can return all value types.
87+
// */
88+
// public function offsetGet(mixed $offset): mixed
89+
// {
90+
// return $this->$offset ?? null;
91+
// }
92+
93+
// /**
94+
// * Offset to set
95+
// * @param mixed $offset The offset to assign the value to.
96+
// * @param mixed $value The value to set.
97+
// */
98+
// public function offsetSet(mixed $offset, mixed $value): void
99+
// {
100+
// $this->$offset = $value;
101+
// }
102+
103+
// /**
104+
// * Offset to unset
105+
// * @param mixed $offset The offset to unset.
106+
// */
107+
// public function offsetUnset(mixed $offset): void
108+
// {
109+
// $this->$offset = null;
110+
// }
111+
}

app/helpers/MetaFormats/MetaFormatHelper.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use App\Helpers\MetaFormats\Attributes\Path;
1515
use App\Helpers\MetaFormats\Attributes\Post;
1616
use App\Helpers\MetaFormats\Attributes\Query;
17+
use App\Helpers\MetaFormats\Attributes\ResponseFormat;
1718
use ReflectionClass;
1819
use App\Helpers\Swagger\AnnotationHelper;
1920
use ReflectionMethod;
@@ -42,6 +43,24 @@ public static function extractFormatFromAttribute(
4243
return $formatAttribute->class;
4344
}
4445

46+
/**
47+
* Checks whether an entity contains a ResponseFormat attribute and extracts the format if so.
48+
* @param \ReflectionClass|\ReflectionProperty|\ReflectionMethod $reflectionObject A reflection
49+
* object of the entity.
50+
* @return ?string Returns the format or null if no ResponseFormat attribute was present.
51+
*/
52+
public static function extractResponseFormatFromAttribute(
53+
ReflectionClass | ReflectionProperty | ReflectionMethod $reflectionObject
54+
): ?string {
55+
$formatAttributes = $reflectionObject->getAttributes(ResponseFormat::class);
56+
if (count($formatAttributes) === 0) {
57+
return null;
58+
}
59+
60+
$formatAttribute = $formatAttributes[0]->newInstance();
61+
return $formatAttribute->class;
62+
}
63+
4564
/**
4665
* Extracts all endpoint parameter attributes.
4766
* @param \ReflectionMethod $reflectionMethod The endpoint reflection method.

app/helpers/Swagger/AnnotationData.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function __construct(
4242
array $bodyParams,
4343
array $fileParams,
4444
?string $endpointDescription = null,
45+
?array $responseParams = null,
4546
) {
4647
$this->className = $className;
4748
$this->methodName = $methodName;
@@ -51,6 +52,7 @@ public function __construct(
5152
$this->bodyParams = $bodyParams;
5253
$this->fileParams = $fileParams;
5354
$this->endpointDescription = $endpointDescription;
55+
$this->responseParams = $responseParams;
5456
}
5557

5658
public function getAllParams(): array

app/helpers/Swagger/AnnotationHelper.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ private static function annotationParameterDataToAnnotationData(
289289
string $methodName,
290290
HttpMethods $httpMethod,
291291
array $params,
292-
?string $description
292+
?string $description,
293+
?array $responseParams = null,
293294
): AnnotationData {
294295
$pathParams = [];
295296
$queryParams = [];
@@ -318,7 +319,8 @@ private static function annotationParameterDataToAnnotationData(
318319
$queryParams,
319320
$bodyParams,
320321
$fileParams,
321-
$description
322+
$description,
323+
$responseParams,
322324
);
323325
}
324326

@@ -377,6 +379,16 @@ public static function extractAttributeData(string $className, string $methodNam
377379
$attributeData = array_merge($attributeData, FormatCache::getFieldDefinitions($format));
378380
}
379381

382+
// if the endpoint uses a response format, extract its parameters
383+
$responseFormat = MetaFormatHelper::extractResponseFormatFromAttribute($reflectionMethod);
384+
$responseParams = null;
385+
if ($responseFormat !== null) {
386+
$responseFieldDefinitions = FormatCache::getFieldDefinitions($responseFormat);
387+
$responseParams = array_map(function ($data) {
388+
return $data->toAnnotationParameterData();
389+
}, $responseFieldDefinitions);
390+
}
391+
380392
$params = array_map(function ($data) {
381393
return $data->toAnnotationParameterData();
382394
}, $attributeData);
@@ -387,7 +399,8 @@ public static function extractAttributeData(string $className, string $methodNam
387399
$methodName,
388400
$httpMethod,
389401
$params,
390-
$description
402+
$description,
403+
$responseParams,
391404
);
392405
}
393406

app/model/view/GroupViewFactory.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\EvaluationStatus\EvaluationStatus;
66
use App\Helpers\GroupBindings\GroupBindingAccessor;
7+
use App\Helpers\MetaFormats\FormatDefinitions\GroupFormat;
78
use App\Helpers\PermissionHints;
89
use App\Model\Entity\Assignment;
910
use App\Model\Entity\AssignmentSolution;
@@ -271,6 +272,28 @@ function (Group $group) use ($ignoreArchived) {
271272
}
272273
);
273274

275+
// $groupFormat = new GroupFormat();
276+
// $groupFormat->id = $group->getId();
277+
// $groupFormat->externalId = $group->getExternalId();
278+
// $groupFormat->organizational = $group->isOrganizational();
279+
// $groupFormat->exam = $group->isExam();
280+
// $groupFormat->archived = $group->isArchived();
281+
// $groupFormat->public = $group->isPublic();
282+
// $groupFormat->directlyArchived = $group->isDirectlyArchived();
283+
// $groupFormat->localizedTexts = $group->getLocalizedTexts()->getValues();
284+
// $groupFormat->primaryAdminsIds = $group->getPrimaryAdminsIds();
285+
// $groupFormat->parentGroupId = $group->getParentGroup() ? $group->getParentGroup()->getId() : null;
286+
// $groupFormat->parentGroupsIds = $group->getParentGroupsIds();
287+
// $groupFormat->childGroups = $childGroups->map(
288+
// function (Group $group) {
289+
// return $group->getId();
290+
// }
291+
// )->getValues();
292+
// $groupFormat->privateData = $privateData;
293+
// $groupFormat->permissionHints = PermissionHints::get($this->groupAcl, $group);
294+
295+
// return $groupFormat;
296+
274297
return [
275298
"id" => $group->getId(),
276299
"externalId" => $group->getExternalId(),

0 commit comments

Comments
 (0)