Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. <?php
  2.  
  3. class Ratings implements IteratorAggregate {
  4. private $ratings;
  5.  
  6. public function __construct(Rating ...$ratings) {
  7. $this->ratings = $ratings;
  8. }
  9.  
  10. public function getAverage() : Rating {
  11. if (empty($this->ratings)) {
  12. return new Rating(0);
  13. }
  14.  
  15. $total = 0;
  16.  
  17. foreach ($this->ratings as $rating) {
  18. $total += $rating->getValue();
  19. }
  20.  
  21. $average = $total / count($this->ratings);
  22. return new Rating($average);
  23. }
  24.  
  25. public function getIterator() { /* ... */ }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement