Skip to content

Commit

Permalink
Add strictly typed responses; fix GET in extended models [#52]
Browse files Browse the repository at this point in the history
  • Loading branch information
dnl-blkv committed Sep 25, 2017
1 parent d96585d commit 00e223b
Show file tree
Hide file tree
Showing 204 changed files with 6,556 additions and 403 deletions.
39 changes: 12 additions & 27 deletions src/Model/BunqModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ abstract class BunqModel implements JsonSerializable
*/
const FIELD_RESPONSE = 'Response';
const FIELD_PAGINATION = 'Pagination';
const FIELD_ID = 'Id';
const FIELD_UUID = 'Uuid';

/**
* Regex constants.
Expand All @@ -36,9 +38,9 @@ abstract class BunqModel implements JsonSerializable
const REGEX_MATCH_RESULT_IS_ARRAY = 2;

/**
* Array count constants.
* Index of the very first item in an array.
*/
const COUNT_ONE = 1;
const INDEX_FIRST = 0;

/**
* Type constants.
Expand Down Expand Up @@ -223,43 +225,26 @@ protected static function classFromJson($class, BunqResponseRaw $responseRaw)
protected static function processForId(BunqResponseRaw $responseRaw)
{
/** @var Id $id */
$id = Id::fromJson($responseRaw)->getValue();
$id = Id::fromJson($responseRaw, self::FIELD_ID)->getValue();

return new BunqResponse($id->getId(), $responseRaw->getHeaders());
}

/**
* @param BunqResponseRaw $responseRaw
* @param string|null $wrapper
*
* @return BunqResponse
* @throws BunqException When the result is not expected.
*/
protected static function fromJson(BunqResponseRaw $responseRaw)
protected static function fromJson(BunqResponseRaw $responseRaw, string $wrapper = null)
{
$json = $responseRaw->getBodyString();
$response = ModelUtil::deserializeResponseArray($json)[self::FIELD_RESPONSE];
$bunqModelList = [];

foreach ($response as $modelPropertyArray) {
$value = self::determineJsonResponseValue($modelPropertyArray);
$bunqModelList[] = static::createFromResponseArray($value);
}

if (count($bunqModelList) === self::COUNT_ONE) {
return new BunqResponse(current($bunqModelList), $responseRaw->getHeaders());
} else {
throw new BunqException(self::ERROR_UNEXPECTED_RESULT, [count($bunqModelList)]);
}
}
$responseArray = ModelUtil::deserializeResponseArray($json);
$response = $responseArray[self::FIELD_RESPONSE];
$value = static::createListFromResponseArray($response, $wrapper);

/**
* @param mixed[] $responseArray
*
* @return mixed[]
*/
private static function determineJsonResponseValue(array $responseArray)
{
return current($responseArray);
return new BunqResponse($value[self::INDEX_FIRST], $responseRaw->getHeaders());
}

/**
Expand All @@ -270,7 +255,7 @@ private static function determineJsonResponseValue(array $responseArray)
protected static function processForUuid(BunqResponseRaw $responseRaw)
{
/** @var Uuid $uuid */
$uuid = Uuid::fromJson($responseRaw)->getValue();
$uuid = Uuid::fromJson($responseRaw, self::FIELD_UUID)->getValue();

return new BunqResponse($uuid->getUuid(), $responseRaw->getHeaders());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Model/Generated/AttachmentConversationContent.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Model/Generated/AttachmentMonetaryAccount.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Model/Generated/AttachmentPublic.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Model/Generated/AttachmentPublicContent.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Model/Generated/AttachmentTab.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Model/Generated/AttachmentTabContent.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Model/Generated/Avatar.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions src/Model/Generated/BillingContractSubscription.php

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 00e223b

Please sign in to comment.