本文整理汇总了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;
}
示例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;
}
示例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> </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;
}
示例4: testReduce
public function testReduce()
{
$data = new Collection([1, 2, 3]);
$this->assertEquals(6, $data->reduce(function ($carry, $element) {
return $carry += $element;
}));
}
示例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));
}