Exemple #1
0
<?php

ini_set('display_errors', '1');
function __autoload($class_name)
{
    set_include_path('class');
    include_once $class_name . '.php';
}
writeln('BEGIN TESTING INTERPRETER PATTERN');
writeln('');
//load BookList for test data
$bookList = new BookList();
$inBook1 = new Book('PHP for Cats', 'Larry Truett');
$inBook2 = new Book('MySQL for Cats', 'Larry Truett');
$bookList->addBook($inBook1);
$bookList->addBook($inBook2);
$interpreter = new Interpreter($bookList);
writeln('test 1 - invalid request missing "book"');
writeln($interpreter->interpret('author 1'));
writeln('');
writeln('test 2 - valid book author request');
writeln($interpreter->interpret('book author 1'));
writeln('');
writeln('test 3 - valid book title request');
writeln($interpreter->interpret('book title 2'));
writeln('');
writeln('test 4 - valid book author title request');
writeln($interpreter->interpret('book author title 1'));
writeln('');
writeln('test 5 - invalid request with invalid book number');
writeln($interpreter->interpret('book title 3'));
Exemple #2
0
<?php

require 'Book.php';
require 'BookList.php';
require 'BookListIterator.php';
$firstBook = new Book('Core PHP Programming, Third Edition', 'Atkinson and Suraski');
$secondBook = new Book('PHP Bible', 'Converse and Park');
$thirdBook = new Book('Design Patterns', 'Gamma, Helm, Johnson, and Vlissides');
$books = new BookList();
$books->addBook($firstBook);
$books->addBook($secondBook);
$books->addBook($thirdBook);
$booksIterator = new BookListIterator($books);
while ($booksIterator->hasNextBook()) {
    $book = $booksIterator->getNextBook();
    echo $book->getAuthorAndTitle();
}