Skip to content

Commit

Permalink
Merge pull request #16 from amvid/feat/phone-codes-endpoint
Browse files Browse the repository at this point in the history
feat: phone codes endpoint
  • Loading branch information
Vadims Filipovskis committed Nov 24, 2023
2 parents dbaae0b + a708405 commit ed9eb66
Show file tree
Hide file tree
Showing 11 changed files with 168 additions and 2 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,4 @@
"symfony/stopwatch": "6.3.*",
"symfony/web-profiler-bundle": "6.3.*"
}
}
}
32 changes: 32 additions & 0 deletions src/Country/Action/GetPhoneCodes/GetPhoneCodesAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Country\Action\GetPhoneCodes;

use App\Country\Repository\CountryRepositoryInterface;
use App\Currency\Exception\CurrencyNotFoundException;
use App\SubRegion\Exception\SubRegionNotFoundException;

readonly class GetPhoneCodesAction implements GetPhoneCodesActionInterface
{
public function __construct(private CountryRepositoryInterface $countryRepository)
{
}

/**
* @throws CurrencyNotFoundException
* @throws SubRegionNotFoundException
*/
public function run(GetPhoneCodesActionRequest $request): GetPhoneCodesActionResponse
{
return new GetPhoneCodesActionResponse(
$this->countryRepository->findPhoneCodes(
$request->offset,
$request->limit,
$request->title,
$request->phoneCode,
)
);
}
}
10 changes: 10 additions & 0 deletions src/Country/Action/GetPhoneCodes/GetPhoneCodesActionInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace App\Country\Action\GetPhoneCodes;

interface GetPhoneCodesActionInterface
{
public function run(GetPhoneCodesActionRequest $request): GetPhoneCodesActionResponse;
}
28 changes: 28 additions & 0 deletions src/Country/Action/GetPhoneCodes/GetPhoneCodesActionRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace App\Country\Action\GetPhoneCodes;

use App\Application\Controller\Request\LimitOffsetInterface;
use App\Application\Controller\Request\LimitOffsetParser;
use App\Application\Controller\Request\LimitOffsetRequestTrait;

class GetPhoneCodesActionRequest implements LimitOffsetInterface
{
use LimitOffsetRequestTrait;

public ?string $title = null;
public ?string $phoneCode = null;

public static function fromArray(array $params): self
{
/** @var GetPhoneCodesActionRequest $req */
$req = LimitOffsetParser::parse($params, new self());

$req->phoneCode = $params['phoneCode'] ?? null;
$req->title = $params['title'] ?? null;

return $req;
}
}
24 changes: 24 additions & 0 deletions src/Country/Action/GetPhoneCodes/GetPhoneCodesActionResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace App\Country\Action\GetPhoneCodes;

use App\Country\Controller\Response\PhoneCodeResponse;
use App\Country\Entity\Country;

class GetPhoneCodesActionResponse
{
/** @var array<PhoneCodeResponse> $response */
public array $response = [];

/**
* @param array<Country> $phoneCodes
*/
public function __construct(array $phoneCodes)
{
foreach ($phoneCodes as $phoneCode) {
$this->response[] = new PhoneCodeResponse($phoneCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
->setIso3($country['iso3'])
->setIso2($country['iso2'])
->setNumericCode($country['numeric_code'])
->setPhoneCode($country['phone_code'])
->setPhoneCode(str_replace(['+', '-'], '', $country['phone_code']))
->setNativeTitle($country['native'])
->setTld($country['tld'])
->setLongitude((float)$country['longitude'])
Expand Down
9 changes: 9 additions & 0 deletions src/Country/Controller/Api/V1/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use App\Country\Action\Delete\DeleteCountryActionRequest;
use App\Country\Action\Get\GetCountriesActionInterface;
use App\Country\Action\Get\GetCountriesActionRequest;
use App\Country\Action\GetPhoneCodes\GetPhoneCodesActionInterface;
use App\Country\Action\GetPhoneCodes\GetPhoneCodesActionRequest;
use App\Country\Action\Update\UpdateCountryActionInterface;
use App\Country\Action\Update\UpdateCountryActionRequest;
use JsonException;
Expand Down Expand Up @@ -68,4 +70,11 @@ public function update(string $id, Request $request, UpdateCountryActionInterfac

return $this->json($action->run($req)->countryResponse);
}

#[Route(self::API_ROUTE . '/phone-codes', name: 'app_country_api_v1_country_phone_codes', methods: HttpMethod::GET)]
public function getPhoneCodes(Request $request, GetPhoneCodesActionInterface $action): JsonResponse
{
$req = GetPhoneCodesActionRequest::fromArray($request->query->all());
return $this->json($action->run($req)->response);
}
}
2 changes: 2 additions & 0 deletions src/Country/Controller/Response/CountryResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Country\Controller\Response;

use App\Country\Entity\Country;
use App\Timezone\Entity\Timezone;
use App\Currency\Controller\Response\CurrencyResponse;
use App\SubRegion\Controller\Response\SubRegionResponse;
use App\Timezone\Controller\Response\TimezoneResponse;
Expand Down Expand Up @@ -54,6 +55,7 @@ public function __construct(Country $country, bool $withRelations = true)

$tzs = [];

/** @var Timezone $timezone */
foreach ($country->getTimezones() as $timezone) {
$tzs[] = new TimezoneResponse($timezone);
}
Expand Down
25 changes: 25 additions & 0 deletions src/Country/Controller/Response/PhoneCodeResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace App\Country\Controller\Response;

use App\Country\Entity\Country;

class PhoneCodeResponse
{
public string $title;
public string $iso2;
public string $iso3;
public string $phoneCode;
public string $flag;

public function __construct(Country $country)
{
$this->title = $country->getTitle();
$this->iso2 = $country->getIso2();
$this->iso3 = $country->getIso3();
$this->phoneCode = $country->getPhoneCode();
$this->flag = $country->getFlag();
}
}
29 changes: 29 additions & 0 deletions src/Country/Repository/CountryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,35 @@ public function findByIso2(string $iso2): ?Country
return $this->findOneBy(['iso2' => $iso2]);
}

public function findPhoneCodes(
int $offset,
int $limit,
?string $title,
?string $phoneCode,
): array {
$qb = $this->createQueryBuilder('c')
->setFirstResult($offset)
->setMaxResults($limit)
->where('1=1');

$params = [];

if ($title) {
$params['title'] = "$title%";
$qb->andWhere('c.title LIKE :title');
}

if ($phoneCode) {
$params['phoneCode'] = "$phoneCode%";
$qb->andWhere('c.phoneCode LIKE :phoneCode');
}

return $qb
->setParameters($params)
->getQuery()
->getResult();
}

public function list(
int $offset,
int $limit,
Expand Down
7 changes: 7 additions & 0 deletions src/Country/Repository/CountryRepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public function findByIso3(string $iso3): ?Country;

public function findByIso2(string $iso2): ?Country;

public function findPhoneCodes(
int $offset,
int $limit,
?string $title,
?string $phoneCode,
): array;

public function list(
int $offset,
int $limit,
Expand Down

0 comments on commit ed9eb66

Please sign in to comment.