Skip to content

Commit c2d9721

Browse files
committed
add Laravel IDE helper hook
1 parent d33ecb9 commit c2d9721

5 files changed

+63
-3
lines changed

README.md

+18-2
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,27 @@ You can install the package via composer:
1919
composer require datomatic/laravel-enum-collections
2020
```
2121

22-
The main parts of the package are:
22+
The main parts of the package are:
2323
- [EnumCollection](#enumCollection)
24-
- [Eloquent model casting](#casting)
24+
- [Eloquent model casting](#casting)
2525
- [HasEnumCollections trait](#HasEnumCollections-trait)
2626

27+
28+
### Using Laravel IDE Helper?
29+
If you are using [Laravel IDE Helper](https://github.com/barryvdh/laravel-ide-helper), you need to run the following command:
30+
31+
```bash
32+
php artisan vendor:publish --tag=ide-helper-hooks
33+
```
34+
and add `LaravelEnumCollectionModelIdeHelperHook::class` on `model_hooks` array in `config/ide-helper.php`
35+
36+
```php
37+
'model_hooks' => [
38+
...,
39+
LaravelEnumCollectionModelIdeHelperHook::class,
40+
],
41+
```
42+
2743
## EnumCollection
2844

2945
`EnumCollection` is an extension of base [Laravel collection](https://laravel.com/docs/collections) that expand his functionalities to add the compatibility with:

phpstan.neon.dist

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ parameters:
55
level: 9
66
paths:
77
- src
8+
89
tmpDir: build/phpstan
910
checkOctaneCompatibility: true
1011
checkModelProperties: true

src/Casts/AsLaravelEnumCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes)
6565
}
6666

6767
/**
68-
* @param Arrayable<int, int|string|TValue>|iterable<int, int|string|TValue>|int|string|null|EnumCollection $value
68+
* @param Arrayable<TKey, int|string|TValue>|iterable<TKey, int|string|TValue>|int|string|null|EnumCollection<TKey,TValue> $value
6969
*/
7070
public function set(Model $model, string $key, mixed $value, array $attributes)
7171
{

src/EnumCollectionServiceProvider.php

+5
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,10 @@ public function configurePackage(Package $package): void
1717
* More info: https://github.com/spatie/laravel-package-tools
1818
*/
1919
$package->name('laravel-enum-collections');
20+
21+
$this->publishes([
22+
$this->package->basePath('/../stubs/LaravelEnumCollectionModelIdeHelperHook.stub')
23+
=> app_path('Support/IdeHelper/LaravelEnumCollectionModelIdeHelperHook.stub'),
24+
], 'ide-helper-hooks');
2025
}
2126
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace App\Support\IdeHelper;
5+
6+
use Barryvdh\LaravelIdeHelper\Console\ModelsCommand;
7+
use Barryvdh\LaravelIdeHelper\Contracts\ModelHookInterface;
8+
use Datomatic\EnumCollections\Casts\AsLaravelEnumCollection;
9+
use Datomatic\EnumCollections\EnumCollection;
10+
use Illuminate\Database\Eloquent\Model;
11+
use Illuminate\Support\Str;
12+
13+
class LaravelEnumCollectionModelIdeHelperHook implements ModelHookInterface
14+
{
15+
public function run(ModelsCommand $command, Model $model): void
16+
{
17+
foreach ($model->getCasts() as $attribute => $cast) {
18+
if(Str::contains($cast, ':') && Str::contains($cast, AsLaravelEnumCollection::class)) {
19+
strtok($cast, ':');
20+
$params = strtok(':');
21+
$params = $params ? explode(',', $params) : [];
22+
$type = $this->getCollectionTypeHint('\\' . EnumCollection::class,'\\' . $params[0]);
23+
$command->setProperty($attribute, $type);
24+
}
25+
}
26+
}
27+
28+
protected function getCollectionTypeHint(string $collectionClassNameInModel, string $relatedModel): string
29+
{
30+
$useGenericsSyntax = config('ide-helper.use_generics_annotations', true);
31+
if ($useGenericsSyntax) {
32+
return $collectionClassNameInModel . '<int, ' . $relatedModel . '>';
33+
} else {
34+
return $collectionClassNameInModel . '|' . $relatedModel . '[]';
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)