Skip to content

Commit d5ec7ac

Browse files
committed
GetFareFamilyListParametersFactory added
1 parent 51cc153 commit d5ec7ac

2 files changed

Lines changed: 89 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TK\SDK\ValueObject\Factory;
5+
6+
use TK\SDK\Exception\InvalidArgumentException;
7+
use TK\SDK\ValueObject\GetFareFamilyListParameters;
8+
9+
class GetFareFamilyListParametersFactory implements ValueObjectFactoryInterface
10+
{
11+
/**
12+
* @param array $parameters
13+
* @return GetFareFamilyListParameters
14+
* @throws \Exception
15+
*/
16+
public static function createFromArray(array $parameters) : GetFareFamilyListParameters
17+
{
18+
$getFareFamilyListParameters = new GetFareFamilyListParameters();
19+
foreach ($parameters['portList'] as $airportIataCode) {
20+
$getFareFamilyListParameters->withAirportIataCode($airportIataCode);
21+
}
22+
if (array_key_exists('isMilesRequest', $parameters) && $parameters['isMilesRequest'] === 'T') {
23+
$getFareFamilyListParameters->withMilesRequest();
24+
}
25+
return $getFareFamilyListParameters;
26+
}
27+
28+
29+
/**
30+
* @param string $json
31+
* @return GetFareFamilyListParameters
32+
* @throws InvalidArgumentException
33+
* @throws \Exception
34+
*/
35+
public static function createFromJson(string $json) : GetFareFamilyListParameters
36+
{
37+
$parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
38+
if (json_last_error() !== JSON_ERROR_NONE) {
39+
throw new InvalidArgumentException(
40+
'GetFareFamilyListParametersFactory Error: ' . json_last_error_msg()
41+
);
42+
}
43+
return self::createFromArray($parameters);
44+
}
45+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TK\Test\Unit\Factory;
5+
6+
use TK\SDK\ValueObject\Factory\GetFareFamilyListParametersFactory;
7+
use TK\SDK\ValueObject\GetFareFamilyListParameters;
8+
9+
class GetFareFamilyListParametersFactoryTest extends \Codeception\Test\Unit
10+
{
11+
/**
12+
* @var \UnitTester
13+
*/
14+
protected $tester;
15+
16+
protected function _before()
17+
{
18+
}
19+
20+
protected function _after()
21+
{
22+
}
23+
24+
25+
/**
26+
* @test
27+
* @throws \Exception
28+
*/
29+
public function shouldReturnCalculateFlightMilesParameters() : void
30+
{
31+
$json =<<<JSON
32+
{
33+
"portList":[
34+
"IST",
35+
"JFK"
36+
],
37+
"isMilesRequest" : "T"
38+
}
39+
JSON;
40+
$parameterObject = GetFareFamilyListParametersFactory::createFromJson($json);
41+
$this->assertInstanceOf(GetFareFamilyListParameters::class, $parameterObject);
42+
$this->assertEquals(json_decode($json, true), $parameterObject->getValue());
43+
}
44+
}

0 commit comments

Comments
 (0)