Skip to content

Commit

Permalink
PHP 8 Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Numkil committed Oct 10, 2023
1 parent 053f02c commit 42905f6
Show file tree
Hide file tree
Showing 22 changed files with 330 additions and 243 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php-versions: ['7.4']
php-versions: ['8.2']
name: PHP ${{ matrix.php-versions }}
steps:
- name: 📤 Checkout project
Expand Down Expand Up @@ -36,7 +36,7 @@ jobs:
- name: 🐘 Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.2
tools: composer

- name: 📦 Install dependencies
Expand All @@ -55,7 +55,7 @@ jobs:
- name: 🐘 Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.2
tools: composer

- name: 📦 Install dependencies
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ build
coverage
composer.lock
/.php_cs.cache
.phpunit.cache/
19 changes: 10 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "cultuurnet/search-v3",
"description": "Cultuurnet search service for version 3",
"type": "library",
"version": "2.0.0",
"license": "Apache-2.0",
"authors": [
{
Expand All @@ -22,16 +23,15 @@
}
},
"require": {
"php": "^7.1",
"guzzlehttp/guzzle": "~6.0",
"jms/serializer": "~1.9",
"simple-bus/jms-serializer-bridge": "^1.0",
"yogarine/doctrine-annotation-autoload": "^0.3.0"
"php": "^8",
"guzzlehttp/guzzle": "^7.8",
"jms/serializer": "^3.28",
"simple-bus/jms-serializer-bridge": "^6.3.1"
},
"require-dev": {
"phpunit/phpunit": "~7.5",
"publiq/php-cs-fixer-config": "^1.3",
"phpstan/phpstan": "^0.12.80"
"phpunit/phpunit": "^10.4.1",
"publiq/php-cs-fixer-config": "^1.3.1",
"phpstan/phpstan": "^0.12.100"
},
"scripts": {
"test": "vendor/bin/phpunit",
Expand All @@ -43,5 +43,6 @@
"composer phpstan",
"composer test"
]
}
},
"config": {}
}
30 changes: 17 additions & 13 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
<phpunit bootstrap="test/bootstrap.php" colors="true">
<testsuite name="all">
<directory>test</directory>
</testsuite>
<logging>
<log type="coverage-clover" target="build/logs/clover.xml"/>
<log type="coverage-html" target="build/logs/coverage"/>
</logging>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">./src</directory>
</whitelist>
</filter>
<?xml version="1.0"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" bootstrap="test/bootstrap.php" colors="true" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.3/phpunit.xsd" cacheDirectory=".phpunit.cache">
<coverage>
<report>
<clover outputFile="build/logs/clover.xml"/>
<html outputDirectory="build/logs/coverage"/>
</report>
</coverage>
<testsuite name="all">
<directory>test</directory>
</testsuite>
<logging/>
<source>
<include>
<directory suffix=".php">./src</directory>
</include>
</source>
</phpunit>
2 changes: 1 addition & 1 deletion src/SearchClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@ private function search(SearchQueryInterface $searchQuery, $type): PagedCollecti

$result = $this->client->request('GET', $type, $options);

return $this->serializer->deserialize((string) $result->getBody());
return $this->serializer->deserialize($result->getBody()->getContents());
}
}
70 changes: 70 additions & 0 deletions src/Serializer/Handler/CollectionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\Serializer\Handler;

use CultuurNet\SearchV3\Serializer\Serializer;
use CultuurNet\SearchV3\ValueObjects\Collection;
use CultuurNet\SearchV3\ValueObjects\Event;
use CultuurNet\SearchV3\ValueObjects\Place;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonDeserializationVisitor;

final class CollectionHandler implements SubscribingHandlerInterface
{
private $contextMapping = [
'/contexts/event' => Event::class,
'/contexts/place' => Place::class,
];

private $typeMapping = [
'Event' => Event::class,
'Place' => Place::class,
];

public static function getSubscribingMethods(): array
{
return [
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => Collection::class,
'method' => 'deserializeCultuurCollectionFromJson',
],
];
}

/**
* @param array<string, mixed> $values
* @param array<string, mixed> $type
*/
public function deserializeCultuurCollectionFromJson(JsonDeserializationVisitor $visitor, array $values, array $type, Context $context): ?Collection
{
$collection = new Collection();
$items = [];
$serializer = new Serializer();

$metadata = [];
foreach ($values as $member) {

if (!isset($member['@context']) && !isset($member['@type'])) {
continue;
}

if (isset($member['@context'])) {
$class = $this->contextMapping[$member['@context']];
} else {
$class = $this->typeMapping[$member['@type']];
}

$object = $serializer->deserialize(json_encode($member), $class);

$collection->addItem($object);
}

return $collection;
}
}
21 changes: 17 additions & 4 deletions src/Serializer/Handler/DateTimeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,34 @@
namespace CultuurNet\SearchV3\Serializer\Handler;

