Skip to content

Commit 23cd6b4

Browse files
committed
Add DateHelper
1 parent 63fb3e0 commit 23cd6b4

File tree

9 files changed

+186
-31
lines changed

9 files changed

+186
-31
lines changed

src/Drivers/BaseCurrencyDriver.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
<?php namespace Otherguy\Currency\Drivers;
22

3+
use DateInterval;
34
use DateTime;
5+
use DateTimeInterface;
6+
use Exception;
47
use GuzzleHttp\ClientInterface;
58
use GuzzleHttp\Exception\GuzzleException;
69
use Otherguy\Currency\Exceptions\ApiException;
10+
use Otherguy\Currency\Helpers\DateHelper;
711

812
/**
913
* Class BaseDriver
@@ -99,20 +103,19 @@ public function amount($amount): CurrencyDriverContract
99103
}
100104

101105
/**
102-
* @param int|string|DateTime $date
106+
* @param int|string|DateTime|DateInterval|DateTimeInterface $date
103107
*
104108
* @return self
109+
*
110+
* @throws Exception
105111
*/
106112
public function date($date): CurrencyDriverContract
107113
{
108-
if (is_integer($date)) {
109-
$this->date = date('Y-m-d', $date);
110-
} else if ($date instanceof DateTime) {
111-
$this->date = $date->format('Y-m-d');
112-
} else if (is_string($date)) {
113-
$this->date = date('Y-m-d', strtotime($date));
114+
if($date === null) {
115+
return $this;
114116
}
115117

118+
$this->date = DateHelper::format($date, 'Y-m-d');
116119
return $this;
117120
}
118121

src/Drivers/CurrencyLayer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,13 @@ public function historical($date = null, $forCurrency = []): ConversionResult
6868
$this->currencies((array)$forCurrency);
6969
}
7070

71+
if(null === $this->getDate() ) {
72+
throw new ApiException('Date needs to be set!');
73+
}
74+
7175
// Get API response
7276
$response = $this->apiRequest('historical', [
73-
'date' => $this->date,
77+
'date' => $this->getDate(),
7478
'source' => $this->getBaseCurrency(),
7579
'currencies' => join(',', $this->getSymbols()),
7680
]);

src/Drivers/FixerIo.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,12 @@ public function historical($date = null, $forCurrency = []): ConversionResult
5959
$this->currencies((array)$forCurrency);
6060
}
6161

