Generate grid
This commit is contained in:
@@ -2,12 +2,12 @@
|
||||
|
||||
namespace App\Context\TMDB;
|
||||
|
||||
use App\Model\TMDB\TMDBActor;
|
||||
use App\Model\TMDB\TMDBMovieCredit;
|
||||
|
||||
class MovieCreditsContext
|
||||
{
|
||||
public function __construct(
|
||||
/** @var TMDBActor[] */
|
||||
/** @var TMDBMovieCredit[] */
|
||||
public array $cast { get => $this->cast; },
|
||||
) {}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Gateway\TMDBGateway;
|
||||
use App\Repository\ActorRepository;
|
||||
use App\Repository\MovieRepository;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
@@ -14,24 +15,60 @@ use Symfony\Component\Serializer\SerializerInterface;
|
||||
class HomepageController extends AbstractController
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MovieRepository $movieRepository,
|
||||
private readonly TMDBGateway $TMDBGateway,
|
||||
private readonly ActorRepository $actorRepository
|
||||
) {}
|
||||
|
||||
#[Route('/')]
|
||||
public function index(SerializerInterface $serializer): Response
|
||||
{
|
||||
$movie = $this->movieRepository->findOneBy([]);
|
||||
$creditsContext = $this->TMDBGateway->getMovieCredits($movie->getTmdbId());
|
||||
$cast = $creditsContext->cast;
|
||||
$actors = [];
|
||||
foreach ($cast as $actor) {
|
||||
if (2 <= $actor->popularity) {
|
||||
$actors[] = $actor;
|
||||
}
|
||||
}
|
||||
dd($actors);
|
||||
// Final actor to be guessed
|
||||
$mainActor = $this->actorRepository->findOneRandom(4);
|
||||
|
||||
return $this->render('homepage/index.html.twig');
|
||||
// Actors for the grid
|
||||
$actors = [];
|
||||
$leftSize = 0;
|
||||
$rightSize = 0;
|
||||
foreach (str_split(strtolower($mainActor->getName())) as $char) {
|
||||
if (!preg_match('/[a-z]/', $char)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$tryFindActor = 0;
|
||||
do {
|
||||
$actor = $this->actorRepository->findOneRandom(4, $char);
|
||||
++$tryFindActor;
|
||||
} while (
|
||||
$actor === $mainActor
|
||||
|| in_array($actor, array_map(fn ($actorMap) => $actorMap['actor'], $actors))
|
||||
|| $tryFindActor < 5
|
||||
);
|
||||
|
||||
$actorData = [
|
||||
'actor' => $actor,
|
||||
'pos' => strpos($actor->getName(), $char),
|
||||
];
|
||||
|
||||
if ($leftSize < $actorData['pos']) {
|
||||
$leftSize = $actorData['pos'];
|
||||
}
|
||||
|
||||
$rightSizeActor = strlen($actor->getName()) - $actorData['pos'] - 1;
|
||||
if ($rightSize < $rightSizeActor) {
|
||||
$rightSize = $rightSizeActor;
|
||||
}
|
||||
|
||||
$actors[] = $actorData;
|
||||
}
|
||||
|
||||
// Predict grid size
|
||||
$width = $rightSize + $leftSize + 1;
|
||||
$middle = $leftSize;
|
||||
|
||||
return $this->render('homepage/index.html.twig', [
|
||||
'mainActor' => $mainActor,
|
||||
'actors' => $actors,
|
||||
'width' => $width,
|
||||
'middle' => $middle,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
27
src/Doctrine/Extension/Random.php
Normal file
27
src/Doctrine/Extension/Random.php
Normal file
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Doctrine\Extension;
|
||||
|
||||
use Doctrine\ORM\Query\AST\Functions\FunctionNode;
|
||||
use Doctrine\ORM\Query\Parser;
|
||||
use Doctrine\ORM\Query\QueryException;
|
||||
use Doctrine\ORM\Query\SqlWalker;
|
||||
use Doctrine\ORM\Query\TokenType;
|
||||
|
||||
class Random extends FunctionNode
|
||||
{
|
||||
/**
|
||||
* @throws QueryException
|
||||
*/
|
||||
public function parse(Parser $parser): void
|
||||
{
|
||||
$parser->match(TokenType::T_IDENTIFIER);
|
||||
$parser->match(TokenType::T_OPEN_PARENTHESIS);
|
||||
$parser->match(TokenType::T_CLOSE_PARENTHESIS);
|
||||
}
|
||||
|
||||
public function getSql(SqlWalker $sqlWalker): string
|
||||
{
|
||||
return 'RANDOM()';
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Gateway;
|
||||
|
||||
use App\Context\TMDB\ActorCreditsContext;
|
||||
use App\Context\TMDB\MovieCreditsContext;
|
||||
use App\Context\TMDB\MovieSearchContext;
|
||||
use App\Exception\GatewayException;
|
||||
@@ -44,7 +45,7 @@ readonly class TMDBGateway
|
||||
*/
|
||||
public function getMovieCredits(int $movieId): ?MovieCreditsContext
|
||||
{
|
||||
$url = str_replace('{id}', $movieId, $this->host.self::MOVIE_CREDITS_URI);
|
||||
$url = $this->host.str_replace('{id}', $movieId, self::MOVIE_CREDITS_URI);
|
||||
return $this->fetchSerialized('GET', $url, MovieCreditsContext::class);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Model\TMDB;
|
||||
|
||||
class TMDBActor
|
||||
class TMDBMovieCredit
|
||||
{
|
||||
public function __construct(
|
||||
public int $id { get => $this->id; },
|
||||
@@ -16,28 +16,26 @@ class ActorRepository extends ServiceEntityRepository
|
||||
parent::__construct($registry, Actor::class);
|
||||
}
|
||||
|
||||
// /**
|
||||
// * @return Actor[] Returns an array of Actor objects
|
||||
// */
|
||||
// public function findByExampleField($value): array
|
||||
// {
|
||||
// return $this->createQueryBuilder('a')
|
||||
// ->andWhere('a.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->orderBy('a.id', 'ASC')
|
||||
// ->setMaxResults(10)
|
||||
// ->getQuery()
|
||||
// ->getResult()
|
||||
// ;
|
||||
// }
|
||||
public function findOneRandom(?float $popularity = null, ?string $char = null): Actor
|
||||
{
|
||||
$qb = $this->createQueryBuilder('o');
|
||||
$expr = $qb->expr();
|
||||
|
||||
// public function findOneBySomeField($value): ?Actor
|
||||
// {
|
||||
// return $this->createQueryBuilder('a')
|
||||
// ->andWhere('a.exampleField = :val')
|
||||
// ->setParameter('val', $value)
|
||||
// ->getQuery()
|
||||
// ->getOneOrNullResult()
|
||||
// ;
|
||||
// }
|
||||
if (!empty($popularity)) {
|
||||
$qb->andWhere($expr->gte('o.popularity', ':popularity'))
|
||||
->setParameter('popularity', $popularity);
|
||||
}
|
||||
|
||||
if (!empty($char)) {
|
||||
$qb->andWhere($expr->like('o.name', ':name'))
|
||||
->setParameter('name', '%'.$char.'%');
|
||||
}
|
||||
|
||||
return $qb
|
||||
->orderBy('RANDOM()')
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getOneOrNullResult()
|
||||
;
|
||||
}
|
||||
}
|
||||
|
||||
21
src/Twig/AppExtension.php
Normal file
21
src/Twig/AppExtension.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
public function getFilters(): array
|
||||
{
|
||||
return [
|
||||
new TwigFilter('match', [$this, 'match']),
|
||||
];
|
||||
}
|
||||
|
||||
public function match(string $string, string $pattern): bool
|
||||
{
|
||||
return preg_match($pattern, $string);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user