http://php.net/manual/en/filter.filters.sanitize.php
Testfall
//testCase $bookUnlimited = new Book("Daniel Toll", "PHP Unlimited", "123456"); $bookUnlimited2 = new Book("Daniel Toll", "PHP Unlimited II", "123457"); $bookDuck = new Book("Arne Anka", "Duck Tales", "7654321"); $bookStore = new BookStore(); $bookStore->AddBook($bookUnlimited); $bookStore->AddBook($bookUnlimited2); $bookStore->AddBook($bookDuck); $controller = new AddBookController($bookStore); echo $controller->addBook();
AddBookController.php
require_once("BookStore.php"); require_once("BookStoreView.php"); require_once("BookView.php"); /** * AddBookController handles the use case Add book * Use-Case starts when user wants to add a book * User presents Book title, author and ISBN number to system * The system confirmes that the book has been added * and shows all books in the store. */ class AddBookController { private $bookStoreView; private $bookView; private $bookStore; public function __construct(BookStore $bookStore) { $this->bookStore = $bookStore; $this->bookStoreView = new BookStoreView($this->bookStore); $this->bookView = new BookView(); } /** * @return String HTML */ public function addBook() { $this->handleInput(); //Combine Output return $this->bookStoreView->getHTML() . $this->bookView->getHTML(); } private function handleInput() { if ($this->bookView->userWantsToAddBook()) { try { $book = $this->bookView->getNewBook(); if ($this->bookStore->addBook( $book ) == false) { $this->bookView->showThisBookExists(); } else { $this->bookView->bookAddedSuccess(); } } catch (Exception $exception) { } } } }
BookView.php
class BookView { private static $AuthorName = "BookView::AuthorName"; private static $TitleName = "BookView::TitleName"; private static $ISBNName = "BookView::ISBNName"; private static $addBook = "addBook"; private $message = ""; /** * @return boolean true if user wants to add a book */ public function userWantsToAddBook() { if (isset($_GET[self::$addBook])) return true; return false; } /** * @return String HTML */ public function getHTML() { //previous input $author = $this->getCleanInput(self::$AuthorName); $title = $this->getCleanInput(self::$TitleName); $isbn = $this->getCleanInput(self::$ISBNName); return " <div> <form action='?" . self::$addBook . "' method='post' enctype='multipart/form-data'> <fieldset> <legend>Add a new book</legend> <label for='AuthorID' >Author :</label> <input type='text' name='". self::$AuthorName ."' id='AuthorID' value='$author' /> <label for='TitleID' >Title :</label> <input type='text' name='". self::$TitleName ."' id='TitleID' value='$title' /> <label for='ISBNID' >ISBN :</label> <input type='text' name='". self::$ISBNName ."' id='ISBNID' value='$isbn' /> <input type='submit' value='Add Book'/> $this->message </fieldset> </form> </div>"; } /** * @return Book * @throws Exception if something is wrong */ public function getNewBook() { $author = $this->getCleanInput(self::$AuthorName); $title = $this->getCleanInput(self::$TitleName); $isbn = $this->getCleanInput(self::$ISBNName); try { return new Book($author, $title, $isbn); } catch (Exception $e) { $this->message = "All fields need to be set"; throw $e; } } public function bookAddedSuccess() { $this->message = "The book was successfully added..."; } public function showThisBookExists() { $this->message = "The book already exists in library..."; } /** * @param String input * @return String input - tags - trim * @throws Exception if something is wrong or input does not exist */ private function getCleanInput($inputName) { if (isset($_POST[$inputName]) == false) { return ""; } return $this->sanitize($_POST[$inputName]); } /** * @param String input * @return String input - tags - trim */ private function sanitize($input) { $temp = trim($input); return filter_var($temp, FILTER_SANITIZE_STRING, FILTER_FLAG_STRIP_LOW); } }
BookStore
require_once("Book.php"); /** * BookStore Contains a record of which books are available * in a BookStore */ class BookStore { /** * @var array of Book objects */ private $books = array(); /** * @param Book book The book to be added * @return boolean Return true if book was added, * false if book already was in store... */ public function addBook(Book $book) { if ($this->isBookInStore($book)) return false; //Add book to the end of the array $this->books[] = $book; return true; } /** * @return array of Book objects sorted by author */ public function getByAuthorsSorted() { //http://se1.php.net/usort $sortOk = usort($this->books, array("Book", "compareByAuthor") ); assert($sortOk); return $this->books; } /** * @param Book book, book to be compared to Store * @return boolean true if book exists in store */ private function isBookInStore(Book $book) { //isBookin store foreach ($this->books as $inStoreBook) { if ($inStoreBook->isSame($book)) { return true; } } return false; } }
Book.php
/** * An example class with member variables * http://www.phpdoc.org/ */ class Book { /** * @var String */ public $author; /** * @var String */ public $title; /** * @var String */ public $isbn; /** * @param String author , Example "J.K. Rowling" * @param String title , Example "Harry Potter and the Philosopher's Stone" * @param String isbn , Example "9788478888566" * @throws Exception if a book could not be constructed */ public function __construct($author, $title, $isbn) { if ($author == "") throw new Exception("Cannot create book without author"); if ($title == "") throw new Exception("Cannot create book without title"); if ($isbn == "") throw new Exception("Cannot create book without isbn"); $this->author = $author; $this->title = $title; $this->isbn = $isbn; } /** * @param Book other book to compare to * @return boolean return true if the books are the same */ public function isSame(Book $other) { if($this->author != $other->author) { return false; } if($this->title != $other->title) { return false; } if($this->isbn != $other->isbn) { return false; } return true; } /** * @param Book a * @param Book b * @return int return 1 if a > b * return 0 if a = b * return -1 if a < b */ public static function compareByAuthor($a, $b) { //http://se1.php.net/usort# Example #3 $al = strtolower($a->author); $bl = strtolower($b->author); if ($al == $bl) { return 0; } return ($al > $bl) ? +1 : -1; } }
BookStoreView.php
require_once("BookStore.php"); /** * BookStoreView visualizes a BookStore in HTML */ class BookStoreView { /** * @var BookStore */ private $bookStore; /** * @param BookStore */ public function __construct(BookStore $store) { $this->bookStore = $store; } /** * @return String (HTML) */ public function getHTML() { $returnValue = "<h2>Book Store</h2>"; $sortedBooks = $this->bookStore->getByAuthorsSorted(); $returnValue .= "<ol>"; foreach ($sortedBooks as $book) { $returnValue .= "<li>" . $this->getBookHTML($book) . " </li>"; } $returnValue .= "</ol>"; return $returnValue; } /** * @param Book * @return String (HTML) */ private function getBookHTML(Book $book) { return " <div> <h3>$book->title</h3> <p>Author: $book->author</p> <p>ISBN: $book->isbn</p> </div> n"; } }