Skip to content
Snippets Groups Projects
Commit b71eb8cf authored by Tobias Gross-Vogt's avatar Tobias Gross-Vogt
Browse files

optionally include local data

parent 2afbddaf
No related branches found
No related tags found
No related merge requests found
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0df1de6656e47e39e70adb1f054ff0ab", "content-hash": "b9c94ef304f615c39444318e0d0175e8",
"packages": [ "packages": [
{ {
"name": "api-platform/core", "name": "api-platform/core",
...@@ -4533,16 +4533,16 @@ ...@@ -4533,16 +4533,16 @@
}, },
{ {
"name": "symfony/security-bundle", "name": "symfony/security-bundle",
"version": "v5.4.3", "version": "v5.4.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/security-bundle.git", "url": "https://github.com/symfony/security-bundle.git",
"reference": "d3239128269ae67d78df535f65f41cf02cabdc6c" "reference": "d6ae2f605fa8e4e0860c1a6574271af2bb4ba16c"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/security-bundle/zipball/d3239128269ae67d78df535f65f41cf02cabdc6c", "url": "https://api.github.com/repos/symfony/security-bundle/zipball/d6ae2f605fa8e4e0860c1a6574271af2bb4ba16c",
"reference": "d3239128269ae67d78df535f65f41cf02cabdc6c", "reference": "d6ae2f605fa8e4e0860c1a6574271af2bb4ba16c",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -4615,7 +4615,7 @@ ...@@ -4615,7 +4615,7 @@
"description": "Provides a tight integration of the Security component into the Symfony full-stack framework", "description": "Provides a tight integration of the Security component into the Symfony full-stack framework",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/security-bundle/tree/v5.4.3" "source": "https://github.com/symfony/security-bundle/tree/v5.4.5"
}, },
"funding": [ "funding": [
{ {
...@@ -4631,20 +4631,20 @@ ...@@ -4631,20 +4631,20 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2022-01-02T09:53:40+00:00" "time": "2022-02-18T16:06:09+00:00"
}, },
{ {
"name": "symfony/security-core", "name": "symfony/security-core",
"version": "v5.4.3", "version": "v5.4.5",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/security-core.git", "url": "https://github.com/symfony/security-core.git",
"reference": "b26a44457a4d1a60c79f1c23273e812c4077ce85" "reference": "11d815ccbff929899a4ec545f9f85185071abd12"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/security-core/zipball/b26a44457a4d1a60c79f1c23273e812c4077ce85", "url": "https://api.github.com/repos/symfony/security-core/zipball/11d815ccbff929899a4ec545f9f85185071abd12",
"reference": "b26a44457a4d1a60c79f1c23273e812c4077ce85", "reference": "11d815ccbff929899a4ec545f9f85185071abd12",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -4708,7 +4708,7 @@ ...@@ -4708,7 +4708,7 @@
"description": "Symfony Security Component - Core Library", "description": "Symfony Security Component - Core Library",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/security-core/tree/v5.4.3" "source": "https://github.com/symfony/security-core/tree/v5.4.5"
}, },
"funding": [ "funding": [
{ {
...@@ -4724,7 +4724,7 @@ ...@@ -4724,7 +4724,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2022-01-02T09:53:40+00:00" "time": "2022-02-18T16:06:09+00:00"
}, },
{ {
"name": "symfony/security-csrf", "name": "symfony/security-csrf",
...@@ -5586,16 +5586,16 @@ ...@@ -5586,16 +5586,16 @@
}, },
{ {
"name": "symfony/validator", "name": "symfony/validator",
"version": "v5.4.3", "version": "v5.4.6",
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/symfony/validator.git", "url": "https://github.com/symfony/validator.git",
"reference": "b420894e98f414b9ad5d4494650bf281f6dd6028" "reference": "ab461eab209e3be062ba9c609d37b37e8364dbe4"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/symfony/validator/zipball/b420894e98f414b9ad5d4494650bf281f6dd6028", "url": "https://api.github.com/repos/symfony/validator/zipball/ab461eab209e3be062ba9c609d37b37e8364dbe4",
"reference": "b420894e98f414b9ad5d4494650bf281f6dd6028", "reference": "ab461eab209e3be062ba9c609d37b37e8364dbe4",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
...@@ -5679,7 +5679,7 @@ ...@@ -5679,7 +5679,7 @@
"description": "Provides tools to validate values", "description": "Provides tools to validate values",
"homepage": "https://symfony.com", "homepage": "https://symfony.com",
"support": { "support": {
"source": "https://github.com/symfony/validator/tree/v5.4.3" "source": "https://github.com/symfony/validator/tree/v5.4.6"
}, },
"funding": [ "funding": [
{ {
...@@ -5695,7 +5695,7 @@ ...@@ -5695,7 +5695,7 @@
"type": "tidelift" "type": "tidelift"
} }
], ],
"time": "2022-01-26T16:28:35+00:00" "time": "2022-03-02T12:42:23+00:00"
}, },
{ {
"name": "symfony/var-dumper", "name": "symfony/var-dumper",
......
...@@ -8,7 +8,9 @@ use ApiPlatform\Core\DataProvider\ItemDataProviderInterface; ...@@ -8,7 +8,9 @@ use ApiPlatform\Core\DataProvider\ItemDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface; use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use Dbp\Relay\BaseCourseBundle\API\CourseProviderInterface; use Dbp\Relay\BaseCourseBundle\API\CourseProviderInterface;
use Dbp\Relay\BaseCourseBundle\Entity\Course; use Dbp\Relay\BaseCourseBundle\Entity\Course;
use Dbp\Relay\CoreBundle\Exception\ApiError;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
final class CourseItemDataProvider extends AbstractController implements ItemDataProviderInterface, RestrictedDataProviderInterface final class CourseItemDataProvider extends AbstractController implements ItemDataProviderInterface, RestrictedDataProviderInterface
{ {
...@@ -28,8 +30,17 @@ final class CourseItemDataProvider extends AbstractController implements ItemDat ...@@ -28,8 +30,17 @@ final class CourseItemDataProvider extends AbstractController implements ItemDat
{ {
$this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY'); $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
$options = [];
$filters = $context['filters'] ?? []; $filters = $context['filters'] ?? [];
$options = ['lang' => $filters['lang'] ?? 'de']; $options['lang'] = $filters['lang'] ?? 'de';
if ($include = ($filters['include'] ?? null)) {
if ($include === 'localData') {
$options['includeLocalData'] = true;
} else {
throw new ApiError(Response::HTTP_BAD_REQUEST, 'requested inclusion of unknown resource '.$include);
}
}
return $this->api->getCourseById($id, $options); return $this->api->getCourseById($id, $options);
} }
......
...@@ -60,7 +60,8 @@ use Symfony\Component\Serializer\Annotation\Groups; ...@@ -60,7 +60,8 @@ use Symfony\Component\Serializer\Annotation\Groups;
* "tags" = {"BaseCourse"}, * "tags" = {"BaseCourse"},
* "parameters" = { * "parameters" = {
* {"name" = "identifier", "in" = "path", "description" = "Id of course", "required" = true, "type" = "string", "example" = "257571"}, * {"name" = "identifier", "in" = "path", "description" = "Id of course", "required" = true, "type" = "string", "example" = "257571"},
* {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"} * {"name" = "lang", "in" = "query", "description" = "Language of result", "type" = "string", "enum" = {"de", "en"}, "example" = "de"},
* {"name" = "include", "in" = "query", "description" = "Optional resources to include ", "type" = "string", "example" = "localData"}
* } * }
* } * }
* } * }
......
...@@ -6,6 +6,7 @@ namespace Dbp\Relay\BaseCourseBundle\Entity; ...@@ -6,6 +6,7 @@ namespace Dbp\Relay\BaseCourseBundle\Entity;
use ApiPlatform\Core\Annotation\ApiProperty; use ApiPlatform\Core\Annotation\ApiProperty;
use Symfony\Component\Serializer\Annotation\Groups; use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\Ignore;
trait CourseTrait trait CourseTrait
{ {
...@@ -86,23 +87,32 @@ trait CourseTrait ...@@ -86,23 +87,32 @@ trait CourseTrait
$this->description = $description; $this->description = $description;
} }
public function getLocalData(): array
{
return $this->localData;
}
/** /**
* Allows attaching local data to a Course object. * Allows attaching local data to a Course object.
* *
* @param ?mixed $value * @param ?mixed $value
*/ */
public function setLocalData(string $key, $value): void public function setLocalDataValue(string $key, $value): void
{ {
if (!$this->localData) {
$this->localData = [];
}
$this->localData[$key] = $value; $this->localData[$key] = $value;
} }
/** /**
* @Ignore
* Gets local data from a Course object. * Gets local data from a Course object.
* *
* @return ?mixed * @return ?mixed
*/ */
public function getLocalData(string $key) public function getLocalDataValue(string $key)
{ {
return $this->localData[$key] ?? null; return $this->localData ? ($this->localData[$key] ?? null) : null;
} }
} }
...@@ -9,6 +9,11 @@ services: ...@@ -9,6 +9,11 @@ services:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
# Dbp\Relay\BaseCourseBundle\Serializer\Normalizer\CourseNormalizer:
# autowire: true
# autoconfigure: true
# tags: ['serializer.normalizer']
Dbp\Relay\BaseCourseBundle\Service\DummyCourseProvider: Dbp\Relay\BaseCourseBundle\Service\DummyCourseProvider:
autowire: true autowire: true
autoconfigure: true autoconfigure: true
......
<?php
declare(strict_types=1);
namespace Dbp\Relay\BaseCourseBundle\Serializer\Normalizer;
use Dbp\Relay\BaseCourseBundle\Entity\Course;
use Symfony\Component\Serializer\Exception\ExceptionInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerAwareTrait;
class CourseNormalizer implements ContextAwareNormalizerInterface, NormalizerAwareInterface
{
use NormalizerAwareTrait;
private const ALREADY_CALLED = 'COURSE_NORMALIZER_CURRENT_USER_ALREADY_CALLED';
/**
* @return array|string|int|float|bool|\ArrayObject|null
*
* @throws ExceptionInterface
*/
public function normalize($object, $format = null, array $context = [])
{
// TODO: only add localData group if it is requested?
// problem: $context['filters'] not available at this point
$context['groups'][] = 'BaseCourse:localData';
$context[self::ALREADY_CALLED] = true;
return $this->normalizer->normalize($object, $format, $context);
}
public function supportsNormalization($data, $format = null, array $context = []): bool
{
if (isset($context[self::ALREADY_CALLED])) {
return false;
}
return $data instanceof Course;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment