當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Collection::reduce方法代碼示例

本文整理匯總了PHP中Illuminate\Support\Collection::reduce方法的典型用法代碼示例。如果您正苦於以下問題:PHP Collection::reduce方法的具體用法?PHP Collection::reduce怎麽用?PHP Collection::reduce使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在Illuminate\Support\Collection的用法示例。


在下文中一共展示了Collection::reduce方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。

示例1: resolve

 /**
  * 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;
 }
開發者ID:mattsches,項目名稱:Jeopardy,代碼行數:22,代碼來源:Resolver.php

示例2: createActivities

 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;
 }
開發者ID:filmoteca,項目名稱:filmoteca,代碼行數:14,代碼來源:ActivitiesRepository.php

示例3: breakIntoWidgets

 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;
 }
開發者ID:shortlist-digital,項目名稱:agreable-catfish-importer-plugin,代碼行數:64,代碼來源:Html.php

示例4: testReduce

 public function testReduce()
 {
     $data = new Collection([1, 2, 3]);
     $this->assertEquals(6, $data->reduce(function ($carry, $element) {
         return $carry += $element;
     }));
 }
開發者ID:sa7bi,項目名稱:euro16,代碼行數:7,代碼來源:SupportCollectionTest.php

示例5: originalPrice

 /**
  * 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));
 }
開發者ID:hughgrigg,項目名稱:ching-shop,代碼行數:13,代碼來源:PotentialOffer.php


注:本文中的Illuminate\Support\Collection::reduce方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。