stefna/ds-collection

Common collection classes for stefna

1.1.0 2024-03-22 12:30 UTC

This package is auto-updated.

Last update: 2024-05-22 12:59:59 UTC


README

Build Status Latest Version on Packagist Software License

This package is a lightweight config loader with type safety as the primary corner stone.

Requirements

PHP 8.2 or higher.

Installation

composer require stefna/ds-collection

Usage

Generic implementations

We provide 2 generic implementations of the collections for convenience and internal usage

List:

<?php

use Stefna\Collection\GenericListCollection;
use Stefna\Collection\GenericMapCollection;


/** @var GenericListCollection<ClassType> */
$collection = new GenericListCollection(ClassType::class);

$collection[] = new ClassType();
// or
$collection->add(new ClassType());

/** @var GenericMapCollection<ClassType> */
$newCollection = $collection->indexBy(fn (ClassType $o) => (string)$o->id);

Map

<?php

use Stefna\Collection\GenericListCollection;
use Stefna\Collection\GenericMapCollection;


/** @var GenericMapCollection<ClassType> */
$collection = new GenericMapCollection(ClassType::class);

$collection['id'] = new ClassType();
// or
$collection->add('id', new ClassType());

foreach ($collection as $key => $object) {
	$key === string;
	$object === ClassType::class;
}

/** @var GenericListCollection<ClassType> */
$listCollection = $collection->toList();
foreach ($collection as $key => $object) {
	$key === int;
	$object === ClassType::class;
}

Creating Typed Collections

It is preferable to subclass AbstractListCollection and AbstractMapCollection

<?php

use Stefna\Collection\AbstractListCollection;

final class RowCollection extends AbstractListCollection
{
	protected static string $defaultCollectionType = ClassType::class;
}

$collection = new RowCollection();
$collection->getType() === ClassType::class;

Contribute

We are always happy to receive bug/security reports and bug/security fixes

License

The MIT License (MIT). Please see License File for more information.