-
-
Save miyatahirotaka/87700e8eba7d8aaf13cf6eb54fd9061d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| BookShelfクラスに、searchメソッドを実装してください。 | |
| run.phpが正しく動作することを確認してください。 | |
| [検索要件] | |
| ISBN => 完全一致 | |
| タイトル => 部分一致 | |
| 著者 => 部分一致 | |
| readme.txt以外のファイルの変更は自由です。 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Mitsubachi\BookShelf; | |
| class Book { | |
| /** | |
| * ISBN | |
| * | |
| * @var string | |
| */ | |
| protected $isbn; | |
| /** | |
| * タイトル | |
| * | |
| * @var string | |
| */ | |
| protected $title; | |
| /** | |
| * 著者 | |
| * | |
| * @var string | |
| */ | |
| protected $author; | |
| /** | |
| * Set the isbn. | |
| * | |
| * @param string $isbn | |
| */ | |
| public function setIsbn($isbn) | |
| { | |
| $this->isbn = $isbn; | |
| } | |
| /** | |
| * Set the title. | |
| * | |
| * @param string $title | |
| */ | |
| public function setTitle($title) | |
| { | |
| $this->title = $title; | |
| } | |
| /** | |
| * Set the author. | |
| * | |
| * @param string $author | |
| */ | |
| public function setAuthor($author) | |
| { | |
| $this->author = $author; | |
| } | |
| /** | |
| * Get the isbn. | |
| */ | |
| public function getIsbn() | |
| { | |
| return $this->isbn; | |
| } | |
| /** | |
| * Get the title. | |
| */ | |
| public function getTitle() | |
| { | |
| return $this->title; | |
| } | |
| /** | |
| * Get the author. | |
| */ | |
| public function getAuthor() | |
| { | |
| return $this->author; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Mitsubachi\BookShelf; | |
| class BookShelf | |
| { | |
| /** | |
| * Bookオブジェクトの配列 | |
| * | |
| * @var array | |
| */ | |
| private $books; | |
| /** | |
| * BookShelf constructor. | |
| * | |
| * @param array $books | |
| */ | |
| public function __construct($books) | |
| { | |
| $this->books = $books; | |
| } | |
| /** | |
| * 蔵書を検索し、条件に一致するBookオブジェクトの配列を返す | |
| * | |
| * @param array $conditions | |
| * @return array | |
| */ | |
| public function search($conditions) | |
| { | |
| $callback = $this->createSearchBookFunction($conditions); | |
| return array_filter($this->books, $callback); | |
| } | |
| /** | |
| * 検索条件$conditionから、Bookが検索条件に適合するかどうかを判定する関数を生成する | |
| * | |
| * @param array $conditions | |
| * @return Closure | |
| */ | |
| private function createSearchBookFunction($conditions) | |
| { | |
| $predicates = []; // Closure[] 配列の要素1つ1つが検索条件に合っているかどうかのboolean値を返す関数である | |
| if (isset($conditions['isbn'])) $predicates[] = function ($book) use ($conditions) { return $book->getIsbn() === $conditions['isbn']; }; // ISBN => 完全一致 | |
| if (isset($conditions['title'])) $predicates[] = function ($book) use ($conditions) { return strpos($book->getTitle(), $conditions['title']) !== false; }; // タイトル => 部分一致 | |
| if (isset($conditions['author'])) $predicates[] = function ($book) use ($conditions) { return strpos($book->getAuthor(), $conditions['author']) !== false; }; // 著者 => 部分一致 | |
| // predicates内の全てのpredicate関数がtrueである場合、searchBookFunction関数はtrueを返す | |
| $searchBookFunction = function ($book) use ($predicates) { | |
| return array_reduce($predicates, function($boolean, $predicate) use ($book) { | |
| return $boolean && $predicate($book); | |
| }, true); | |
| }; | |
| return $searchBookFunction; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| namespace Mitsubachi\BookShelf; | |
| class Faker | |
| { | |
| public static function isbn() | |
| { | |
| return 'ISBN' . self::number(3) . '-' . self::number(1) . '-' . self::number(4) . '-' . self::number(4) . '-' . self::number(1); | |
| } | |
| public static function number($length) | |
| { | |
| return str_pad(rand(0, pow(10, $length) - 1), $length, '0', STR_PAD_LEFT); | |
| } | |
| public static function title() | |
| { | |
| $language = ['PHP', 'Java', 'Ruby', 'Python']; | |
| $purpose = ['プログラミング', 'によるオブジェクト指向開発', 'で始めるプログラミング', 'で作るWEBアプリケーション']; | |
| $suffix = ['概論', '入門', 'の絵本', 'の基礎', '応用編']; | |
| return $language[array_rand($language)] . $purpose[array_rand($purpose)] . $suffix[array_rand($suffix)]; | |
| } | |
| public static function author() | |
| { | |
| $firstName = ['Philip', 'Carolyn', 'Brian', 'Kevin', 'Pamela']; | |
| $lastName = ['Hill', 'Gonzalez', 'Williams', 'Powell', 'Garcia']; | |
| return $firstName[array_rand($firstName)] . ' ' . $lastName[array_rand($lastName)]; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require('Book.php'); | |
| require('Faker.php'); | |
| require('BookShelf.php'); | |
| $books = []; | |
| $total = 100; | |
| $replace = rand(0, $total); | |
| for ($i=1; $i<=$total; $i++) { | |
| $book = new Mitsubachi\BookShelf\Book(); | |
| if ($i === $replace) { | |
| $book->setIsbn('ISBN000-0-0000-0000-0'); | |
| } else { | |
| $book->setIsbn(Mitsubachi\BookShelf\Faker::isbn()); | |
| } | |
| $book->setTitle(Mitsubachi\BookShelf\Faker::title()); | |
| $book->setAuthor(Mitsubachi\BookShelf\Faker::author()); | |
| $books[] = $book; | |
| } | |
| $shelf = new Mitsubachi\BookShelf\BookShelf($books); | |
| $book1 = $shelf->search(['isbn' => 'ISBN000-0-0000-0000-0']); | |
| var_dump($book1); | |
| echo '=====' . PHP_EOL; | |
| $book2 = $shelf->search(['title' => 'オブジェクト指向']); | |
| var_dump($book2); | |
| echo '=====' . PHP_EOL; | |
| $book3 = $shelf->search(['author' => 'Williams']); | |
| var_dump($book3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment