Skip to content

Instantly share code, notes, and snippets.

@yasuken1990
Forked from sato0408/Book.php
Created July 7, 2016 09:03
Show Gist options
  • Save yasuken1990/40a6a03ecd287f5bd9d3959057a4d698 to your computer and use it in GitHub Desktop.
Save yasuken1990/40a6a03ecd287f5bd9d3959057a4d698 to your computer and use it in GitHub Desktop.
BookShelfクラスに、searchメソッドを実装してください。
run.phpが正しく動作することを確認してください。
[検索要件]
ISBN => 完全一致
タイトル => 部分一致
著者 => 部分一致
readme.txt以外のファイルの変更は自由です。
<?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;
}
}
<?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)
{
}
}
<?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)];
}
}
<?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