The ArrayCollection and ArrayGroupCollection classes provide convenient, reusable abstractions for working with constant-like key-value mappings (e.g. status codes, roles, priorities).
They are designed to be extended in your modules or application code.
ArrayCollection – simple flat key-value storageArrayGroupCollection – grouped key-value storage (multiple categories)These classes help centralize constants, improve type safety, and make lookups cleaner than raw arrays.
They are typically used as base classes you extend with your own constant definitions.
Example use case
<?php
use Krystal\Stdlib\ArrayCollection;
class StatusCollection extends ArrayCollection
{
protected $collection = [
1 => 'Active',
2 => 'Disabled',
3 => 'Deleted',
];
}
Simple key-value lookup for flat constants (e.g. status codes, priorities, types).
Extend this class and define your $collection property.
Typical usage
<?php
use Krystal\Stdlib\ArrayCollection;
class UserStatusCollection extends ArrayCollection
{
protected $collection = [
1 => 'Active',
2 => 'Suspended',
3 => 'Banned',
];
}
hasKey($key): bool
Determine whether a specific key is defined in the collection.
Example
<?php
$statuses = new UserStatusCollection();
if ($statuses->hasKey(1)) {
// Active status exists
}
hasKeys(array $keys): bool
Verify that all specified keys are present in the collection.
Example
if ($statuses->hasKeys([1, 3])) {
// Both Active and Banned are defined
}
findByKey($key, $default = ''): mixed
Retrieve a value by key, returning a default if the key is missing.
$statusName = $statuses->findByKey(1, 'Unknown'); // 'Active'
$missing = $statuses->findByKey(99, 'Unknown'); // 'Unknown'
getAll(): array
Return the full collection array.
Organize constants into named groups (e.g. user statuses, order statuses, payment methods). Useful when you have multiple related sets of constants.
Extend this class and define grouped $collection arrays.
Typical usage
<?php
use Krystal\Stdlib\ArrayGroupCollection;
class OrderStatusCollection extends ArrayGroupCollection
{
protected $collection = [
'pending' => [
1 => 'New',
2 => 'Awaiting Payment',
],
'processing' => [
10 => 'Processing',
11 => 'Shipped',
],
'completed' => [
20 => 'Delivered',
21 => 'Completed',
],
];
}
hasKey($target): bool
Determine whether a key exists in any of the groups.
Example
$orderStatuses = new OrderStatusCollection();
if ($orderStatuses->hasKey(10)) {
// Processing status exists
}
findByKey($target, $default = ''): mixed
Retrieve a value by key across all groups, returning a default if missing.
Example
$status = $orderStatuses->findByKey(10, 'Unknown'); // 'Processing' $missing = $orderStatuses->findByKey(999, 'Unknown'); // 'Unknown'
getAll(): array
Return a flattened array of all key-value pairs across groups.
Example
print_r($orderStatuses->getAll());
// Array ( [1] => New [2] => Awaiting Payment [10] => Processing ... )