Skip to content

Commit 01c0272

Browse files
committed
GetPortListParametersFactory added
1 parent 1c3bd69 commit 01c0272

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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\GetPortListParameters;
8+
9+
final class GetPortListParametersFactory implements ValueObjectFactoryInterface
10+
{
11+
/**
12+
* @param array $parameters
13+
* @return GetPortListParameters
14+
* @throws \Exception
15+
*/
16+
public static function createFromArray(array $parameters) : GetPortListParameters
17+
{
18+
$getPortListParameters = new GetPortListParameters($parameters['airlineCode']);
19+
if (array_key_exists('languageCode', $parameters)) {
20+
$getPortListParameters = $getPortListParameters->withLanguageCode($parameters['languageCode']);
21+
}
22+
return$getPortListParameters;
23+
}
24+
25+
26+
/**
27+
* @param string $json
28+
* @return GetPortListParameters
29+
* @throws InvalidArgumentException
30+
* @throws \Exception
31+
*/
32+
public static function createFromJson(string $json) : GetPortListParameters
33+
{
34+
$parameters = json_decode($json, (bool) JSON_OBJECT_AS_ARRAY);
35+
if (json_last_error() !== JSON_ERROR_NONE) {
36+
throw new InvalidArgumentException(
37+
'GetPortListParametersFactory Error: ' . json_last_error_msg()
38+
);
39+
}
40+
return self::createFromArray($parameters);
41+
}
42+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace TK\Test\Unit\Factory;
5+
6+
use TK\SDK\ValueObject\Factory\GetPortListParametersFactory;
7+
use TK\SDK\ValueObject\GetPortListParameters;
8+
9+
class GetPortListParametersFactoryTest 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+
* @test
26+
* @throws \Exception
27+
*/
28+
public function shouldReturnGetPortListParameters() : void
29+
{
30+
$json =<<<JSON
31+
{
32+
"airlineCode": "TK",
33+
"languageCode": "TR"
34+
}
35+
JSON;
36+
$parameterObject = GetPortListParametersFactory::createFromJson($json);
37+
$this->assertInstanceOf(GetPortListParameters::class, $parameterObject);
38+
$this->assertEquals(json_decode($json, true), $parameterObject->getValue());
39+
}
40+
}

0 commit comments

Comments
 (0)