Skip to content

Commit

Permalink
Merge pull request #36 from bookboon/hotfix/type-safe-api
Browse files Browse the repository at this point in the history
fix: MappingApi and safe type the config
  • Loading branch information
lkm authored Jun 23, 2022
2 parents 027e6e1 + 902b107 commit 07a375a
Show file tree
Hide file tree
Showing 8 changed files with 142 additions and 58 deletions.
22 changes: 21 additions & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,27 @@ public function getConfigTreeBuilder()
->getRootNode()
->children()
->scalarNode('default_class_namespace')->setDeprecated()->end()
->arrayNode('apis')->normalizeKeys(false)->scalarPrototype()->end()->end()
->arrayNode('apis')
->beforeNormalization()
->ifArray()
->then(static function ($v) {
$outArray = [];
foreach ($v as $namespace => $uri) {
$outArray[] = isset($uri['uri']) ? $uri : [
'namespace' => $namespace,
'uri' => $uri
];
}
return $outArray;
})
->end()
->arrayPrototype()
->children()
->scalarNode('namespace')->end()
->scalarNode('uri')->end()
->end()
->end()
->end()
->arrayNode('mappings')->setDeprecated()
->beforeNormalization()
->ifArray()
Expand Down
6 changes: 5 additions & 1 deletion Helpers/LinkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ protected function getParameters(string $content) : array
$parameters = [ApiIterable::LIMIT, ApiIterable::OFFSET];

$parsedUrl = parse_url($content, PHP_URL_QUERY);
parse_str($parsedUrl, $queryParams);
$queryParams = [];

if (is_string($parsedUrl)) {
parse_str($parsedUrl, $queryParams);
}

foreach ($parameters as $param) {
if (array_key_exists($param, $queryParams)) {
Expand Down
37 changes: 37 additions & 0 deletions Mapping/MappingApi.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Bookboon\JsonLDClient\Mapping;

class MappingApi
{
protected string $namespace;
protected string $uri;

public function __construct($namespace, $uri)
{
$this->namespace = $namespace;
$this->uri = $uri;
}

public function getNamespace(): string
{
return $this->namespace;
}

public function setNamespace(string $namespace): void
{
$this->namespace = $namespace;
}

public function getUri(): string
{
return $this->uri;
}

public function setUri(string $uri): void
{
$this->uri = $uri;
}


}
25 changes: 18 additions & 7 deletions Mapping/MappingCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
class MappingCollection
{
protected const CACHE_PREFIX = 'api-mapping-';

/** @var array<MappingEndpoint> */
protected array $mappings;
/** @var array<string,string> */

/** @var array<MappingApi> */
private array $apis;
private ?CacheInterface $cache;

/**
* @param array $mappings
* @param array<string,string> $apis
* @param array<MappingEndpoint> $mappings
* @param array<MappingApi> $apis
* @throws MappingException
*/
public function __construct(array $mappings, array $apis, CacheInterface $cache = null)
Expand All @@ -37,6 +39,7 @@ public function __construct(array $mappings, array $apis, CacheInterface $cache
public static function create(array $mappings, array $apis): MappingCollection
{
$endpoints = [];
$apiObjects = [];

foreach ($mappings as $map) {
if (!isset($map['uri'], $map['type'])) {
Expand All @@ -55,7 +58,15 @@ public static function create(array $mappings, array $apis): MappingCollection
);
}

return new self($endpoints, $apis);
foreach ($apis as $api) {
if (!isset($api['uri'], $api['namespace'])) {
throw new MappingException('Invalid apis config');
}

$apiObjects[] = new MappingApi($api['namespace'], $api['uri']);
}

return new self($endpoints, $apiObjects);
}

/**
Expand Down Expand Up @@ -162,9 +173,9 @@ protected function getEndpointProperties(string $className): array {
}

protected function findApiForClass(string $classname): string {
foreach ($this->apis as $namespace => $apiUrl) {
if (str_starts_with($classname, $namespace)) {
return $apiUrl;
foreach ($this->apis as $api) {
if (str_starts_with($classname, $api->getNamespace())) {
return $api->getUri();
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/Client/JsonLdClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Bookboon\JsonLDClient\Client\JsonLDNotFoundException;
use Bookboon\JsonLDClient\Client\JsonLDResponseException;
use Bookboon\JsonLDClient\Client\JsonLDSerializationException;
use Bookboon\JsonLDClient\Mapping\MappingApi;
use Bookboon\JsonLDClient\Mapping\MappingCollection;
use Bookboon\JsonLDClient\Mapping\MappingEndpoint;
use Bookboon\JsonLDClient\Models\ApiIterable;
Expand Down Expand Up @@ -521,7 +522,7 @@ protected function getClientForResponses(array $responses, CacheInterface $cache
new MappingEndpoint(NestedClass::class, 'http://otherhost/nested', [], true)
];
$apis = [
'Bookboon\JsonLDClient\Tests\Fixtures\Models' => 'https://example.com/api/v1'
new MappingApi('Bookboon\JsonLDClient\Tests\Fixtures\Models', 'https://example.com/api/v1')
];

return new JsonLDClient(
Expand Down
11 changes: 9 additions & 2 deletions Tests/Fixtures/SerializerHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
namespace Bookboon\JsonLDClient\Tests\Fixtures;


use Bookboon\JsonLDClient\Mapping\MappingApi;
use Bookboon\JsonLDClient\Mapping\MappingCollection;
use Bookboon\JsonLDClient\Mapping\MappingEndpoint;
use Bookboon\JsonLDClient\Serializer\JsonLDEncoder;
use Bookboon\JsonLDClient\Serializer\JsonLDMapNormalizer;
use Bookboon\JsonLDClient\Serializer\JsonLDNormalizer;
Expand All @@ -15,14 +17,19 @@
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
use Symfony\Component\Serializer\Encoder\JsonDecode;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
use Symfony\Component\Serializer\Serializer;
use Symfony\Component\Serializer\SerializerInterface;

class SerializerHelper
{
/**
* @param array<MappingEndpoint> $mappings
* @param array $defaultContext
* @param array<MappingApi> $apiRoot
* @return SerializerInterface
* @throws \Bookboon\JsonLDClient\Mapping\MappingException
*/
public static function create(array $mappings = [], array $defaultContext = [], array $apiRoot = []) : SerializerInterface
{
$docblockExtractor = new PhpDocExtractor();
Expand Down
7 changes: 5 additions & 2 deletions Tests/Mapping/MappingCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,20 @@ public function testMissingPart() : void
[
'type' => 'OtherApp\Test'
]
],[]
],
[]
);
}

public function testInvalidConstructor() : void
{
$this->expectException(MappingException::class);
/** @psalm-suppress InvalidArgument */
$collection = new MappingCollection(
[
new SimpleClass()
],[]
],
[]
);
}

Expand Down
Loading

0 comments on commit 07a375a

Please sign in to comment.