diff --git a/Homeworks/1-Creational-design-patterns/1-Database-Factory-UML.png b/Homeworks/1-Creational-design-patterns/1-Database-Factory-UML.png new file mode 100644 index 0000000..4d24060 Binary files /dev/null and b/Homeworks/1-Creational-design-patterns/1-Database-Factory-UML.png differ diff --git a/Homeworks/1-Creational-design-patterns/Answer.php b/Homeworks/1-Creational-design-patterns/Answer.php new file mode 100644 index 0000000..2a4afc7 --- /dev/null +++ b/Homeworks/1-Creational-design-patterns/Answer.php @@ -0,0 +1,30 @@ +xml = $xml; + } + + public function getJson() + { + $xml = simplexml_load_string($this->xml->getXML()); + return json_encode($xml); + } +} diff --git a/Homeworks/3-Behavioral-design-patterns/3-Book-Iterator-UML.png b/Homeworks/3-Behavioral-design-patterns/3-Book-Iterator-UML.png new file mode 100644 index 0000000..bd89ff0 Binary files /dev/null and b/Homeworks/3-Behavioral-design-patterns/3-Book-Iterator-UML.png differ diff --git a/Homeworks/3-Behavioral-design-patterns/Answer.php b/Homeworks/3-Behavioral-design-patterns/Answer.php new file mode 100644 index 0000000..afa2968 --- /dev/null +++ b/Homeworks/3-Behavioral-design-patterns/Answer.php @@ -0,0 +1,85 @@ +id = $id; + $this->content = $content; + } + public function getId() + { + return $this->id; + } + public function getContent() + { + return $this->content; + } +} + +class BookList { + private $books = []; + private $bookCount = 0; + + public function getBookCount() + { + return $this->bookCount; + } + public function getBook($bookNumber) { + return isset($this->books[$bookNumber]) ? $this->books[$bookNumber] : null; + } + public function addBook(Book $book) + { + $this->bookCount += 1; + $this->books[$this->bookCount] = $book; + return $this->bookCount; + } + public function removeBook(Book $book) { + $counter = 0; + while (++$counter <= $this->getBookCount()) { + if ($book->getId() == $this->books[$counter]->getId()) { + for ($i = $counter; $i < $this->getBookCount(); $i++) { + $this->books[$i] = $this->books[$i + 1]; + } + $this->bookCount -= 1; + } + } + return $this->getBookCount(); + } +} + +class BookListIterator { + protected $bookList; + protected $currentBook = 0; + + public function __construct(BookList $bookList) + { + $this->bookList = $bookList; + } + public function getCurrentBook() + { + return isset($this->bookList[$this->currentBook]) ? $this->bookList[$this->currentBook] : null; + } + public function hasNextBook() + { + return $this->bookList->getBookCount() > $this->currentBook; + } + public function getNextBook() { + return $this->hasNextBook() ? $this->bookList->getBook(++$this->currentBook) : null; + } +} + +class BookListReverseIterator extends BookListIterator { + public function __construct(BookList $bookList) + { + $this->bookList = $bookList; + $this->currentBook = $this->bookList->getBookCount() + 1; + } + public function hasNextBook() { + return 1 < $this->currentBook; + } + public function getNextBook() { + return $this->hasNextBook() ? $this->bookList->getBook(--$this->currentBook) : null; + } +}