reduce() public method

Reduce the collection to a single value.
public reduce ( callable $callback, mixed $initial = null ) : mixed
$callback callable
$initial mixed
return mixed
Example #1
0
 /**
  * Determines a winner from the list of buzzes accumulated.
  *
  * Note: This method is destructive, and will clear the list of buzzes on completion.
  * @return BuzzerResolution
  */
 public function resolve()
 {
     if ($this->isEmpty()) {
         return BuzzerResolution::createFailure();
     }
     /** @var BuzzReceivedEvent $winner */
     $winner = $this->buzzes->reduce(function (BuzzReceivedEvent $carry, BuzzReceivedEvent $event) {
         if ($event->getDifference() < $carry->getDifference()) {
             return $event;
         }
         return $carry;
     }, $this->buzzes->first());
     $resolution = BuzzerResolution::createSuccess($winner->getContestant(), $winner->getDifference());
     $this->buzzes = new Collection();
     return $resolution;
 }
 protected function createActivities(Collection $schedulesByAuditorium)
 {
     $activities = $schedulesByAuditorium->reduce(function (Collection $allActivities, $arrayAuditoriumSchedules) {
         $auditoriumSchedules = Collection::make($arrayAuditoriumSchedules);
         // List of schedules of a auditorium grouped by films.
         $schedulesByFilm = $auditoriumSchedules->groupBy(function ($item) {
             return $item->exhibition->exhibition_film->film->id;
         });
         $activities = $this->activityFactory->collection($schedulesByFilm);
         // merge return a new Collection
         return $allActivities->merge($activities);
     }, Collection::make([]));
     return $activities;
 }
 public static function breakIntoWidgets($widgetDom)
 {
     $widgets = [];
     $previous_was_paragraph = false;
     $current_paragraph_string = "";
     foreach ($widgetDom->find('*') as $index => $node) {
         if (self::checkIfValidParagraph($node->outertext)) {
             $new_paragraph = str_replace("<p>&nbsp;</p>", "", $node->outertext);
             $current_paragraph_string .= $new_paragraph;
         } else {
             if (!empty($current_paragraph_string)) {
                 $paragraphDom = HtmlDomParser::str_get_html($current_paragraph_string);
                 array_push($widgets, Paragraph::getFromWidgetDom($paragraphDom));
                 $current_paragraph_string = "";
             }
             if ($node->tag == 'h2') {
                 array_push($widgets, Heading::getFromWidgetDom($node));
                 continue;
             }
             if ($embedData = Embed::getFromWidgetDom($node)) {
                 array_push($widgets, $embedData);
                 continue;
             } else {
                 if (self::checkStringAgainstBlacklist($node->outertext)) {
                     $html = new stdClass();
                     $html->type = 'html';
                     $html->html = $node->outertext;
                     array_push($widgets, $html);
                 }
             }
         }
     }
     if (!empty($current_paragraph_string)) {
         $paragraphDom = HtmlDomParser::str_get_html($current_paragraph_string);
         array_push($widgets, Paragraph::getFromWidgetDom($paragraphDom));
     }
     $widgetCollection = new Collection($widgets);
     // HTML Merge step
     foreach ($widgets as $index => $widget) {
         if ($index != 0) {
             $prev = $widgets[$index - 1];
             if ($prev->type == 'html' && $widget->type == 'html') {
                 $widget->html = $prev->html . $widget->html;
                 unset($widgets[$index - 1]);
             }
         }
     }
     $widgets = array_values($widgets);
     $htmlCheck = $widgetCollection->reduce(function ($carry, $item) {
         return $item->type == 'html';
     });
     if ($htmlCheck) {
         $widgets = array($widgetCollection->reduce(function ($carry, $item) {
             if (!$carry) {
                 $carry = new stdClass();
                 $carry->html = '';
             }
             $carry->type = $item->type;
             $carry->html .= $item->html;
             return $carry;
         }));
     }
     return count($widgets) ? $widgets : false;
 }
Example #4
0
 public function testReduce()
 {
     $data = new Collection([1, 2, 3]);
     $this->assertEquals(6, $data->reduce(function ($carry, $element) {
         return $carry += $element;
     }));
 }
Example #5
0
 /**
  * What would the offer components have cost without the offer?
  *
  * @throws \InvalidArgumentException
  *
  * @return Money
  */
 public function originalPrice() : Money
 {
     return $this->components->reduce(function (Money $price, OfferComponent $component) {
         return $price->add($component->product()->price()->asMoney());
     }, Money::fromInt(0));
 }