Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 26 additions & 22 deletions src/Model/Factory/RencontreDetailsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Alamirault\FFTTApi\Model\Factory;

use Accentuation\Accentuation;
use Alamirault\FFTTApi\Model\Rencontre\Joueur;
use Alamirault\FFTTApi\Model\Rencontre\Partie;
use Alamirault\FFTTApi\Model\Rencontre\RencontreDetails;
Expand All @@ -29,11 +28,14 @@ public function createFromArray(array $array, string $clubEquipeA, string $clubE
$joueursA = [];
$joueursB = [];
foreach ($array['joueur'] as $joueur) {
$joueursA[] = [$joueur['xja'] ?: '', $joueur['xca'] ?: ''];
$joueursB[] = [$joueur['xjb'] ?: '', $joueur['xcb'] ?: ''];
$joueursA[] = ['nom' => $joueur['xja'] ?: '', 'points' => $joueur['xca'] ?: ''];
$joueursB[] = ['nom' => $joueur['xjb'] ?: '', 'points' => $joueur['xcb'] ?: ''];
}
$joueursAFormatted = $this->formatJoueurs($joueursA, $clubEquipeA);
$joueursBFormatted = $this->formatJoueurs($joueursB, $clubEquipeB);

$wholeTeamAForfeit = 0 === count(array_filter($joueursA, function ($joueurA) { return $joueurA['nom'] && $joueurA['points']; }));
$wholeTeamBForfeit = 0 === count(array_filter($joueursB, function ($joueurB) { return $joueurB['nom'] && $joueurB['points']; }));
$joueursAFormatted = !$wholeTeamAForfeit ? $this->formatJoueurs($joueursA, $clubEquipeA) : [];
$joueursBFormatted = !$wholeTeamBForfeit ? $this->formatJoueurs($joueursB, $clubEquipeB) : [];
Comment on lines 30 to +38
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WDYT of this instead ?

        $wholeTeamAForfeit = true;
        $wholeTeamBForfeit = true;
        foreach ($array['joueur'] as $joueur) {
            $nomJoueurA = $joueur['xja'];
            $pointsJoueurA = $joueur['xca'];

            if($nomJoueurA && $pointsJoueurA){
                $wholeTeamAForfeit = false;
            }

            $nomJoueurB = $joueur['xjb'];
            $pointsJoueurB = $joueur['xcb'];

            if($nomJoueurB && $pointsJoueurB){
                $wholeTeamBForfeit = false;
            }

            $joueursA[] = ['nom' => $nomJoueurA, 'points' =>  $pointsJoueurA];
            $joueursB[] = ['nom' => $nomJoueurB, 'points' => $pointsJoueurB];
        }


$parties = $this->getParties($array['partie']);

Expand Down Expand Up @@ -129,7 +131,7 @@ private function getScores(array $parties): array
}

/**
* @param array<array{0: string, 1: string}> $data
* @param array<array{nom: string, points: string}> $data
*
* @return array<string, Joueur>
*/
Expand All @@ -139,9 +141,11 @@ private function formatJoueurs(array $data, string $playerClubId): array

$joueurs = [];
foreach ($data as $joueurData) {
$nomPrenom = $joueurData[0];
[$nom, $prenom] = $this->nomPrenomExtractor->extractNomPrenom($nomPrenom);
$joueurs[$this->nomPrenomExtractor->removeSeparatorsDuplication($nomPrenom)] = $this->formatJoueur($prenom, $nom, $joueurData[1], $joueursClub);
if ($joueurData['nom'] && $joueurData['points']) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So in this way, we can have 4 players in left, and 3 players in right, is not a problem ?

Historically I wanted always same number of players on left and right

$nomPrenom = $joueurData['nom'];
[$nom, $prenom] = $this->nomPrenomExtractor->extractNomPrenom($nomPrenom);
$joueurs[$this->nomPrenomExtractor->removeSeparatorsDuplication($nomPrenom)] = $this->formatJoueur($prenom, $nom, $joueurData['points'], $joueursClub);
}
}

return $joueurs;
Expand All @@ -157,9 +161,7 @@ private function formatJoueur(string $prenom, string $nom, string $points, array
}