62+
if(null === $this->getDate() ) {
63+
throw new ApiException('Date needs to be set!');
64+
}
65+
6266
// Get API response
63-
$response = $this->apiRequest($this->date, [
67+
$response = $this->apiRequest($this->getDate(), [
6468
'base' => $this->getBaseCurrency(),
6569
'symbols' => join(',', $this->getSymbols()),
6670
]);

src/Helpers/DateHelper.php

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php namespace Otherguy\Currency\Helpers;
2+
3+
use DateInterval;
4+
use DateTime;
5+
use DateTimeInterface;
6+
use Exception;
7+
8+
/**
9+
* Class DateHelper
10+
*
11+
* @package Otherguy\Currency\Helpers
12+
*/
13+
class DateHelper
14+
{
15+
16+
/**
17+
* Parse a date from string with a format to a DateTime object
18+
*
19+
* @param string $string
20+
* @param string $format
21+
*
22+
* @return DateTime
23+
*/
24+
public static function parse(string $string, string $format): DateTime
25+
{
26+
return DateTime::createFromFormat($format, $string);
27+
}
28+
29+
/**
30+
* Format a date (or interval) to a string with a given format
31+
*
32+
* See formatting options as in PHP date()
33+
*
34+
* @param int|string|DateTime|DateInterval|DateTimeInterface $date
35+
* @param string $format
36+
*
37+
* @return string
38+
*
39+
* @throws Exception
40+
*
41+
* @see date()
42+
*/
43+
public static function format($date, string $format): string
44+
{
45+
if ($date instanceof DateTime || $date instanceof DateTimeInterface || $date instanceof DateInterval) {
46+
return $date->format($format);
47+
} else if ($date === 'now') {
48+
return date($format);
49+
} else if (is_string($date)) {
50+
return (new DateTime($date))->format($format);
51+
} else {
52+
$timestamp = (integer)$date;
53+
return date($format, $timestamp);
54+
}
55+
}
56+
57+
58+
/**
59+
* Get a date object by given date or time format
60+
*
61+
* Examples::
62+
*
63+
* Date.create('2018-12-04')
64+
* Date.create('first day of next year')
65+
*
66+
* @param String $time A date/time string. For valid formats see http://php.net/manual/en/datetime.formats.php
67+
*
68+
* @return DateTime
69+
*
70+
* @throws Exception
71+
*/
72+
public static function create(string $time): DateTime
73+
{
74+
return new DateTime($time);
75+
}
76+
77+
/**
78+
* Get the current date and time
79+
*
80+
* Examples::
81+
*
82+
* Date.now().timestamp
83+
*
84+
* @return DateTime
85+
*
86+
* @throws Exception
87+
*/
88+
public static function now(): DateTime
89+
{
90+
return new DateTime('now');
91+
}
92+
93+
/**
94+
* Get the current date
95+
*
96+
* @return DateTime
97+
*
98+
* @throws Exception
99+
*/
100+
public static function today(): DateTime
101+
{
102+
return new DateTime('today');
103+
}
104+
}

src/Results/ConversionResult.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php namespace Otherguy\Currency\Results;
22

33
use DateTime;
4+
use Exception;
45
use Otherguy\Currency\Exceptions\CurrencyException;
6+
use Otherguy\Currency\Helpers\DateHelper;
57

68
/**
79
* Class ConversionResult
@@ -14,7 +16,7 @@ class ConversionResult
1416
private $originalBaseCurrency = [];
1517

1618
protected $baseCurrency;
17-
protected $timestamp;
19+
protected $date;
1820
protected $conversionRates = [];
1921

2022

@@ -24,23 +26,15 @@ class ConversionResult
2426
* @param string $baseCurrency
2527
* @param int|DateTime|string $date
2628
* @param array $rates
29+
*
30+
* @throws Exception
2731
*/
2832
public function __construct(string $baseCurrency, $date, array $rates)
2933
{
3034
$this->originalBaseCurrency = $baseCurrency;
3135
$this->baseCurrency = $baseCurrency;
3236

33-
if (is_integer($date)) {
34-
$this->timestamp = $date;
35-
} else {
36-
if ($date instanceof DateTime) {
37-
$this->timestamp = $date->getTimestamp();
38-
} else {
39-
if (is_string($date)) {
40-
$this->timestamp = strtotime($date);
41-
}
42-
}
43-
}
37+
$this->date = DateHelper::format($date, 'Y-m-d');
4438

4539
$rates[$baseCurrency] = 1.0;
4640

@@ -97,15 +91,7 @@ public function setBaseCurrency(string $baseCurrency): ConversionResult
9791
*/
9892
public function getDate()
9993
{
100-
return date('Y-m-d', $this->timestamp);
101-
}
102-
103-
/**
104-
* Get timestamp.
105-
*/
106-
public function getTimestamp()
107-
{
108-
return $this->timestamp;
94+
return $this->date;
10995
}
11096

11197
/**

tests/Drivers/CurrencyLayerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ public function can_get_historical_rates()
6767
$this->assertEquals(1.293878, $result->rate(Symbol::AUD));
6868
}
6969

70+
/** @test */
71+
public function fails_to_get_historical_rates_if_date_not_set()
72+
{
73+
$this->expectException(ApiException::class);
74+
$this->currencyLayer->from(Symbol::USD)->to(Symbol::EUR)->historical();
75+
}
76+
7077
/** @test */
7178
public function can_convert_currency_amounts()
7279
{

tests/Drivers/FixerIoTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ public function can_get_historical_rates()
6262
$this->assertEquals(1.739516, $result->rate(Symbol::CAD));
6363
}
6464

65+
/** @test */
66+
public function fails_to_get_historical_rates_if_date_not_set()
67+
{
68+
$this->expectException(ApiException::class);
69+
$this->fixerIo->from(Symbol::USD)->to(Symbol::EUR)->historical();
70+
}
71+
6572
/** @test */
6673
public function can_convert_currency_amounts()
6774
{

tests/Helpers/DateHelperTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
use Otherguy\Currency\Helpers\DateHelper;
4+
use PHPUnit\Framework\TestCase;
5+
6+
class DateHelperTest extends TestCase
7+
{
8+
9+
/** @test */
10+
public function can_parse_a_date()
11+
{
12+
$this->assertEquals('23:15:03', DateHelper::parse('23h 15m 03s', 'H\h i\m s\s',)->format('H:i:s'));
13+
}
14+
15+
/** @test */
16+
public function can_format_a_date()
17+
{
18+
$this->assertEqualsWithDelta((new DateTime())->format('Y-m-d'), DateHelper::format('now', 'Y-m-d'), 0.1);
19+
$this->assertEquals('2019-01-01', DateHelper::format(1546300800, 'Y-m-d'));
20+
$this->assertEquals('2019-01-01', DateHelper::format('2019-01-01', 'Y-m-d'));
21+
$this->assertEquals(DateHelper::today()->format('Y-m-d'), DateHelper::format(DateHelper::today(), 'Y-m-d'));
22+
}
23+
24+
/** @test */
25+
public function can_create_a_date()
26+
{
27+
$this->assertEquals(1546300800, DateHelper::create('1.1.2019')->getTimestamp());
28+
}
29+
30+
/** @test */
31+
public function can_get_current_date_and_time()
32+
{
33+
$this->assertEqualsWithDelta(new DateTime(), DateHelper::now(), 0.1);
34+
}
35+
36+
/** @test */
37+
public function can_get_current_date()
38+
{
39+
$this->assertEquals(new DateTime('today'), DateHelper::today());
40+
}
41+
}

tests/Results/ConversionResultTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ protected function setUp()
2828
public function construct_will_properly_set_parameters()
2929
{
3030
$this->assertEquals(Symbol::USD, $this->classUnderTest->getBaseCurrency());
31-
$this->assertEquals(1560293762, $this->classUnderTest->getTimestamp());
3231
$this->assertEquals('2019-06-11', $this->classUnderTest->getDate());
3332

3433
$result = new ConversionResult(Symbol::USD, '1936-07-21', [

0 commit comments

Comments
 (0)