| @@ 16-66 (lines=51) @@ | ||
| 13 | ||
| 14 | use BenatEspina\StackExchangeApiClient\Model\AccessToken; |
|
| 15 | ||
| 16 | final class AccessTokenSerializer implements Serializer |
|
| 17 | { |
|
| 18 | /** |
|
| 19 | * The instance itself. |
|
| 20 | * |
|
| 21 | * @var self|null |
|
| 22 | */ |
|
| 23 | private static $instance; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Constructor in a singleton way. |
|
| 27 | * |
|
| 28 | * @return self |
|
| 29 | */ |
|
| 30 | public static function instance() |
|
| 31 | { |
|
| 32 | if (null === self::$instance) { |
|
| 33 | self::$instance = new self(); |
|
| 34 | } |
|
| 35 | ||
| 36 | return self::$instance; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * {@inheritdoc} |
|
| 41 | */ |
|
| 42 | public function serialize($data) |
|
| 43 | { |
|
| 44 | if (false === array_key_exists('items', $data)) { |
|
| 45 | throw new \Exception('Given data is incorrect'); |
|
| 46 | } |
|
| 47 | if (count($data['items']) > 1) { |
|
| 48 | $answers = []; |
|
| 49 | foreach ($data['items'] as $item) { |
|
| 50 | $answers[] = AccessToken::fromJson($item); |
|
| 51 | } |
|
| 52 | ||
| 53 | return $answers; |
|
| 54 | } |
|
| 55 | ||
| 56 | return AccessToken::fromJson($data['items'][0]); |
|
| 57 | } |
|
| 58 | ||
| 59 | /** |
|
| 60 | * Constructor. Avoids to use the "new" |
|
| 61 | * statement to instantiate the class. |
|
| 62 | */ |
|
| 63 | private function __construct() |
|
| 64 | { |
|
| 65 | } |
|
| 66 | } |
|
| 67 | ||
| @@ 16-66 (lines=51) @@ | ||
| 13 | ||
| 14 | use BenatEspina\StackExchangeApiClient\Model\Answer; |
|
| 15 | ||
| 16 | final class AnswerSerializer implements Serializer |
|
| 17 | { |
|
| 18 | /** |
|
| 19 | * The instance itself. |
|
| 20 | * |
|
| 21 | * @var self|null |
|
| 22 | */ |
|
| 23 | private static $instance; |
|
| 24 | ||
| 25 | /** |
|
| 26 | * Constructor in a singleton way. |
|
| 27 | * |
|
| 28 | * @return self |
|
| 29 | */ |
|
| 30 | public static function instance() |
|
| 31 | { |
|
| 32 | if (null === self::$instance) { |
|
| 33 | self::$instance = new self(); |
|
| 34 | } |
|
| 35 | ||
| 36 | return self::$instance; |
|
| 37 | } |
|
| 38 | ||
| 39 | /** |
|
| 40 | * {@inheritdoc} |
|
| 41 | */ |
|
| 42 | public function serialize($data) |
|
| 43 | { |
|
| 44 | if (false === array_key_exists('items', $data)) { |
|
| 45 | throw new \Exception('Given data is incorrect'); |
|
| 46 | } |
|
| 47 | if (count($data['items']) > 1) { |
|
| 48 | $answers = []; |
|
| 49 | foreach ($data['items'] as $item) { |
|
| 50 | $answers[] = Answer::fromJson($item); |
|
| 51 | } |
|
| 52 | ||
| 53 | return $answers; |
|
| 54 | } |
|
| 55 | ||
| 56 | return Answer::fromJson($data['items'][0]); |
|
| 57 | } |
|
| 58 | ||
| 59 | /** |
|
| 60 | * Constructor. Avoids to use the "new" |
|
| 61 | * statement to instantiate the class. |
|
| 62 | */ |
|
| 63 | private function __construct() |
|
| 64 | { |
|
| 65 | } |
|
| 66 | } |
|
| 67 | ||