Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Ratings implements IteratorAggregate {
- private $ratings;
- public function __construct(Rating ...$ratings) {
- $this->ratings = $ratings;
- }
- public function getAverage() : Rating {
- if (empty($this->ratings)) {
- return new Rating(0);
- }
- $total = 0;
- foreach ($this->ratings as $rating) {
- $total += $rating->getValue();
- }
- $average = $total / count($this->ratings);
- return new Rating($average);
- }
- public function getIterator() { /* ... */ }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement