webparking / laravel-type-safe-collection
This package provides type-safe extension of the laravel collection, forcing a single type of object.
Installs: 8 234
Dependents: 0
Suggesters: 0
Security: 0
Stars: 37
Watchers: 4
Forks: 2
Open Issues: 0
Requires
- php: >=7.1.0
- ext-json: *
- illuminate/console: ^5.5|^6.0|^7.0|^8.0
- illuminate/database: ^5.5|^6.0|^7.0|^8.0
- illuminate/support: ^5.5|^6.0|^7.0|^8.0
- webmozart/assert: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.12
- orchestra/database: ^3.5|^4.0|^5.0|dev-6.x
- orchestra/testbench: ^3.5|^4.0|^5.0|dev-6.x
- phpstan/phpstan: ^0.12
- phpunit/phpunit: ^6.0|^7.0|^8.0
This package is auto-updated.
Last update: 2025-01-10 20:01:42 UTC
README
Laravel Type-Safe Collection
PHP is getting more mature and allows us to program strong typed in new ways with each new version, however we still lack the option to have generic lists/arrays. This package aims to provide such a thing in the meantime.
The TypeSafeCollection
provided by this package will make sure that any object within it is the object you expect.
Installation
Add this package to composer.
composer require webparking/laravel-type-safe-collection
Usage
/** * @method \ArrayIterator|User[] getIterator() * @method User|null first() */ class UserCollection extends TypeSafeCollection { protected $type = User::class; }
class User extends Model { public function newCollection(array $models = []): UserCollection { return new UserCollection($models); } }
All queries on User that would result in a Collection will now result in a UserCollection.
get_class(User::all()) // UserCollection get_class(User::where('type', '=', 'admin')->get()) // UserCollection get_class(User::where('id', '=', 1)->first()) // User
Licence and Postcardware
This software is open source and licensed under the MIT license.
If you use this software in your daily development we would appreciate to receive a postcard of your hometown.
Please send it to: Webparking BV, Cypresbaan 31a, 2908 LT Capelle aan den IJssel, The Netherlands