From 24e0a14b8963ecaef21e497b3451c3ef06d75030 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Lucas?= Date: Wed, 8 Jan 2014 17:51:40 +0100 Subject: [PATCH] Simplify a little some array queries --- book.php | 22 ++++++++-------------- test/bookTest.php | 2 +- 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/book.php b/book.php index e5e95ed..554822a 100644 --- a/book.php +++ b/book.php @@ -235,12 +235,10 @@ class Book extends Base { public function getDataById ($idData) { - foreach ($this->getDatas () as $data) { - if ($data->id == $idData) { - return $data; - } - } - return NULL; + $reduced = array_filter ($this->getDatas (), function ($data) use ($idData) { + return $data->id == $idData; + }); + return reset ($reduced); } public function getRating () { @@ -283,14 +281,10 @@ class Book extends Base { } public function getDataFormat ($format) { - foreach ($this->getDatas () as $data) - { - if ($data->format == $format) - { - return $data; - } - } - return NULL; + $reduced = array_filter ($this->getDatas (), function ($data) use ($format) { + return $data->format == $format; + }); + return reset ($reduced); } public function getFilePath ($extension, $idData = NULL, $relative = false) diff --git a/test/bookTest.php b/test/bookTest.php index 90c6df8..15ae1c4 100644 --- a/test/bookTest.php +++ b/test/bookTest.php @@ -290,7 +290,7 @@ class BookTest extends PHPUnit_Framework_TestCase $data = $book->getDataFormat ("PDF"); $this->assertEquals (19, $data->id); - $this->assertNull ($book->getDataFormat ("FB2")); + $this->assertFalse ($book->getDataFormat ("FB2")); } public function testGetMimeType () {