foreach ($joueursClub as $joueurClub) {
$nomJoueurClub = $this->nomPrenomExtractor->removeSeparatorsDuplication($joueurClub->getNom());
$prenomJoueurClub = $this->nomPrenomExtractor->removeSeparatorsDuplication($joueurClub->getPrenom());
if ($nomJoueurClub === Accentuation::remove($nom) && $prenomJoueurClub === $prenom) {
if ($joueurClub->getNom() === $nom && $joueurClub->getPrenom() === $prenom) {
$return = preg_match('/^(N°[0-9]*- ){0,1}(?<sexe>[A-Z]{1}) (?<points>[0-9]+)pts$/', $points, $result);

if (false === $return) {
Expand All @@ -169,8 +171,8 @@ private function formatJoueur(string $prenom, string $nom, string $points, array
$playerPoints = $result['points'];

return new Joueur(
$nomJoueurClub,
$prenomJoueurClub,
$joueurClub->getNom(),
$joueurClub->getPrenom(),
$joueurClub->getLicence(),
(int) $playerPoints,
$sexe
Expand All @@ -190,20 +192,22 @@ private function getParties(array $data): array
{
$parties = [];
foreach ($data as $partieData) {
$setDetails = explode(' ', $partieData['detail']);
$setsDetails = array_map(function ($setDetail) {
return intval($setDetail);
}, explode(' ', trim($partieData['detail'])));
Comment on lines +195 to +197
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like that ?

Suggested change
$setsDetails = array_map(function ($setDetail) {
return intval($setDetail);
}, explode(' ', trim($partieData['detail'])));
$setsDetails = array_map(fn (string $setDetail): int => (int) $setDetail, explode(' ', trim($partieData['detail'])));


/** @var string $adverssaireA */
$adverssaireA = is_array($partieData['ja']) ? 'Absent Absent' : $this->nomPrenomExtractor->removeSeparatorsDuplication($partieData['ja']);
/** @var string $adversaireA */
$adversaireA = is_array($partieData['ja']) ? 'Absent Absent' : $this->nomPrenomExtractor->removeSeparatorsDuplication($partieData['ja']);

/** @var string $adverssaireB */
$adverssaireB = is_array($partieData['jb']) ? 'Absent Absent' : $this->nomPrenomExtractor->removeSeparatorsDuplication($partieData['jb']);
/** @var string $adversaireB */
$adversaireB = is_array($partieData['jb']) ? 'Absent Absent' : $this->nomPrenomExtractor->removeSeparatorsDuplication($partieData['jb']);

$parties[] = new Partie(
$adverssaireA,
$adverssaireB,
$adversaireA,
$adversaireB,
'-' === $partieData['scorea'] ? 0 : (int) $partieData['scorea'],
'-' === $partieData['scoreb'] ? 0 : (int) $partieData['scoreb'],
$setDetails
$setsDetails
);
}

Expand Down
20 changes: 17 additions & 3 deletions src/Model/Joueur.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
final class Joueur
{
/**
* @param string|null $points Points du joueur ou classement si classé dans les 1000 premiers français
* @param int|null $points Points du joueur ou classement si classé dans les 1000 premiers français
* @param string|null $echelon Egal à 'N' si classé dans les 1000 premiers français, sinon null
* @param int|null $place Classement national si classé dans les 1000 premiers français
*/
public function __construct(
private readonly string $licence,
private readonly string $clubId,
private readonly string $club,
private readonly string $nom,
private readonly string $prenom,
private readonly ?string $points,
private readonly ?int $points,
private readonly ?string $echelon = null,
private readonly ?int $place = null,
) {}

public function getLicence(): string
Expand Down Expand Up @@ -41,8 +45,18 @@ public function getPrenom(): string
return $this->prenom;
}

public function getPoints(): ?string
public function getPoints(): ?int
{
return $this->points;
}

public function getEchelon(): ?string
{
return $this->echelon;
}

public function getPlace(): ?int
{
return $this->place;
}
}
10 changes: 5 additions & 5 deletions src/Model/Rencontre/Partie.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
final class Partie
{
/**
* @param array<string> $setDetails
* @param array<int> $setsDetails
*/
public function __construct(
private readonly string $adversaireA,
private readonly string $adversaireB,
private readonly int $scoreA,
private readonly int $scoreB,
private readonly array $setDetails,
private readonly array $setsDetails,
) {}

public function getAdversaireA(): string
Expand All @@ -36,10 +36,10 @@ public function getScoreB(): int
}

/**
* @return array<string>
* @return array<int>
*/
public function getSetDetails(): array
public function getSetsDetails(): array
{
return $this->setDetails;
return $this->setsDetails;
}
}
4 changes: 2 additions & 2 deletions src/Service/FFTTApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ public function __construct(string $id, string $password)
$clubFactory = new ClubFactory();
$this->listClubOperation = new ListClubOperation($FFTTClient, $clubFactory, $arrayWrapper);

$nomPrenomExtractor = new NomPrenomExtractor();
$this->retrieveClubDetailsOperation = new RetrieveClubDetailsOperation($FFTTClient, $this->listClubOperation);
$this->listJoueurOperation = new ListJoueurOperation($FFTTClient, $arrayWrapper);
$this->listJoueurOperation = new ListJoueurOperation($FFTTClient, $arrayWrapper, $nomPrenomExtractor);
$this->retrieveJoueurDetailsOperation = new RetrieveJoueurDetailsOperation($FFTTClient);
$this->retrieveClassementOperation = new RetrieveClassementOperation($FFTTClient);
$this->listHistoriqueOperation = new ListHistoriqueOperation($FFTTClient, $arrayWrapper);

$nomPrenomExtractor = new NomPrenomExtractor();
$this->listPartieOperation = new ListPartieOperation($FFTTClient, $arrayWrapper, $nomPrenomExtractor);

$pointCalculator = new PointCalculator();
Expand Down
4 changes: 2 additions & 2 deletions src/Service/NomPrenomExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public function extractNomPrenom(string $raw): array
{
$raw = $this->removeSeparatorsDuplication($raw);
// On extrait le nom et le prénom
$return = preg_match("/^(?<nom>[A-ZÀ-Ý]+(?:(?:[\s'\-])*[A-ZÀ-Ý]+)*)\s(?<prenom>[A-ZÀ-Ý][a-zà-ÿ]*(?:(?:[\s'\-])*[A-ZÀ-Ý][a-zà-ÿ]*)*)$/", $raw, $result);
$return = preg_match("/^(?<nom>[A-ZÀ-Ý]+(?:(?:[\s'\-])*[A-ZÀ-Ý]+)*)\s(?<prenom>[A-ZÀ-Ý][a-zà-ÿ]*(?:(?:[\s'\-])*[A-ZÀ-Ý]?[a-zà-ÿ]*)*)$/", $raw, $result);

return 1 !== $return ? ['', ''] :
[
Expand All @@ -22,6 +22,6 @@ public function extractNomPrenom(string $raw): array
*/
public function removeSeparatorsDuplication(string $raw): string
{
return preg_replace(['/\s+/', '/-+/'], [' ', '-'], $raw) ?? '';
return preg_replace(['/\s+/', '/(?:\s*\-\s*)+|-+/'], [' ', '-'], $raw) ?? '';
}
}
15 changes: 11 additions & 4 deletions src/Service/Operation/ListJoueurOperation.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
use Alamirault\FFTTApi\Exception\InvalidResponseException;
use Alamirault\FFTTApi\Model\Joueur;
use Alamirault\FFTTApi\Service\FFTTClientInterface;
use Alamirault\FFTTApi\Service\NomPrenomExtractorInterface;

final class ListJoueurOperation
{
public function __construct(
private readonly FFTTClientInterface $client,
private readonly ArrayWrapper $arrayWrapper,
private readonly NomPrenomExtractorInterface $nomPrenomExtractor,
) {}

/**
Expand All @@ -36,9 +38,12 @@ public function listJoueursByClub(string $clubId): array
$joueur['licence'],
$joueur['nclub'],
$joueur['club'],
$joueur['nom'],
$joueur['prenom'],
$joueur['points']);
$this->nomPrenomExtractor->removeSeparatorsDuplication(trim($joueur['nom'])),
$this->nomPrenomExtractor->removeSeparatorsDuplication(trim($joueur['prenom'])),
!is_array($joueur['points']) ? (int) $joueur['points'] : null,
!is_array($joueur['echelon']) ? $joueur['echelon'] : null,
!is_array($joueur['place']) ? (int) $joueur['place'] : null,
);
$result[] = $realJoueur;
}

Expand Down Expand Up @@ -69,7 +74,9 @@ public function listJoueursByNom(string $nom, string $prenom = ''): array
$joueur['club'],
$joueur['nom'],
$joueur['prenom'],
$joueur['clast']);
$joueur['clast'] ? (int) $joueur['clast'] : null,
null,
null, );
$result[] = $realJoueur;
}

Expand Down
24 changes: 18 additions & 6 deletions tests/Unit/Service/NomPrenomExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,20 @@ public function getData(): \Generator

yield [
'GARBANI - LECOURT Dimitri',
'GARBANI - LECOURT',
'GARBANI-LECOURT',
'Dimitri',
];

yield [
'GARBANI - LECOURT NEVEU Dimitri-Sébastien',
'GARBANI - LECOURT NEVEU',
'GARBANI-LECOURT NEVEU',
'Dimitri-Sébastien',
];

yield [
'GARBANI - LECOURT NEVEU Dimitri - Sébastien',
'GARBANI - LECOURT NEVEU',
'Dimitri - Sébastien',
'GARBANI-LECOURT NEVEU',
'Dimitri-Sébastien',
];

yield [
Expand All @@ -86,7 +86,7 @@ public function getData(): \Generator

yield [
'ÆYAO -- ÆYAOÈ Attien Henri',
'ÆYAO - ÆYAOÈ',
'ÆYAO-ÆYAOÈ',
'Attien Henri',
];

Expand Down Expand Up @@ -122,8 +122,20 @@ public function getData(): \Generator

yield [
"DE L'EPREVIER PAVRON - SEC Domitille",
"DE L'EPREVIER PAVRON - SEC",
"DE L'EPREVIER PAVRON-SEC",
'Domitille',
];

yield [
'SÉJOURNÉ Jérémy',
'SÉJOURNÉ',
'Jérémy',
];

yield [
'EVEN Gwec hen',
'EVEN',
'Gwec hen',
];
}
}
52 changes: 52 additions & 0 deletions tests/Unit/Service/Operation/ListJoueurOperationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php declare(strict_types=1);

namespace Alamirault\FFTTApi\Tests\Unit\Service\Operation;

use Alamirault\FFTTApi\Model\Joueur;
use Alamirault\FFTTApi\Service\FFTTClient;
use Alamirault\FFTTApi\Service\NomPrenomExtractor;
use Alamirault\FFTTApi\Service\Operation\ArrayWrapper;
use Alamirault\FFTTApi\Service\Operation\ListJoueurOperation;
use Alamirault\FFTTApi\Service\UriGenerator;
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;
use PHPUnit\Framework\TestCase;

/**
* @coversDefaultClass \Alamirault\FFTTApi\Service\Operation\ListJoueurOperation
*/
final class ListJoueurOperationTest extends TestCase
{
/**
* @covers ::listJoueursByClub
* This covers the route 'xml_liste_joueur_o'
*/
public function testListJoueursByClub(): void
{
/** @var string $responseContent */
$responseContent = file_get_contents(__DIR__.'/../fixtures/ListJoueurOperationTest/liste_joueurs_xml_liste_joueur_o.xml');
$mock = new MockHandlerStub([
new Response(200, [
'content-type' => ['text/html; charset=UTF-8'],
], $responseContent),
]);

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

$operation = new ListJoueurOperation($FFTTClient, new ArrayWrapper(), new NomPrenomExtractor());

$result = $operation->listJoueursByClub('05650412');

$this->assertCount(7, $result);
$this->assertEquals(new Joueur('9521866', 'CERGY PONTOISE', '05650412', 'HERNANDEZ', 'Francois', 816, null, null), $result[0]);
$this->assertEquals(new Joueur('9521899', 'CERGY PONTOISE', '05650412', "DE L'EPREVIER PAVRON-SEC", 'Domìtïlle', 812, null, null), $result[1]);
$this->assertEquals(new Joueur('9521877', 'CERGY PONTOISE', '05650412', 'NDJOM BASSANAGA', 'Moïse Çéliñó', 1216, null, null), $result[2]);
$this->assertEquals(new Joueur('95218367', 'CERGY PONTOISE', '05650412', 'ÂBBÄS', 'Ãbdel-Jælil', 1456, 'N', 924), $result[3]);
$this->assertEquals(new Joueur('9521870', 'CERGY PONTOISE', '05650412', 'GARBANI-LECOURT NEVEU', 'Dimitri-Sébastien', 1456, null, null), $result[4]);
$this->assertEquals(new Joueur('9521876', 'CERGY PONTOISE', '05650412', 'AMOR QUOINTEAU', 'Erwan', 756, null, null), $result[5]);
$this->assertEquals(new Joueur('9521888', 'CERGY PONTOISE', '05650412', 'MOREAU', 'Véronique', 1956, 'N', 54), $result[6]);
}
}
Loading