From 6976e7eec5d7daaf09b897c06abac3bc6c711d93 Mon Sep 17 00:00:00 2001 From: Gregory Oschwald Date: Mon, 10 Oct 2016 10:59:59 -0700 Subject: [PATCH] Fix isset/empty on ->name. Closes #79 --- CHANGELOG.md | 5 +++++ src/Record/AbstractPlaceRecord.php | 21 ++++++++++++++++++++- tests/GeoIp2/Test/Model/NameTest.php | 20 ++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc3aa409..9c778db4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) ------------------ diff --git a/src/Record/AbstractPlaceRecord.php b/src/Record/AbstractPlaceRecord.php index 0b7e7d69..fd704c3c 100644 --- a/src/Record/AbstractPlaceRecord.php +++ b/src/Record/AbstractPlaceRecord.php @@ -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; } } diff --git a/tests/GeoIp2/Test/Model/NameTest.php b/tests/GeoIp2/Test/Model/NameTest.php index 962341d9..bc15fdcf 100644 --- a/tests/GeoIp2/Test/Model/NameTest.php +++ b/tests/GeoIp2/Test/Model/NameTest.php @@ -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, @@ -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,