Skip to content

Commit

Permalink
Mixed type supports nullable by design
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 12, 2023
1 parent 03e7a9c commit 5af394a
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Serializer/CastToArray.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct(
}

$this->class = ltrim($propertyType, '?');
$this->isNullable = str_starts_with($propertyType, '?');
$this->isNullable = $baseType->equals(Type::Mixed) ? true : str_starts_with($propertyType, '?');

if (!$shape instanceof ArrayShape) {
$shape = ArrayShape::tryFrom($shape) ?? throw new MappingFailed('Unable to resolve the array shape; Verify your cast arguments.');
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/CastToBool.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(
throw new MappingFailed('The property type `'.$propertyType.'` is not supported; a `bool` type is required.');
}

$this->isNullable = str_starts_with($propertyType, '?');
$this->isNullable = $baseType->equals(Type::Mixed) ? true : str_starts_with($propertyType, '?');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/CastToDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function __construct(
}

$this->class = $class;
$this->isNullable = str_starts_with($propertyType, '?');
$this->isNullable = $baseType->equals(Type::Mixed) ? true : str_starts_with($propertyType, '?');
try {
$this->timezone = is_string($timezone) ? new DateTimeZone($timezone) : $timezone;
$this->default = (null !== $default) ? $this->cast($default) : $default;
Expand Down
4 changes: 3 additions & 1 deletion src/Serializer/CastToEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,18 @@ public function __construct(
throw new MappingFailed('The property type `'.$propertyType.'` is not supported; an `Enum` is required.');
}

$this->isNullable = str_starts_with($propertyType, '?');
$class = ltrim($propertyType, '?');
$isNullable = str_starts_with($propertyType, '?');
if ($baseType->equals(Type::Mixed)) {
if (null === $enum || !enum_exists($enum)) {
throw new MappingFailed('You need to specify the enum class with a `mixed` typed property.');
}
$class = $enum;
$isNullable = true;
}

$this->class = $class;
$this->isNullable = $isNullable;

try {
$this->default = (null !== $default) ? $this->cast($default) : $default;
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/CastToFloat.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
throw new MappingFailed('The property type `'.$propertyType.'` is not supported; a `float` type is required.');
}

$this->isNullable = str_starts_with($propertyType, '?');
$this->isNullable = $baseType->equals(Type::Mixed) ? true : str_starts_with($propertyType, '?');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/CastToInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct(
throw new MappingFailed('The property type `'.$propertyType.'` is not supported; a `int` type is required.');
}

$this->isNullable = str_starts_with($propertyType, '?');
$this->isNullable = $baseType->equals(Type::Mixed) ? true : str_starts_with($propertyType, '?');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Serializer/CastToString.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
throw new MappingFailed('The property type `'.$propertyType.'` is not supported; a `string` type is required.');
}

$this->isNullable = str_starts_with($propertyType, '?');
$this->isNullable = $baseType->equals(Type::Mixed) ? true : str_starts_with($propertyType, '?');
}

/**
Expand Down

0 comments on commit 5af394a

Please sign in to comment.