Skip to content

Commit 634cc9c

Browse files
authored
Fixes #180; reference birth or death days as YEAR-01-01 instead of using today. (#227)
1 parent b6c810b commit 634cc9c

File tree

3 files changed

+44
-5
lines changed

3 files changed

+44
-5
lines changed

lib/Tmdb/Model/Person.php

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,22 @@ public function getBirthday()
228228
*/
229229
public function setBirthday($birthday)
230230
{
231-
if (!$birthday instanceof DateTime) {
232-
$birthday = new DateTime($birthday);
231+
if (!$birthday instanceof DateTime && !empty($birthday)) {
232+
if (ctype_digit($birthday) && strlen(4)) {
233+
$birthday = DateTime::createFromFormat(
234+
'Y-m-d',
235+
sprintf('%d-01-01', $birthday),
236+
new \DateTimeZone('UTC')
237+
);
238+
} elseif (strtotime($birthday) === false) {
239+
$birthday = DateTime::createFromFormat('Y-d-m', $birthday);
240+
} else {
241+
$birthday = new DateTime($birthday);
242+
}
243+
}
244+
245+
if (empty($birthday)) {
246+
$birthday = false;
233247
}
234248

235249
$this->birthday = $birthday;
@@ -271,8 +285,13 @@ public function getDeathday()
271285
public function setDeathday($deathday)
272286
{
273287
if (!$deathday instanceof DateTime && !empty($deathday)) {
274-
// Is the format Y-m-d ?
275-
if (strtotime($deathday) === false) {
288+
if (ctype_digit($deathday) && strlen(4)) {
289+
$deathday = DateTime::createFromFormat(
290+
'Y-m-d',
291+
sprintf('%d-01-01', $deathday),
292+
new \DateTimeZone('UTC')
293+
);
294+
} elseif (strtotime($deathday) === false) {
276295
$deathday = DateTime::createFromFormat('Y-d-m', $deathday);
277296
} else {
278297
$deathday = new DateTime($deathday);

test/Tmdb/Tests/Api/TestCase.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use Tmdb\Client;
2222
use Tmdb\Common\ParameterBag;
2323
use Tmdb\Tests\TestCase as Base;
24-
2524
use Tmdb\Token\Session\GuestSessionToken;
2625

2726
use function json_decode;

test/Tmdb/Tests/Model/PersonTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414

1515
namespace Tmdb\Tests\Model;
1616

17+
use Http\Mock\Client;
1718
use stdClass;
19+
use Tmdb\Common\ObjectHydrator;
20+
use Tmdb\Factory\PeopleFactory;
21+
use Tmdb\HttpClient\HttpClient;
1822
use Tmdb\Model\Common\GenericCollection;
1923
use Tmdb\Model\Person;
2024

@@ -76,4 +80,21 @@ public function shouldAllowOverridingDefaultCollectionObjects()
7680
]
7781
);
7882
}
83+
84+
/**
85+
* @test
86+
*/
87+
public function regressionIssue180()
88+
{
89+
$person = (new ObjectHydrator())->hydrate(new Person(), [
90+
'birthday' => '1945',
91+
'deathday' => '1946',
92+
]);
93+
94+
$actualBirthDate = $person->getBirthday()->format('Y-m-d');
95+
$actualDeathDate = $person->getDeathday()->format('Y-m-d');
96+
97+
$this->assertEquals('1945-01-01', $actualBirthDate);
98+
$this->assertEquals('1946-01-01', $actualDeathDate);
99+
}
79100
}

0 commit comments

Comments
 (0)