Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support named parameter 'type' in the Cast attribute. #87

Merged
merged 5 commits into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
* text=auto eol=lf

/.github export-ignore
/art export-ignore
/tests export-ignore
Expand Down
4 changes: 1 addition & 3 deletions src/Attributes/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
#[Attribute(Attribute::TARGET_PROPERTY)]
final class Cast
{
public function __construct(public string $type)
{
}
public function __construct(public string $type) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ final class Column
public function __construct(
public ?string $name = null,
public mixed $default = null,
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,5 @@ public function __construct(
* @var array<string, string>
*/
public array $messages = [],
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/CreateRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public function __construct(
* @var array<string, string>
*/
public array $messages = [],
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/DB.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ public function __construct(
public ?string $connection = null,
public ?string $table = null,
public bool $timestamps = true,
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Events/Dispatches.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ final class Dispatches
public function __construct(
public string $eventClass,
public string $event = ''
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Events/Listener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ final class Listener
public function __construct(
public string $event = '',
public bool $queue = false
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Events/Observer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ final class Observer
{
public function __construct(
public string $observer
) {
}
) {}
}
4 changes: 1 addition & 3 deletions src/Attributes/Fillable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class Fillable
{
}
final class Fillable {}
4 changes: 1 addition & 3 deletions src/Attributes/Hidden.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class Hidden
{
}
final class Hidden {}
4 changes: 1 addition & 3 deletions src/Attributes/Immutable.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
use Attribute;

#[Attribute(Attribute::TARGET_PROPERTY)]
final class Immutable
{
}
final class Immutable {}
3 changes: 1 addition & 2 deletions src/Attributes/PrimaryKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@ final class PrimaryKey
public function __construct(
public string $type = 'int',
public bool $incrementing = true,
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Rules.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public function __construct(
* @var array<string, string>
*/
public array $messages = [],
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/UpdateRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,5 @@ public function __construct(
* @var array<string, string>
*/
public array $messages = [],
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Attributes/Watch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ final class Watch
{
public function __construct(
public string $event,
) {
}
) {}
}
11 changes: 8 additions & 3 deletions src/Concerns/CastValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,16 @@ private static function buildCastableProperties(Collection $properties): void
continue;
}

$castAttribute = $castAttribute->newInstance();
if (blank($castAttribute->type)) {
continue;
}

$configAttribute = $property->attributes->first(fn ($attribute) => $attribute->getName() === Config::class);
if (filled($configAttribute)) {
$configAttribute = $configAttribute->newInstance();
if (filled($configAttribute->column)) {
self::$modelCastableProperties[static::class][$configAttribute->column] = $castAttribute->getArguments()[0];
self::$modelCastableProperties[static::class][$configAttribute->column] = $castAttribute->type;

continue;
}
Expand All @@ -103,13 +108,13 @@ private static function buildCastableProperties(Collection $properties): void
if (filled($columnAttribute)) {
$columnAttribute = $columnAttribute->newInstance();
if (filled($columnAttribute->name)) {
self::$modelCastableProperties[static::class][$columnAttribute->name] = $castAttribute->getArguments()[0];
self::$modelCastableProperties[static::class][$columnAttribute->name] = $castAttribute->type;

continue;
}
}

self::$modelCastableProperties[static::class][$property->name] = $castAttribute->getArguments()[0];
self::$modelCastableProperties[static::class][$property->name] = $castAttribute->type;
}

$castableProperties = self::getPropertiesForAttributes($properties, [Config::class]);
Expand Down
22 changes: 11 additions & 11 deletions src/Console/Commands/LiftMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ private function buildMigrationCalls(Model $model, array $modelProperties): arra
continue;
}

if (blank($type)) {
if ($type === null) {
$result[] = "\$table->string('{$property}');";

continue;
}

if ($this->isDateType($type)) { // @phpstan-ignore-line
if ($this->isDateType($type)) {
if (
$modelHasTimestamps &&
($property === $model->getCreatedAtColumn() || $property === $model->getUpdatedAtColumn())
Expand All @@ -134,17 +134,17 @@ private function buildMigrationCalls(Model $model, array $modelProperties): arra
continue;
}

$result[] = $this->generateMigrationCall('timestamp', $type, $property, $tableExists, $modelColumns); // @phpstan-ignore-line
$result[] = $this->generateMigrationCall('timestamp', $type, $property, $tableExists, $modelColumns);
}

$result[] = match (true) {
$type->getName() === 'bool' => $this->generateMigrationCall('boolean', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
$type->getName() === 'int' => $this->generateMigrationCall('integer', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
$type->getName() === 'float' => $this->generateMigrationCall('float', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
$type->getName() === 'string' => $this->generateMigrationCall('string', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
$type->getName() === 'array' => $this->generateMigrationCall('json', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
$type->getName() === 'object' => $this->generateMigrationCall('json', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
default => $this->generateMigrationCall('string', $type, $property, $tableExists, $modelColumns), // @phpstan-ignore-line
$type->getName() === 'bool' => $this->generateMigrationCall('boolean', $type, $property, $tableExists, $modelColumns),
$type->getName() === 'int' => $this->generateMigrationCall('integer', $type, $property, $tableExists, $modelColumns),
$type->getName() === 'float' => $this->generateMigrationCall('float', $type, $property, $tableExists, $modelColumns),
$type->getName() === 'string' => $this->generateMigrationCall('string', $type, $property, $tableExists, $modelColumns),
$type->getName() === 'array' => $this->generateMigrationCall('json', $type, $property, $tableExists, $modelColumns),
$type->getName() === 'object' => $this->generateMigrationCall('json', $type, $property, $tableExists, $modelColumns),
default => $this->generateMigrationCall('string', $type, $property, $tableExists, $modelColumns),
};
}

Expand Down Expand Up @@ -176,7 +176,7 @@ private function buildMigrationCalls(Model $model, array $modelProperties): arra

private function buildPrimaryKeyMigrationCall(string $property, ?ReflectionNamedType $type): string
{
return ! blank($type) && $type->getName() === 'int' // @phpstan-ignore-line
return $type !== null && $type->getName() === 'int'
? '$table->id();'
: "\$table->string('{$property}')->primary();";
}
Expand Down
3 changes: 1 addition & 2 deletions src/Support/MethodInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ public function __construct(
* @var Collection<ReflectionAttribute>
*/
public readonly Collection $attributes
) {
}
) {}
}
3 changes: 1 addition & 2 deletions src/Support/PropertyInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,5 @@ public function __construct(
* @var Collection<ReflectionAttribute>
*/
public readonly Collection $attributes,
) {
}
) {}
}
4 changes: 1 addition & 3 deletions tests/Datasets/LibraryBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;

#[BelongsTo(Library::class)]
class LibraryBook extends Book
{
}
class LibraryBook extends Book {}
3 changes: 1 addition & 2 deletions tests/Datasets/PriceChangedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ final class PriceChangedEvent

public function __construct(
public ProductWatch $product,
) {
}
) {}
}
8 changes: 4 additions & 4 deletions tests/Datasets/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ class Product extends Model

public string $name;

#[Cast('float')]
#[Cast(type: 'float')]
public float $price;

#[Cast('int')]
#[Cast(type: 'int')]
public int $random_number;

#[Cast('immutable_datetime')]
#[Cast(type: 'immutable_datetime')]
public CarbonImmutable $expires_at;

#[Cast('array')]
#[Cast(type: 'array')]
public ?array $json_column;

public string $hash;
Expand Down
3 changes: 1 addition & 2 deletions tests/Datasets/RandomNumberChangedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ final class RandomNumberChangedEvent

public function __construct(
public ProductWatch $product,
) {
}
) {}
}
2 changes: 1 addition & 1 deletion tests/Datasets/UserColumn.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class UserColumn extends Model
public string $user_password;

#[Fillable]
#[Cast('boolean')]
#[Cast(type: 'boolean')]
#[Column(default: false)]
public bool $active;

Expand Down
4 changes: 1 addition & 3 deletions tests/Datasets/WorkBook.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,4 @@
use WendellAdriel\Lift\Attributes\Relations\BelongsTo;

#[BelongsTo(User::class)]
class WorkBook extends Book
{
}
class WorkBook extends Book {}
7 changes: 2 additions & 5 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@
|
*/

uses(
Tests\TestCase::class,
\Illuminate\Foundation\Testing\RefreshDatabase::class
)->in('Feature');
uses(Tests\TestCase::class)->in('Feature');

/*
|--------------------------------------------------------------------------
Expand All @@ -32,7 +29,7 @@
expect()->extend('toBeFileWithContent', function (string $fileContent) {
test()->assertFileExists($this->value);

expect(file_get_contents($this->value))->toBe($fileContent);
expect(str_replace(PHP_EOL, "\n", file_get_contents($this->value)))->toBe($fileContent);

return $this;
});
Expand Down