Skip to content
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
1 change: 1 addition & 0 deletions mago.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ empty-line-after-opening-tag = false
integrations = ["php-unit", "tempest"]

[linter.rules]
yoda-conditions = { enabled = false }
interface-name = { psr = false }
trait-name = { psr = false }
class-name = { psr = false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ public function render(
}

// splits the text to an array so we can work with individual lines
// @mago-expect lint:no-nested-ternary
$lines = str($buffer->text ?: ($placeholder ?: ''))
->explode("\n")
->flatMap(fn (string $line) => str($line)->chunk($this->maxLineCharacters)->toArray())
Expand Down
20 changes: 13 additions & 7 deletions packages/database/src/Mappers/SelectModelMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,24 @@ private function values(ModelInspector $model, array $data): array
foreach ($data as $key => $value) {
$relation = $model->getRelation($key);

if (! $relation instanceof HasMany) {
if ($relation instanceof BelongsTo) {
if ($relation->property->isNullable() && array_filter($data[$relation->name] ?? []) === []) {
$data[$relation->name] = null;
}

continue;
}

$mapped = [];
$relationModel = inspect($relation);
if ($relation instanceof HasMany) {
$mapped = [];
$relationModel = inspect($relation);

foreach ($value as $item) {
$mapped[] = $this->values($relationModel, $item);
}
foreach ($value as $item) {
$mapped[] = $this->values($relationModel, $item);
}

$data[$key] = $mapped;
$data[$key] = $mapped;
}
}

return $data;
Expand Down
4 changes: 4 additions & 0 deletions packages/mapper/src/Mappers/ArrayToObjectMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public function resolveValue(PropertyReflector $property, mixed $value): mixed
{
$caster = $this->casterFactory->forProperty($property);

if ($property->isNullable() && $value === null) {
return null;
}

if ($caster === null) {
return $value;
}
Expand Down
1 change: 1 addition & 0 deletions packages/support/src/Str/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ function to_snake_case(Stringable|string $string, Stringable|string $delimiter =

$string = preg_replace('/(?<=\p{Ll}|\p{N})(\p{Lu})/u', $delimiter . '$1', $string);
$string = preg_replace('/(?<=\p{Lu})(\p{Lu}\p{Ll})/u', $delimiter . '$1', $string);
// @mago-expect lint:require-preg-quote-delimiter
$string = preg_replace('![^' . preg_quote($delimiter) . '\pL\pN\s]+!u', $delimiter, mb_strtolower($string, 'UTF-8'));
$string = preg_replace('/\s+/u', $delimiter, $string);
$string = trim($string, $delimiter);
Expand Down
1 change: 0 additions & 1 deletion packages/view/src/Elements/PhpDataElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ public function compile(): string
$localVariableName,
$localVariableName,
$isExpression
// @mago-expect lint:no-nested-ternary
? ($value ?: 'null')
: var_export($value, return: true),
);
Expand Down
55 changes: 55 additions & 0 deletions tests/Integration/Database/Builder/IsDatabaseModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,21 @@ public function test_delete_nonexistent_record(): void

$this->assertNull(Foo::get($fooId));
}

public function test_nullable_relations(): void
{
$this->migrate(
CreateMigrationsTable::class,
CreateBNullableTable::class,
CreateANullableTable::class,
);

$a = ANullableModel::create();

$a->load('b');

$this->assertNull($a->b);
}
}

final class Foo
Expand Down Expand Up @@ -963,3 +978,43 @@ public function up(): QueryStatement
->text('description');
}
}

final class CreateANullableTable implements MigratesUp
{
private(set) string $name = '100-create-a-nullable';

public function up(): QueryStatement
{
return new CreateTableStatement('a')
->primary()
->belongsTo('a.b_id', 'b.id', nullable: true);
}
}

final class CreateBNullableTable implements MigratesUp
{
private(set) string $name = '100-create-b-nullable';

public function up(): QueryStatement
{
return new CreateTableStatement('b')
->primary()
->string('name');
}
}

#[Table('a')]
final class ANullableModel
{
use IsDatabaseModel;

public ?BNullableModel $b = null;
}

#[Table('b')]
final class BNullableModel
{
use IsDatabaseModel;

public string $name;
}
9 changes: 4 additions & 5 deletions tests/Integration/Database/MultiDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ public function test_database_seed_on_selected_database(): void

$this->assertSame(
'Timeline Taxi',
query(Book::class)->select()->onDatabase('main')->first()->title,
query(Book::class)->select()->onDatabase('main')->where('title', 'Timeline Taxi')->first()->title,
);

$this->assertNull(
Expand All @@ -318,8 +318,7 @@ public function test_database_seed_on_selected_database(): void
->assertSuccess();

/** @var Book $book */
/** @phpstan-ignore-next-line */
$book = query(Book::class)->select()->onDatabase('backup')->first();
$book = query(Book::class)->select()->onDatabase('backup')->where('title', 'Timeline Taxi')->first();

$this->assertSame(
'Timeline Taxi',
Expand All @@ -335,7 +334,7 @@ public function test_migrate_fresh_seed_on_selected_database(): void

$this->assertSame(
'Timeline Taxi',
query(Book::class)->select()->onDatabase('main')->first()->title,
query(Book::class)->select()->onDatabase('main')->where('title', 'Timeline Taxi')->first()->title,
);

$this->assertException(QueryWasInvalid::class, function (): void {
Expand All @@ -348,7 +347,7 @@ public function test_migrate_fresh_seed_on_selected_database(): void

$this->assertSame(
'Timeline Taxi',
query(Book::class)->select()->onDatabase('backup')->first()->title,
query(Book::class)->select()->onDatabase('backup')->where('title', 'Timeline Taxi')->first()->title,
);
}

Expand Down
Loading