Skip to content

Commit

Permalink
Merge branch 'uno-sw-patch-1'
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Oct 28, 2024
2 parents dc70337 + d6fb790 commit 7740b80
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 36 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ Add support for multiple pivot types when using the same accessor.

### Added

- Add support for AsCollection::using and AsEnumCollection::of casts [#1577 / uno-sw](https://github.com/barryvdh/laravel-ide-helper/pull/1577)

2024-07-12, 3.1.0
------------------

Expand Down
14 changes: 12 additions & 2 deletions src/Console/ModelsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,6 @@ public function castPropertiesType($model)
case 'immutable_datetime':
$realType = '\Carbon\CarbonImmutable';
break;
case AsCollection::class:
case AsEnumCollection::class:
case 'collection':
$realType = '\Illuminate\Support\Collection';
break;
Expand All @@ -437,6 +435,18 @@ public function castPropertiesType($model)
continue;
}

if (Str::startsWith($type, AsCollection::class)) {
$realType = $this->getTypeInModel($model, $params[0] ?? null) ?? '\Illuminate\Support\Collection';
}

if (Str::startsWith($type, AsEnumCollection::class)) {
$realType = '\Illuminate\Support\Collection';
$relatedModel = $this->getTypeInModel($model, $params[0] ?? null);
if ($relatedModel) {
$realType = $this->getCollectionTypeHint($realType, $relatedModel);
}
}

$realType = $this->checkForCastableCasts($realType, $params);
$realType = $this->checkForCustomLaravelCasts($realType);
$realType = $this->getTypeOverride($realType);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Collections;

use Illuminate\Support\Collection;

class AdvancedCastCollection extends Collection
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Enums;

enum AdvancedCastEnum
{
case apple;
case banana;
case orange;
}
41 changes: 24 additions & 17 deletions tests/Console/ModelsCommand/AdvancedCasts/Models/AdvancedCast.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,35 @@

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Models;

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Collections\AdvancedCastCollection;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Enums\AdvancedCastEnum;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
use Illuminate\Database\Eloquent\Model;

class AdvancedCast extends Model
{
protected $casts = [
'cast_to_date_serialization' => 'date:Y-m-d',
'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s',
'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_date' => 'immutable_date',
'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_datetime' => 'immutable_datetime',
'cast_to_timestamp' => 'timestamp',
'cast_to_encrypted' => 'encrypted',
'cast_to_encrypted_array' => 'encrypted:array',
'cast_to_encrypted_collection' => 'encrypted:collection',
'cast_to_encrypted_json' => 'encrypted:json',
'cast_to_encrypted_object' => 'encrypted:object',
'cast_to_as_collection' => AsCollection::class,
'cast_to_as_enum_collection' => AsEnumCollection::class,
'cast_to_as_array_object' => AsArrayObject::class,
];
protected function casts(): array
{
return [
'cast_to_date_serialization' => 'date:Y-m-d',
'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s',
'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_date' => 'immutable_date',
'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_datetime' => 'immutable_datetime',
'cast_to_timestamp' => 'timestamp',
'cast_to_encrypted' => 'encrypted',
'cast_to_encrypted_array' => 'encrypted:array',
'cast_to_encrypted_collection' => 'encrypted:collection',
'cast_to_encrypted_json' => 'encrypted:json',
'cast_to_encrypted_object' => 'encrypted:object',
'cast_to_as_collection' => AsCollection::class,
'cast_to_as_collection_using' => AsCollection::using(AdvancedCastCollection::class),
'cast_to_as_enum_collection' => AsEnumCollection::class,
'cast_to_as_enum_collection_of' => AsEnumCollection::of(AdvancedCastEnum::class),
'cast_to_as_array_object' => AsArrayObject::class,
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Models;

use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Collections\AdvancedCastCollection;
use Barryvdh\LaravelIdeHelper\Tests\Console\ModelsCommand\AdvancedCasts\Enums\AdvancedCastEnum;
use Illuminate\Database\Eloquent\Casts\AsArrayObject;
use Illuminate\Database\Eloquent\Casts\AsCollection;
use Illuminate\Database\Eloquent\Casts\AsEnumCollection;
Expand All @@ -27,6 +29,8 @@
* @property \Illuminate\Support\Collection $cast_to_as_collection
* @property \Illuminate\Support\Collection $cast_to_as_enum_collection
* @property \ArrayObject $cast_to_as_array_object
* @property AdvancedCastCollection $cast_to_as_collection_using
* @property \Illuminate\Support\Collection<int, AdvancedCastEnum> $cast_to_as_enum_collection_of
* @method static \Illuminate\Database\Eloquent\Builder<static>|AdvancedCast newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|AdvancedCast newQuery()
* @method static \Illuminate\Database\Eloquent\Builder<static>|AdvancedCast query()
Expand All @@ -49,21 +53,26 @@
*/
class AdvancedCast extends Model
{
protected $casts = [
'cast_to_date_serialization' => 'date:Y-m-d',
'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s',
'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_date' => 'immutable_date',
'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_datetime' => 'immutable_datetime',
'cast_to_timestamp' => 'timestamp',
'cast_to_encrypted' => 'encrypted',
'cast_to_encrypted_array' => 'encrypted:array',
'cast_to_encrypted_collection' => 'encrypted:collection',
'cast_to_encrypted_json' => 'encrypted:json',
'cast_to_encrypted_object' => 'encrypted:object',
'cast_to_as_collection' => AsCollection::class,
'cast_to_as_enum_collection' => AsEnumCollection::class,
'cast_to_as_array_object' => AsArrayObject::class,
];
protected function casts(): array
{
return [
'cast_to_date_serialization' => 'date:Y-m-d',
'cast_to_datetime_serialization' => 'datetime:Y-m-d H:i:s',
'cast_to_custom_datetime' => 'custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_date' => 'immutable_date',
'cast_to_immutable_custom_datetime' => 'immutable_custom_datetime:Y-m-d H:i:s',
'cast_to_immutable_datetime' => 'immutable_datetime',
'cast_to_timestamp' => 'timestamp',
'cast_to_encrypted' => 'encrypted',
'cast_to_encrypted_array' => 'encrypted:array',
'cast_to_encrypted_collection' => 'encrypted:collection',
'cast_to_encrypted_json' => 'encrypted:json',
'cast_to_encrypted_object' => 'encrypted:object',
'cast_to_as_collection' => AsCollection::class,
'cast_to_as_collection_using' => AsCollection::using(AdvancedCastCollection::class),
'cast_to_as_enum_collection' => AsEnumCollection::class,
'cast_to_as_enum_collection_of' => AsEnumCollection::of(AdvancedCastEnum::class),
'cast_to_as_array_object' => AsArrayObject::class,
];
}
}

0 comments on commit 7740b80

Please sign in to comment.