shuba_ivan

Untitled

Jun 16th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.96 KB | None | 0 0
  1. <?php
  2. // write class Event that implements interface ParticipableInterface
  3. // Event represents real life event like - concert, meetup, or some other gathering of people
  4. // for storing participants - use array,
  5. // consider edge cases scenarios
  6. // It is allowed to use google for searching documentation.
  7.  
  8. interface ParticipableInterface
  9. {
  10.     public function getStartDate(); // Trivial - Do not implement
  11.  
  12.     public function getParticipants(); // Trivial - Do not implement
  13.  
  14.     public function setStartDate(DateTime $date);
  15.  
  16.     public function getDuration(); // Trivial -- Do not implement
  17.  
  18.     /**
  19.      * $duration -- integer, amount of minutes
  20.      */
  21.     public function setDuration($duration);
  22.  
  23.     // should return instance of DateTime or rise exception if duration has not been set yet
  24.     public function getEndDate();
  25.  
  26.     // when user joins event
  27.     public function join(ParticipantInterface $participant);
  28.  
  29.     // when user leaves
  30.     public function part(ParticipantInterface $participant);
  31. }
  32.  
  33. // FOR YOUR INFORMATION -- Do not implement this interface
  34. interface ParticipantInterface
  35. {
  36.     public function getId();
  37.  
  38.     public function getName();
  39.  
  40.     public function getEmail();
  41. }
  42.  
  43. class User implements ParticipantInterface
  44. {
  45.     public $id;
  46.     public $name;
  47.     public $email;
  48.  
  49.     public function getId()
  50.     {
  51.         return $this->id;
  52.     }
  53.  
  54.     public function getName()
  55.     {
  56.         return $this->name;
  57.     }
  58.  
  59.     public function getEmail()
  60.     {
  61.         return $this->email;
  62.     }
  63.  
  64.     /**
  65.      * Constructor.
  66.      */
  67.     public function __construct($id, $name, $email)
  68.     {
  69.         $this->id = $id;
  70.         $this->email = $email;
  71.         $this->name = $name;
  72.     }
  73. }
  74.  
  75. class Event implements ParticipableInterface
  76. {
  77.     /** @var  $nameEvent string */
  78.     public $nameEvent;
  79.  
  80.     /**
  81.      * @var array
  82.      */
  83.     public $participants = [];
  84.  
  85.     /** @var $startDate DateTime */
  86.     public $startDate;
  87.  
  88.     /** @var  integer */
  89.     public $duration;
  90.  
  91.     /** @var $endDate DateTime */
  92.     public $endDate;
  93.  
  94.     public function getStartDate() {
  95.         return $this->startDate;
  96.     }
  97.  
  98.     public function getParticipants() {
  99.         return $this->participants;
  100.     }
  101.  
  102.     public function setStartDate(DateTime $date) {
  103.         $this->startDate = $date;
  104.         return $this;
  105.     }
  106.  
  107.     public function getDuration() {
  108.         return $this->duration;
  109.     }
  110.  
  111.     /**
  112.      * $duration integer
  113.      */
  114.     public function setDuration($duration) {
  115.         $this->duration = $duration;
  116.         return $this;
  117.     }
  118.  
  119.     // should return instance of DateTime or rise exception if duration has not been set yet
  120.     public function getEndDate() {
  121.         if ($this->startDate->modify('+'.$this->duration.' minutes') >= new DateTime('now')) {
  122.             return new Exception('has not been set yet');
  123.         }
  124.         return $this->startDate->modify('+'.$this->duration.' minutes')->format('Y-m-d H:i:s');
  125.     }
  126.  
  127.     // when user joins event
  128.     public function join(ParticipantInterface $participant) {
  129.         $this->addParticipant($participant);
  130.     }
  131.  
  132.     /**
  133.      * @param array $participant
  134.      * @return $this
  135.      */
  136.     public function setParticipant(array $participant)
  137.     {
  138.         $this->participants = $participant;
  139.         return $this;
  140.     }
  141.  
  142.     public function addParticipant(ParticipantInterface $participant)
  143.     {
  144.         $participant = strtoupper($participant->getEmail());
  145.         if (!in_array($participant, $this->participants, true)) {
  146.             $this->participants[] = $participant;
  147.         }
  148.  
  149.         return $this;
  150.     }
  151.  
  152.     public function unsetParticipant(ParticipantInterface $participant)
  153.     {
  154.         $usersArrayEmail = $this->getParticipants();
  155.         $key = array_search((strtoupper($participant->getEmail())), $usersArrayEmail);
  156.         if ($key !== false) {
  157.             unset($usersArrayEmail[$key]);
  158.             $this->setParticipant($usersArrayEmail);
  159.         }
  160.  
  161.         return $this;
  162.     }
  163.  
  164.     // when user leaves
  165.     public function part(ParticipantInterface $participant) {
  166.         $this->unsetParticipant($participant);
  167.     }
  168.  
  169.     /**
  170.      * Constructor.
  171.      */
  172.     public function __construct($nameEvent)
  173.     {
  174.         $this->nameEvent = $nameEvent;
  175.     }
  176. }
  177.  
  178.     $event = new Event('concert');
  179.     $event
  180.         ->setStartDate(new DateTime('now'))
  181.         ->setDuration(10)
  182.         ;
  183.  
  184.     $user = new User(1, 'Ivan', '[email protected]');
  185.  
  186.     $event->join($user);
  187.     var_dump($event);
  188.     $event->part($user);
  189.     var_dump($event);    
  190.  
  191.  
  192. // YOUR CODE HERE
  193.  
  194.  
  195. // Palindrome is word that can be read backwards and it is the same word as read forwards example: kayak, dad
  196. // write function that will check if given word is palindrome
  197. // Simplest solution is most desired
  198.  
  199. /**
  200.  * @param string $word
  201.  * @return bool
  202.  */
  203. function is_palindrome($word) {
  204.     return strrev((string) $word) === $word ? true : false;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment