Guest User

Untitled

a guest
Jul 23rd, 2018
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. <?php
  2.  
  3. class Author
  4. {
  5. private $name;
  6. private $email;
  7. private $gender;
  8.  
  9. private $genders = ['m','f'];
  10.  
  11. public function __construct($name, $email, $gender)
  12. {
  13. $this->name = $name;
  14. $this->setEmail($email);
  15. $this->setGender($gender);
  16. }
  17.  
  18. public function __toString()
  19. {
  20. return sprintf('Author[name=%1$s,email=%2$s,gender=%3$s]', $this->name, $this->email, $this->gender);
  21. }
  22.  
  23. /**
  24. * @return mixed
  25. */
  26. public function getName()
  27. {
  28. return $this->name;
  29. }
  30.  
  31. /**
  32. * @return mixed
  33. */
  34. public function getEmail()
  35. {
  36. return $this->email;
  37. }
  38.  
  39. /**
  40. * @param mixed $email
  41. */
  42. public function setEmail($email)
  43. {
  44. if (!$this->isValidEmail($email)){
  45. throw new InvalidArgumentException('Email is not valid');
  46. }
  47. $this->email = $email;
  48. }
  49.  
  50. /**
  51. * @return mixed
  52. */
  53. public function getGender()
  54. {
  55. return $this->gender;
  56. }
  57.  
  58. /**
  59. * @param mixed $gender
  60. */
  61. private function setGender($gender)
  62. {
  63. if(!$this->isValidGender($gender)){
  64. throw new InvalidArgumentException('Gender is not valid');
  65. }
  66. $this->gender = $gender;
  67. }
  68.  
  69. private function isValidEmail($email) {
  70. return filter_var($email, FILTER_VALIDATE_EMAIL)
  71. && preg_match('/@.+\./', $email);
  72. }
  73.  
  74. private function isValidGender($gender)
  75. {
  76. return in_array($gender,$this->genders, true);
  77. }
  78. }
  79.  
  80. class Book
  81. {
  82. private $name;
  83. private $author;
  84. private $qty;
  85. private $price;
  86.  
  87. public function __construct($name, Author $author, $price, $qty = 0)
  88. {
  89. $this->name = $name;
  90. $this->author = $author;
  91. $this->setPrice($price);
  92. $this->setQty($qty);
  93. }
  94.  
  95. public function __toString()
  96. {
  97. return
  98. sprintf('Book[name=%1$s,Author[name=%2$s,email=%3$s,gender=%4$s],price=%5$0.2f,qty=%6$d]',
  99. $this->name, $this->author->getName(),$this->author->getEmail(),$this->author->getGender(), $this->price, $this->qty);
  100. }
  101.  
  102. /**
  103. * @return mixed
  104. */
  105. public function getName()
  106. {
  107. return $this->name;
  108. }
  109.  
  110. /**
  111. * @return mixed
  112. */
  113. public function getAuthor()
  114. {
  115. return $this->author;
  116. }
  117.  
  118. /**
  119. * @return mixed
  120. */
  121. public function getQty()
  122. {
  123. return $this->qty;
  124. }
  125.  
  126. /**
  127. * @param mixed $qty
  128. */
  129. public function setQty($qty)
  130. {
  131. $this->qty = (int) $qty;
  132. }
  133.  
  134. /**
  135. * @return mixed
  136. */
  137. public function getPrice()
  138. {
  139. return $this->price;
  140. }
  141.  
  142. /**
  143. * @param mixed $price
  144. */
  145. public function setPrice($price)
  146. {
  147. if($price <= 0){
  148. throw new InvalidArgumentException('Price must be greater then zero');
  149. }
  150.  
  151. $this->price = (float) $price;
  152. }
  153. }
Add Comment
Please, Sign In to add comment