PHP code example of tiny-blocks / collection
1. Go to this page and download the library: Download tiny-blocks/collection library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
tiny-blocks / collection example snippets
declare(strict_types=1);
namespace Example;
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\Order;
use TinyBlocks\Collection\KeyPreservation;
$collection = Collection::createFrom(elements: [1, 2, 3, 4, 5])
->add(6, 7)
->sort(order: Order::ASCENDING_VALUE)
->filter(predicates: static fn(int $value): bool => $value > 3)
->map(transformations: static fn(int $value): int => $value * 2)
->toArray(keyPreservation: KeyPreservation::DISCARD);
# Output: [8, 10, 12, 14]
declare(strict_types=1);
namespace Example;
use TinyBlocks\Collection\Collection;
final class Invoices extends Collection
{
public function totalAmount(): float
{
return $this->reduce(
accumulator: static fn(float $carry, Invoice $invoice): float => $carry + $invoice->amount,
initial: 0.0
);
}
}
declare(strict_types=1);
namespace Example;
final readonly class Invoice
{
public function __construct(public string $id, public float $amount, public string $customer)
{
}
}
declare(strict_types=1);
namespace Example;
use TinyBlocks\Collection\Collection;
use TinyBlocks\Mapper\ElementType;
#[ElementType(Invoice::class)]
final class Invoices extends Collection
{
}
declare(strict_types=1);
use Example\Invoices;
use TinyBlocks\Mapper\Mapper;
$mapper = Mapper::create();
$invoices = $mapper->toObject(type: Invoices::class, source: [
['id' => 'INV-001', 'amount' => 100.0, 'customer' => 'Alice'],
['id' => 'INV-002', 'amount' => 200.0, 'customer' => 'Bob']
]);
$rows = $mapper->toArray(source: $invoices);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->add(4, 5, 6);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFromEmpty();
$collection->add('X', 'Y', 'Z');
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collectionA = Collection::createFrom(elements: [1, 2]);
$collectionB = Collection::createFrom(elements: [3, 4]);
$collectionA->merge(other: $collectionB);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->remove(element: 1);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: $amounts);
$collection->removeAll(predicate: static fn(Amount $amount): bool => $amount->value > 10.0);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->removeAll();
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: $amounts);
$collection->filter(predicates: static fn(Amount $amount): bool => $amount->value > 100);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [0, 1, null, 2, '', 3]);
$collection->filter();
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\Order;
$collection = Collection::createFrom(elements: [3, 1, 2]);
$collection->sort(order: Order::DESCENDING_VALUE);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\Order;
$collection = Collection::createFrom(elements: $amounts);
$collection->sort(
order: Order::ASCENDING_VALUE,
comparator: static fn(Amount $first, Amount $second): int => $first->value <=> $second->value
);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->count();
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFromEmpty();
$collection->isEmpty();
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: $cryptos);
$collection->findBy(predicates: static fn(CryptoCurrency $crypto): bool => $crypto->symbol === 'ETH');
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->first(defaultValueIfNotFound: 'fallback');
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->getBy(index: 0, defaultValueIfNotFound: 'fallback');
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->last(defaultValueIfNotFound: 'fallback');
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3, 4, 5]);
$collection->slice(offset: 1, length: 2);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->contains(element: 5);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collectionA = Collection::createFrom(elements: [1, 2, 3]);
$collectionB = Collection::createFrom(elements: [1, 2, 3]);
$collectionA->equals(other: $collectionB);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [10.0, 20.0, 30.0]);
$collection->reduce(
accumulator: static fn(float $carry, float $amount): float => $carry + $amount,
initial: 0.0
);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: ['a', 'b', 'c']);
$collection->joinToString(separator: ', ');
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$total = 0.0;
$collection = Collection::createFrom(elements: $amounts);
$collection->each(actions: static function (Amount $amount) use (&$total): void {
$total += $amount->value;
});
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: $amounts);
$collection->groupBy(classifier: static fn(Amount $amount): string => $amount->currency->name);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->map(transformations: static fn(int $value): int => $value * 2);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
$collection = Collection::createFrom(elements: [[1, 2], [3, 4], 5]);
$collection->flatten();
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\KeyPreservation;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->toArray(keyPreservation: KeyPreservation::DISCARD);
declare(strict_types=1);
use TinyBlocks\Collection\Collection;
use TinyBlocks\Collection\KeyPreservation;
$collection = Collection::createFrom(elements: [1, 2, 3]);
$collection->toJson(keyPreservation: KeyPreservation::DISCARD);