Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Author
- {
- private $name;
- private $email;
- private $gender;
- private $genders = ['m','f'];
- public function __construct($name, $email, $gender)
- {
- $this->name = $name;
- $this->setEmail($email);
- $this->setGender($gender);
- }
- public function __toString()
- {
- return sprintf('Author[name=%1$s,email=%2$s,gender=%3$s]', $this->name, $this->email, $this->gender);
- }
- /**
- * @return mixed
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * @return mixed
- */
- public function getEmail()
- {
- return $this->email;
- }
- /**
- * @param mixed $email
- */
- public function setEmail($email)
- {
- if (!$this->isValidEmail($email)){
- throw new InvalidArgumentException('Email is not valid');
- }
- $this->email = $email;
- }
- /**
- * @return mixed
- */
- public function getGender()
- {
- return $this->gender;
- }
- /**
- * @param mixed $gender
- */
- private function setGender($gender)
- {
- if(!$this->isValidGender($gender)){
- throw new InvalidArgumentException('Gender is not valid');
- }
- $this->gender = $gender;
- }
- private function isValidEmail($email) {
- return filter_var($email, FILTER_VALIDATE_EMAIL)
- && preg_match('/@.+\./', $email);
- }
- private function isValidGender($gender)
- {
- return in_array($gender,$this->genders, true);
- }
- }
- class Book
- {
- private $name;
- private $author;
- private $qty;
- private $price;
- public function __construct($name, Author $author, $price, $qty = 0)
- {
- $this->name = $name;
- $this->author = $author;
- $this->setPrice($price);
- $this->setQty($qty);
- }
- public function __toString()
- {
- return
- sprintf('Book[name=%1$s,Author[name=%2$s,email=%3$s,gender=%4$s],price=%5$0.2f,qty=%6$d]',
- $this->name, $this->author->getName(),$this->author->getEmail(),$this->author->getGender(), $this->price, $this->qty);
- }
- /**
- * @return mixed
- */
- public function getName()
- {
- return $this->name;
- }
- /**
- * @return mixed
- */
- public function getAuthor()
- {
- return $this->author;
- }
- /**
- * @return mixed
- */
- public function getQty()
- {
- return $this->qty;
- }
- /**
- * @param mixed $qty
- */
- public function setQty($qty)
- {
- $this->qty = (int) $qty;
- }
- /**
- * @return mixed
- */
- public function getPrice()
- {
- return $this->price;
- }
- /**
- * @param mixed $price
- */
- public function setPrice($price)
- {
- if($price <= 0){
- throw new InvalidArgumentException('Price must be greater then zero');
- }
- $this->price = (float) $price;
- }
- }
Add Comment
Please, Sign In to add comment