-
-
Save igormx/5f0ad7197e29a7b69517894413b203e7 to your computer and use it in GitHub Desktop.
Bookshelf
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 | |
class Book implements ItemInterface | |
{ | |
/** | |
* @var string | |
*/ | |
private string $title; | |
/** | |
* @var string | |
*/ | |
private string $author; | |
use ReadContentTrait; | |
/** | |
* @return string | |
*/ | |
public function getTitle(): string | |
{ | |
return $this->title; | |
} | |
/** | |
* @param string $title | |
*/ | |
public function setTitle(string $title): void | |
{ | |
$this->title = $title; | |
} | |
/** | |
* @return string | |
*/ | |
public function getAuthor(): string | |
{ | |
return $this->author; | |
} | |
/** | |
* @param string $author | |
*/ | |
public function setAuthor(string $author): void | |
{ | |
$this->author = $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 | |
class Bookshelf | |
{ | |
private array $items; | |
private int $maxItems; | |
public function storeItem(ItemInterface $item):void { | |
$this->items[] = $item; | |
} | |
public function retrieveItem(int $itemNumber) { | |
return (key_exists($itemNumber, $this->items) ? $this->items[$itemNumber] : false); | |
} | |
public function getState():array | |
{ | |
return [ | |
'elementCount' => count($this->items), | |
'spaceLeft' => $this->maxItems - count($this->items) | |
]; | |
} | |
public function searchItems(string $contentToSearch) { | |
/** @var ItemInterface $item */ | |
foreach ($this->items as $item) { | |
for ($pageNumber=1;$pageNumber<=$item->countPages();$pageNumber++) { | |
if (strstr($item->readPageContent($pageNumber), $contentToSearch)!== false) { | |
return $item; | |
} | |
} | |
} | |
return false; | |
} | |
} |
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 | |
interface ItemInterface | |
{ | |
/** | |
* @param int $page | |
* @return mixed | |
*/ | |
public function readPageContent(int $page); | |
public function countPages():array; | |
} |
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 | |
class Magazine implements ItemInterface | |
{ | |
private string $name; | |
private DateTime $publicationDate; | |
use ReadContentTrait; | |
/** | |
* @return string | |
*/ | |
public function getName(): string | |
{ | |
return $this->name; | |
} | |
/** | |
* @param string $name | |
*/ | |
public function setName(string $name): void | |
{ | |
$this->name = $name; | |
} | |
/** | |
* @return DateTime | |
*/ | |
public function getPublicationDate(): DateTime | |
{ | |
return $this->publicationDate; | |
} | |
/** | |
* @param DateTime $publicationDate | |
*/ | |
public function setPublicationDate(DateTime $publicationDate): void | |
{ | |
$this->publicationDate = $publicationDate; | |
} | |
} |
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 | |
class Notebook implements ItemInterface | |
{ | |
private string $owner; | |
use ReadContentTrait; | |
/** | |
* @return string | |
*/ | |
public function getOwner(): string | |
{ | |
return $this->owner; | |
} | |
/** | |
* @param string $owner | |
*/ | |
public function setOwner(string $owner): void | |
{ | |
$this->owner = $owner; | |
} | |
} |
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 | |
trait ReadContentTrait | |
{ | |
private array $pages; | |
public function readPageContent(int $page) | |
{ | |
return (key_exists($page,$this->pages))? $this->pages[$page]:null; | |
} | |
public function countPages():array | |
{ | |
return count($this->pages); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment