Skip to content

Commit

Permalink
Fix isset/empty on ->name. Closes #79
Browse files Browse the repository at this point in the history
  • Loading branch information
oschwald committed Oct 10, 2016
1 parent 24b278b commit 6976e7e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.4.3
------------------

* `isset()` on `name` attribute now returns the correct value. Fixes #79.

2.4.2 (2016-08-17)
------------------

Expand Down
21 changes: 20 additions & 1 deletion src/Record/AbstractPlaceRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,31 @@ public function __get($attr)
}
}

/**
* @ignore
*/
public function __isset($attr)
{
if ($attr == 'name') {
return $this->firstSetNameLocale() == null ? false : true;
} else {
return parent::__isset($attr);
}
}

private function name()
{
$locale = $this->firstSetNameLocale();
return $locale === null ? null : $this->names[$locale];
}

private function firstSetNameLocale()
{
foreach ($this->locales as $locale) {
if (isset($this->names[$locale])) {
return $this->names[$locale];
return $locale;
}
}
return null;
}
}
20 changes: 20 additions & 0 deletions tests/GeoIp2/Test/Model/NameTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public function testFallback()
'continent name is in Chinese (no Russian available)'
);

$this->assertTrue(
isset($model->continent->name),
'continent name is set'
);

$this->assertFalse(
empty($model->continent->name),
'continent name is not empty'
);

$this->assertSame(
'объединяет государства',
$model->country->name,
Expand All @@ -56,6 +66,16 @@ public function testTwoFallbacks()
'continent name is undef (no Russian or Japanese available)'
);

$this->assertFalse(
isset($model->continent->name),
'continent name is not set'
);

$this->assertTrue(
empty($model->continent->name),
'continent name is empty'
);

$this->assertSame(
'объединяет государства',
$model->country->name,
Expand Down

0 comments on commit 6976e7e

Please sign in to comment.