From 1188f141275bac2b782f9823f9a100e6c7adf9a5 Mon Sep 17 00:00:00 2001 From: peter279k Date: Fri, 1 Jun 2018 00:14:47 +0800 Subject: [PATCH] Approve the code review --- example.php | 24 ++++------------- src/Collection/AllRatesRecord.php | 31 +++++++-------------- src/Collection/SpecificRateRecord.php | 39 ++++++--------------------- src/Resource/GetAllRate.php | 24 ++++++++--------- src/Resource/GetSpecificRate.php | 17 +++++++----- tests/Functional/GetRateTest.php | 29 ++++++++++---------- 6 files changed, 58 insertions(+), 106 deletions(-) diff --git a/example.php b/example.php index 2f25253..331b426 100644 --- a/example.php +++ b/example.php @@ -24,17 +24,9 @@ $rates = $porter->import(new ImportSpecification($specificRate)) ->findFirstCollection(); -echo $rates->getBase(); -echo PHP_EOL; +$rateRecords = $rates->toAssociativeArray(); -echo $rates->getQuote(); -echo PHP_EOL; - -echo $rates->getTime(); -echo PHP_EOL; - -echo $rates->getRate(); -echo PHP_EOL; +var_dump($rateRecords); // Get all rates @@ -47,14 +39,8 @@ $rates = $porter->import(new ImportSpecification($specificRate)) ->findFirstCollection(); -echo $rates->getBase(); // BTC -echo PHP_EOL; - -var_dump($rates->getQuote()); // array -echo PHP_EOL; +$rateRecords = $rates->toAssociativeArray(); -var_dump($rates->getTime()); // array -echo PHP_EOL; +echo $rates->getBase() . PHP_EOL; -var_dump($rates->getRate()); // rate -echo PHP_EOL; +var_dump($rateRecords); diff --git a/src/Collection/AllRatesRecord.php b/src/Collection/AllRatesRecord.php index e1d93a2..0364afb 100644 --- a/src/Collection/AllRatesRecord.php +++ b/src/Collection/AllRatesRecord.php @@ -10,18 +10,15 @@ class AllRatesRecord extends CountableProviderRecords { private $base; - private $time; + public function __construct( + \Iterator $providerRecords, + string $base, + int $count, + ProviderResource $resource + ) { + parent::__construct($providerRecords, $count, $resource); - private $quote; - - private $rate; - - public function __construct(array $time, string $base, array $quote, array $rate) - { $this->base = $base; - $this->time = $time; - $this->quote = $quote; - $this->rate = $rate; } public function getBase(): string @@ -29,18 +26,8 @@ public function getBase(): string return $this->base; } - public function getTime(): array - { - return $this->time; - } - - public function getQuote(): array - { - return $this->quote; - } - - public function getRate(): array + public function toAssociativeArray(): array { - return $this->rate; + return iterator_to_array($this); } } diff --git a/src/Collection/SpecificRateRecord.php b/src/Collection/SpecificRateRecord.php index 359d3c1..6ede4b7 100644 --- a/src/Collection/SpecificRateRecord.php +++ b/src/Collection/SpecificRateRecord.php @@ -8,39 +8,16 @@ class SpecificRateRecord extends CountableProviderRecords { - private $base; - - private $time; - - private $quote; - - private $rate; - - public function __construct(string $time, string $base, string $quote, float $rate) - { - $this->base = $base; - $this->time = $time; - $this->quote = $quote; - $this->rate = $rate; - } - - public function getBase(): string - { - return $this->base; - } - - public function getTime(): string - { - return $this->time; - } - - public function getQuote(): string - { - return $this->quote; + public function __construct( + \Iterator $providerRecords, + int $count, + ProviderResource $resource + ) { + parent::__construct($providerRecords, $count, $resource); } - public function getRate(): float + public function toAssociativeArray(): array { - return $this->rate; + return iterator_to_array($this); } } diff --git a/src/Resource/GetAllRate.php b/src/Resource/GetAllRate.php index c5848c3..4f54fcc 100644 --- a/src/Resource/GetAllRate.php +++ b/src/Resource/GetAllRate.php @@ -38,17 +38,17 @@ public function fetch(ImportConnector $connector): \Iterator ); $rates = $response['rates']; - $base = $response['asset_id_base']; - $quote = []; - $rate = []; - $time = []; - - foreach($rates as $key => $value) { - $quote[] = $rates[$key]['asset_id_quote']; - $rate[] = $rates[$key]['rate']; - $time[] = $rates[$key]['time']; - } - - return new AllRatesRecord($time, $base, $quote, $rate); + + $rate = function () use ($rates) { + foreach ($rates as $key => $value) { + yield [ + 'asset_id_quote' => $rates[$key]['asset_id_quote'], + 'rate' => $rates[$key]['rate'], + 'time' => $rates[$key]['time'], + ]; + } + }; + + return new AllRatesRecord($rate(), $response['asset_id_base'], count($rates), $this); } } diff --git a/src/Resource/GetSpecificRate.php b/src/Resource/GetSpecificRate.php index 7fc3be7..a425d19 100644 --- a/src/Resource/GetSpecificRate.php +++ b/src/Resource/GetSpecificRate.php @@ -39,12 +39,15 @@ public function fetch(ImportConnector $connector): \Iterator true ); - $base = $response['asset_id_base']; - - $quote = $response['asset_id_quote']; - $rate = $response['rate']; - $time = $response['time']; - - return new SpecificRateRecord($time, $base, $quote, $rate); + $rate = function () use ($response) { + yield [ + 'asset_id_base' => $response['asset_id_base'], + 'asset_id_quote' => $response['asset_id_quote'], + 'rate' => $response['rate'], + 'time' => $response['time'], + ]; + }; + + return new SpecificRateRecord($rate(), count($response), $this); } } diff --git a/tests/Functional/GetRateTest.php b/tests/Functional/GetRateTest.php index ece4aa5..aa03d73 100644 --- a/tests/Functional/GetRateTest.php +++ b/tests/Functional/GetRateTest.php @@ -21,10 +21,13 @@ public function testGetSpecificRateRecords() $rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetSpecificRate($this->apiKey))) ->findFirstCollection(); - $this->assertSame('BTC', $rates->getBase()); - $this->assertSame('USD', $rates->getQuote()); - $this->assertLessThanOrEqual(time(), strtotime($rates->getTime())); - $this->assertInternalType('float', $rates->getRate()); + $rateRecords = $rates->toAssociativeArray(); + + $this->assertCount(1, $rateRecords); + $this->assertArrayHasKey('asset_id_base', $rateRecords[0]); + $this->assertArrayHasKey('asset_id_quote', $rateRecords[0]); + $this->assertArrayHasKey('rate', $rateRecords[0]); + $this->assertArrayHasKey('time', $rateRecords[0]); } public function testGetAllRateRecords() @@ -33,16 +36,12 @@ public function testGetAllRateRecords() $rates = FixtureFactory::createPorter()->import(new ImportSpecification(new GetAllRate($this->apiKey))) ->findFirstCollection(); - $base = $rates->getBase(); - $quote = $rates->getQuote(); - $time = $rates->getTime(); - $rate = $rates->getRate(); - - $this->assertSame('BTC', $base); - $this->assertContains('USD', $quote); - $this->assertContains('GBP', $quote); - $this->assertContains('EUR', $quote); - $this->assertLessThanOrEqual(time(), strtotime($time[0])); - $this->assertInternalType('float', $rate[0]); + $this->assertSame('BTC', $rates->getBase()); + + $rateRecords = $rates->toAssociativeArray(); + + $this->assertArrayHasKey('asset_id_quote', $rateRecords[0]); + $this->assertArrayHasKey('rate', $rateRecords[0]); + $this->assertArrayHasKey('time', $rateRecords[0]); } }