Skip to content

Quick start

github-actions[bot] edited this page Jul 16, 2026 · 1 revision

Быстрый старт

Установка

composer require cloudcastle/collection

Минимальный пример

<?php

declare(strict_types=1);

use CloudCastle\Collection\Collection;

$orders = Collection::make([
    ['id' => 1, 'customer' => 'alice', 'status' => 'paid',    'total' => 150],
    ['id' => 2, 'customer' => 'bob',   'status' => 'pending', 'total' => 70],
    ['id' => 3, 'customer' => 'alice', 'status' => 'paid',    'total' => 220],
]);

// Фильтр → группировка → агрегат → сортировка
$report = $orders
    ->where('status', 'paid')
    ->groupBy('customer')
    ->map(static fn ($group) => $group->sum('total'))
    ->sortDesc()
    ->all(); // ['alice' => 370]

// Статистика по полю
$orders->sum('total');            // 440
$orders->pluck('total')->median(); // 150

// Безопасный JSON-цикл
$restored = Collection::fromJson($orders->toJson());

Строгая семантика — с первого дня

Collection::make([0, '1', null])->contains('admin'); // false (loose дал бы true)
Collection::make([1, '1', true])->unique()->count(); // 3 — типы не схлопываются
Collection::make(['1', '2'])->map('intval');         // [1, 2] — ключ не попал в базу счисления

Дальше

Clone this wiki locally