use DateTime;
use JMS\Serializer\Handler\DateHandler;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonDeserializationVisitor;

final class DateTimeHandler extends DateHandler
final class DateTimeHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods(): array
{
return [
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => DateTime::class,
'method' => 'deserializeDateTimeFromJson',
],
];
}

public function deserializeDateTimeFromJson(JsonDeserializationVisitor $visitor, $data, array $type): ?DateTime
{
if ((string)$data === '') {
return null;
}

if (substr($data, -1) === 'Z') {
$type['params'][0] = 'Y-m-d\TH:i:s.u\Z';
return DateTime::createFromFormat('Y-m-d\TH:i:s.u\Z', $data);
}

return parent::deserializeDateTimeFromJson($visitor, $data, $type);
return new DateTime($data);
}
}
50 changes: 50 additions & 0 deletions src/Serializer/Handler/FacetResultsHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\Serializer\Handler;

use CultuurNet\SearchV3\ValueObjects\FacetResult;
use CultuurNet\SearchV3\ValueObjects\FacetResultItem;
use CultuurNet\SearchV3\ValueObjects\FacetResults;
use CultuurNet\SearchV3\ValueObjects\TranslatedString;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonDeserializationVisitor;

final class FacetResultsHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods(): array
{
return [
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => FacetResults::class,
'method' => 'deserializeFacetResultsFromJson',
],
];
}

public function deserializeFacetResultsFromJson(JsonDeserializationVisitor $visitor, array $values, array $type, Context $context): ?FacetResults
{
$facetResults = new FacetResults();

foreach ($values as $facetType => $results) {
$facetResults->addFacetResult($facetType, new FacetResult($facetType, $this->deserializeResults($results)));
}

return $facetResults;
}

private function deserializeResults($results): array
{
$items = [];
foreach ($results as $value => $result) {
$children = isset($result['children']) ? $this->deserializeResults($result['children']) : [];
$items[] = new FacetResultItem($value, new TranslatedString($result['name']), $result['count'], $children);
}
return $items;
}
}
58 changes: 58 additions & 0 deletions src/Serializer/Handler/TranslatedAddressHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\Serializer\Handler;

use CultuurNet\SearchV3\ValueObjects\Address;
use CultuurNet\SearchV3\ValueObjects\TranslatedAddress;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonDeserializationVisitor;

final class TranslatedAddressHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods(): array
{
return [
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => TranslatedAddress::class,
'method' => 'deserializeStringFromJson',
],
];
}

public function deserializeStringFromJson(JsonDeserializationVisitor $visitor, $values, array $type, Context $context): TranslatedAddress
{
$translatedAddress = new TranslatedAddress();


foreach ($values as $key => $value) {
if (is_array($value)) {
$address = new Address(
$value['addressCountry'],
$value['addressLocality'],
$value['postalCode'],
$value['streetAddress']
);
$translatedAddress->addAddress($key, $address);
}
}

// Some properties are not translated yet in the api. We save these as nl.
if (empty($this->addresses) && !empty($values)) {
$address = new Address(
$values['addressCountry'] ?? null,
$values['addressLocality'] ?? null,
$values['postalCode'] ?? null,
$values['streetAddress'] ?? null
);
$translatedAddress->addAddress('nl', $address);
}

return $translatedAddress;
}
}
34 changes: 34 additions & 0 deletions src/Serializer/Handler/TranslatedStringHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace CultuurNet\SearchV3\Serializer\Handler;

use CultuurNet\SearchV3\ValueObjects\TranslatedString;
use JMS\Serializer\Context;
use JMS\Serializer\GraphNavigator;
use JMS\Serializer\Handler\SubscribingHandlerInterface;
use JMS\Serializer\JsonDeserializationVisitor;

final class TranslatedStringHandler implements SubscribingHandlerInterface
{
public static function getSubscribingMethods(): array
{
return [
[
'direction' => GraphNavigator::DIRECTION_DESERIALIZATION,
'format' => 'json',
'type' => TranslatedString::class,
'method' => 'deserializeStringFromJson',
],
];
}

public function deserializeStringFromJson(JsonDeserializationVisitor $visitor, $value, array $type, Context $context): ?TranslatedString
{
$translatedString = new TranslatedString();

$translatedString->setValues(is_array($value) ? $value : ['nl' => $value]);
return $translatedString;
}
}
Loading

0 comments on commit 42905f6

Please sign in to comment.