Skip to content

Commit

Permalink
Fix xml encoding (#10)
Browse files Browse the repository at this point in the history
* Fix xml encoding
  • Loading branch information
alamirault authored Nov 28, 2022
1 parent 087ea2b commit 7a772f5
Show file tree
Hide file tree
Showing 22 changed files with 192 additions and 161 deletions.
4 changes: 1 addition & 3 deletions src/Exception/ClubNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Alamirault\FFTTApi\Exception;

use Exception;

final class ClubNotFoundException extends Exception
final class ClubNotFoundException extends \Exception
{
public function __construct(string $clubId)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/InvalidLienRencontreException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Alamirault\FFTTApi\Exception;

use Exception;

final class InvalidLienRencontreException extends Exception
final class InvalidLienRencontreException extends \Exception
{
public function __construct(string $lienRencontre)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/InvalidRequestException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Alamirault\FFTTApi\Exception;

use Exception;

final class InvalidRequestException extends Exception
final class InvalidRequestException extends \Exception
{
public function __construct(string $uri, int $statusCode, string $content)
{
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/InvalidResponseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Alamirault\FFTTApi\Exception;

use Exception;

final class InvalidResponseException extends Exception
final class InvalidResponseException extends \Exception
{
/**
* @param array<mixed> $content
Expand Down
4 changes: 1 addition & 3 deletions src/Exception/JoueurNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace Alamirault\FFTTApi\Exception;

use Exception;

final class JoueurNotFoundException extends Exception
final class JoueurNotFoundException extends \Exception
{
public function __construct(string $clubId)
{
Expand Down
6 changes: 2 additions & 4 deletions src/Model/Club.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace Alamirault\FFTTApi\Model;

use DateTime;

final class Club
{
public function __construct(
private readonly string $numero,
private readonly string $nom,
private readonly ?DateTime $dateValidation,
private readonly ?\DateTime $dateValidation,
) {}

public function getNumero(): string
Expand All @@ -22,7 +20,7 @@ public function getNom(): string
return $this->nom;
}

public function getDateValidation(): ?DateTime
public function getDateValidation(): ?\DateTime
{
return $this->dateValidation;
}
Expand Down
5 changes: 2 additions & 3 deletions src/Model/Factory/ClubFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Alamirault\FFTTApi\Model\Factory;

use Alamirault\FFTTApi\Model\Club;
use DateTime;

final class ClubFactory
{
Expand All @@ -16,8 +15,8 @@ public function createFromArray(array $data): array
{
$result = [];
foreach ($data as $clubData) {
/** @var DateTime|null $dateValidation */
$dateValidation = is_array($clubData['validation']) ? null : DateTime::createFromFormat('!d/m/Y', $clubData['validation']);
/** @var \DateTime|null $dateValidation */
$dateValidation = is_array($clubData['validation']) ? null : \DateTime::createFromFormat('!d/m/Y', $clubData['validation']);
$result[] = new Club(
$clubData['numero'],
$clubData['nom'],
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Factory/RencontreDetailsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ private function formatJoueur(string $prenom, string $nom, string $points, array
$joueurClub->getNom(),
$joueurClub->getPrenom(),
$joueurClub->getLicence(),
$playerPoints,
(int) $playerPoints,
$sexe
);
}
Expand Down
10 changes: 4 additions & 6 deletions src/Model/Rencontre/Rencontre.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Alamirault\FFTTApi\Model\Rencontre;

use DateTime;

final class Rencontre
{
public function __construct(
Expand All @@ -13,8 +11,8 @@ public function __construct(
private readonly int $scoreEquipeA,
private readonly int $scoreEquipeB,
private readonly string $lien,
private readonly DateTime $datePrevue,
private readonly ?DateTime $dateReelle,
private readonly \DateTime $datePrevue,
private readonly ?\DateTime $dateReelle,
) {}

public function getLibelle(): string
Expand Down Expand Up @@ -47,12 +45,12 @@ public function getLien(): string
return $this->lien;
}

public function getDatePrevue(): DateTime
public function getDatePrevue(): \DateTime
{
return $this->datePrevue;
}

public function getDateReelle(): ?DateTime
public function getDateReelle(): ?\DateTime
{
return $this->dateReelle;
}
Expand Down
4 changes: 1 addition & 3 deletions src/Service/FFTTClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Alamirault\FFTTApi\Exception\InvalidRequestException;
use Alamirault\FFTTApi\Exception\InvalidResponseException;
use DomainException;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -42,15 +41,14 @@ private function send(string $uri): array
$response = $this->client->request('GET', $uri);

if (200 !== $response->getStatusCode()) {
throw new DomainException(sprintf('Request "%s" returns an error', $uri));
throw new \DomainException(sprintf('Request "%s" returns an error', $uri));
}

$content = $response->getBody()->getContents();

// Lot of hacks due to ugly/buggy FFTT Api response format
/** @var string $content */
$content = preg_replace('/&(?!#?[a-z0-9]+;)/', '&amp;', $content);
$content = mb_convert_encoding($content, 'ISO-8859-1', 'UTF-8');
$content = html_entity_decode($content);

$xml = simplexml_load_string($content, 'SimpleXMLElement', LIBXML_NOCDATA);
Expand Down
5 changes: 2 additions & 3 deletions src/Service/Operation/ListActualiteOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Alamirault\FFTTApi\Model\Actualite;
use Alamirault\FFTTApi\Service\FFTTClientInterface;
use DateTime;

final class ListActualiteOperation
{
Expand All @@ -25,8 +24,8 @@ public function listActualites(): array
$result = [];
/** @var array{date: string, titre: string, description: string, url: string, photo: string, categorie: string} $dataActualite */
foreach ($data as $dataActualite) {
/** @var DateTime $date */
$date = DateTime::createFromFormat('!Y-m-d', $dataActualite['date']);
/** @var \DateTime $date */
$date = \DateTime::createFromFormat('!Y-m-d', $dataActualite['date']);
$result[] = new Actualite(
$date,
$dataActualite['titre'],
Expand Down
3 changes: 1 addition & 2 deletions src/Service/Operation/ListClubOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Alamirault\FFTTApi\Model\Club;
use Alamirault\FFTTApi\Model\Factory\ClubFactory;
use Alamirault\FFTTApi\Service\FFTTClientInterface;
use Exception;

final class ListClubOperation
{
Expand Down Expand Up @@ -43,7 +42,7 @@ public function listClubsByName(string $name): array
$rawClubs = $this->arrayWrapper->wrapArrayIfUnique($rawClubs);

return $this->clubFactory->createFromArray($rawClubs);
} catch (Exception $e) {
} catch (\Exception $e) {
return [];
}
}
Expand Down
29 changes: 14 additions & 15 deletions src/Service/Operation/ListPartieOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Alamirault\FFTTApi\Service\FFTTClientInterface;
use Alamirault\FFTTApi\Service\NomPrenomExtractorInterface;
use DateTime;
use Transliterator;

final class ListPartieOperation
{
Expand Down Expand Up @@ -36,8 +35,8 @@ public function listPartiesJoueurByLicence(string $licenceId): array
/** @var array{advnompre: string, date: string, vd: string, numjourn: string, pointres: string, coefchamp: string, advlic: string, advsexe: string, advclaof: string} $partie */
foreach ($parties as $partie) {
[$nom, $prenom] = $this->nomPrenomExtractor->extractNomPrenom($partie['advnompre']);
/** @var DateTime $date */
$date = DateTime::createFromFormat('d/m/Y', $partie['date']);
/** @var \DateTime $date */
$date = \DateTime::createFromFormat('d/m/Y', $partie['date']);

$realPartie = new Partie(
'V' === $partie['vd'],
Expand Down Expand Up @@ -82,8 +81,8 @@ public function listUnvalidatedPartiesJoueurByLicence(string $joueurId): array
);
}));

/** @var DateTime $date */
$date = DateTime::createFromFormat('d/m/Y', $partie['date']);
/** @var \DateTime $date */
$date = \DateTime::createFromFormat('d/m/Y', $partie['date']);

if (!$found && 'Absent Absent' !== $prenom
&& $this->isInCurrentSaison($date)
Expand All @@ -110,8 +109,8 @@ public function listUnvalidatedPartiesJoueurByLicence(string $joueurId): array

private function removeAccentLowerCaseRegex(string $string): string
{
/** @var Transliterator $transliterator */
$transliterator = Transliterator::create('NFD; [:Nonspacing Mark:] Remove;');
/** @var \Transliterator $transliterator */
$transliterator = \Transliterator::create('NFD; [:Nonspacing Mark:] Remove;');

/** @var string $transliterated */
$transliterated = $transliterator->transliterate($string);
Expand All @@ -122,15 +121,15 @@ private function removeAccentLowerCaseRegex(string $string): string
/**
* Détermine si la date d'une rencontre passée en paramètre correspond à la saison en cours.
*/
private function isInCurrentSaison(Datetime $dateRencontre): bool
private function isInCurrentSaison(\DateTime $dateRencontre): bool
{
$today = new DateTime();
$today = new \DateTime();

$actualMonth = (int) $today->format('n');
$actualYear = (int) $today->format('Y');

$dateDebutSaison = new DateTime($actualYear + ($actualMonth >= 7 ? 0 : -1).'-07-01');
$dateFinSaison = new DateTime($actualYear + ($actualMonth >= 7 ? 1 : 0).'-07-01');
$dateDebutSaison = new \DateTime($actualYear + ($actualMonth >= 7 ? 0 : -1).'-07-01');
$dateFinSaison = new \DateTime($actualYear + ($actualMonth >= 7 ? 1 : 0).'-07-01');

return $dateRencontre >= $dateDebutSaison && $dateRencontre <= $dateFinSaison;
}
Expand All @@ -141,9 +140,9 @@ private function isInCurrentSaison(Datetime $dateRencontre): bool
* Exemple : si nous sommes le 6 Octobre, nous prenons les rencontre du 1er Septembre à aujourd'hui.
* Exemple : si nous sommes le 15 Octobre, nous prenons les rencontre du 1er Octobre (les points virtuels sont connus à partir du 5 et les rencontres sont comptabilisées du 1er au 31 du mois) à aujourd'hui.
*/
private function isInCurrentVirtualMonth(Datetime $dateRencontre): bool
private function isInCurrentVirtualMonth(\DateTime $dateRencontre): bool
{
$today = new DateTime();
$today = new \DateTime();

$actualMonth = $today->format('n');
$actualYear = $today->format('Y');
Expand All @@ -159,8 +158,8 @@ private function isInCurrentVirtualMonth(Datetime $dateRencontre): bool
}
}

$dateDebutMoisVirtuel = new DateTime($actualYear.'-'.$debutMoisVirtuel.'-01');
$dateFinMoisVirtuel = new DateTime();
$dateDebutMoisVirtuel = new \DateTime($actualYear.'-'.$debutMoisVirtuel.'-01');
$dateFinMoisVirtuel = new \DateTime();

return $dateRencontre >= $dateDebutMoisVirtuel && $dateRencontre <= $dateFinMoisVirtuel;
}
Expand Down
9 changes: 4 additions & 5 deletions src/Service/Operation/ListRencontreOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Alamirault\FFTTApi\Model\Equipe;
use Alamirault\FFTTApi\Model\Rencontre\Rencontre;
use Alamirault\FFTTApi\Service\FFTTClientInterface;
use DateTime;

final class ListRencontreOperation
{
Expand All @@ -32,11 +31,11 @@ public function listRencontrePouleByLienDivision(string $lienDivision): array
/** @var string $nomEquipeB */
$nomEquipeB = is_array($equipeB) ? '' : $equipeB;

/** @var DateTime $datePrevue */
$datePrevue = DateTime::createFromFormat('d/m/Y', $dataRencontre['dateprevue']);
/** @var \DateTime $datePrevue */
$datePrevue = \DateTime::createFromFormat('d/m/Y', $dataRencontre['dateprevue']);

/** @var DateTime|null $dateReelle */
$dateReelle = empty($dataRencontre['datereelle']) ? null : DateTime::createFromFormat('d/m/Y', $dataRencontre['datereelle']);
/** @var \DateTime|null $dateReelle */
$dateReelle = empty($dataRencontre['datereelle']) ? null : \DateTime::createFromFormat('d/m/Y', $dataRencontre['datereelle']);

$result[] = new Rencontre(
$dataRencontre['libelle'],
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Service/FFTTClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php declare(strict_types=1);

namespace Alamirault\FFTTApi\Tests\Unit\Service;

use Alamirault\FFTTApi\Service\FFTTClient;
use Alamirault\FFTTApi\Service\UriGenerator;
use Alamirault\FFTTApi\Tests\Unit\Service\Operation\MockHandlerStub;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \Alamirault\FFTTApi\Service\FFTTClient
*/
final class FFTTClientTest extends TestCase
{
/**
* @covers ::get
* @covers ::send
*/
public function testGetWithAccent(): void
{
/** @var string $content */
$content = file_get_contents(__DIR__.'/fixtures/joueur_with_accent.xml');

$mock = new MockHandlerStub([
new Response(200, [], $content),
]);

$handlerStack = HandlerStack::create($mock);
$clientStub = new Client(['handler' => $handlerStack]);
$client = new FFTTClient($clientStub, new UriGenerator('foo', 'bar'));

/** @var array{licence: array<mixed>} $response */
$response = $client->get('bar');

$this->assertSame('Côme', $response['licence']['prenom']);
}
}
5 changes: 2 additions & 3 deletions tests/Unit/Service/NomPrenomExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Alamirault\FFTTApi\Tests\Unit\Service;

use Alamirault\FFTTApi\Service\NomPrenomExtractor;
use Generator;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -27,9 +26,9 @@ public function testExtractNomPrenom(string $raw, string $expectedNom, string $e
}

/**
* @return Generator<array<string>>
* @return \Generator<array<string>>
*/
public function getData(): Generator
public function getData(): \Generator
{
yield [
'MOREAU Véronique',
Expand Down
5 changes: 2 additions & 3 deletions tests/Unit/Service/Operation/ArrayWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Alamirault\FFTTApi\Tests\Unit\Service\Operation;

use Alamirault\FFTTApi\Service\Operation\ArrayWrapper;
use Generator;
use PHPUnit\Framework\TestCase;

/**
Expand All @@ -28,9 +27,9 @@ public function testWrapArrayIfUnique(array $raw, array $expected): void
}

/**
* @return Generator<array<mixed>>
* @return \Generator<array<mixed>>
*/
public function getData(): Generator
public function getData(): \Generator
{
yield 'Unique element' => [
[
Expand Down
Loading

0 comments on commit 7a772f5

Please sign in to comment.