Installation Add the package via Composer:
composer require alexeyshockov/colada
No service provider or facade registration is required—use the Colada facade or namespace imports directly.
First Use Case
Import the Colada facade or use the \Colada\Colada class:
use Colada\Colada;
// Example: Convert a flat array to a nested structure
$flatArray = ['a', 'b', 'c'];
$nested = Colada::nest($flatArray, 2); // [['a', 'b'], ['c']]
Where to Look First
Colada\Colada for static methods like nest(), flatten(), group(), etc.php artisan tinker session.Array Transformation
Use Colada::nest(), Colada::flatten(), or Colada::chunk() for structural changes:
// Group associative arrays by a key
$users = [['id' => 1, 'role' => 'admin'], ['id' => 2, 'role' => 'user']];
$grouped = Colada::group($users, 'role');
// ['admin' => [...], 'user' => [...]]
// Flatten a multi-dimensional array
$multi = [1, [2, [3]]];
$flat = Colada::flatten($multi); // [1, 2, 3]
Collection Integration Chain with Laravel Collections for hybrid workflows:
use Illuminate\Support\Collection;
$collection = collect([1, 2, 3]);
$chunked = $collection->pipe(function ($coll) {
return Colada::chunk($coll->all(), 2);
}); // [[1, 2], [3]]
Data Validation
Use Colada::validate() to enforce array structures (e.g., required keys):
$data = ['name' => 'John'];
$isValid = Colada::validate($data, ['name' => 'required', 'age' => 'optional']);
API Response Handling Normalize API responses before returning:
$response = Colada::pick($apiData, ['id', 'name', 'created_at']);
return response()->json($response);
Colada class to the container for dependency injection:
$this->app->singleton(Colada::class, function () {
return new Colada();
});
trait ArrayHelper {
public function customGroup(array $array, string $key, string $subKey) {
return Colada::group($array, $key)->mapWithKeys(function ($group) use ($subKey) {
return [$subKey => $group];
});
}
}
Archived Package
spatie/array-to-object or Laravel’s built-in Collection methods.Type Safety
nest() or chunk() may not handle non-array inputs gracefully. Validate inputs:
if (!is_array($input)) {
throw new \InvalidArgumentException('Input must be an array.');
}
Performance
Colada::flatten() on large arrays) can be slow. Test with production-sized data.->chunk()).Configuration Quirks
Colada::group() vs. Collection::groupBy()).IDE Support
@method PHPDoc annotations for clarity:
/**
* @method static array nest(array $array, int $level)
*/
class Colada {}
\Log::debug('Transformed array:', ['data' => $transformedArray]);
null values, or nested structures with mixed types (e.g., [1, 'a', ['b']]).Custom Methods
Extend the Colada class or create a decorator:
class ExtendedColada extends Colada {
public static function customMethod(array $array) {
return parent::nest($array, 1)->map(fn ($item) => strtoupper($item));
}
}
Integration with Laravel Publish a config file (even if unused) to document expected inputs/outputs:
php artisan vendor:publish --provider="Colada\ColadaServiceProvider"
(Note: The package may not include a provider; create a minimal one if needed.)
Alternative for Laravel
Replace Colada::group() with Laravel’s Collection::groupBy() for consistency:
$grouped = collect($array)->groupBy('role');
How can I help you explore Laravel packages today?