File tree Expand file tree Collapse file tree
src/SDK/ValueObject/Factory Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments