Uninitialised properties are useful because they won't be included in json_encode() output.
So, in order to easily re-encode a model back to JSON, it'd be nice if Valinor had some way to say "leave this property uninitialised".
The option allowUndefinedValues doesn't work well for this case, because null would still be included in JSON outputs when encoded, requiring a custom jsonSerialize implementation to remove them.
Perhaps this could take the form of an attribute like #[Optional] or #[MayBeUninitialized], so that specific properties can be designated as optional.
class Model{
#[MayBeUninitialized]
public string $mayBeUnset;
public int $mustBeSet;
}
$model = (new MapperBuilder())->allowUndefinedValues()->mapper()->map(Model::class, ["mustBeSet" => 1]);
var_dump($model);
Of course, I can do this with custom constructors, but this becomes very cumbersome with complex models that have a lot of optional fields, particularly when consumers may want to construct the model without providing all the arguments. Valinor appears not to set any fields directly if a constructor is present, so any constructor would have to accept all arguments.
Uninitialised properties are useful because they won't be included in
json_encode()output.So, in order to easily re-encode a model back to JSON, it'd be nice if Valinor had some way to say "leave this property uninitialised".
The option
allowUndefinedValuesdoesn't work well for this case, becausenullwould still be included in JSON outputs when encoded, requiring a customjsonSerializeimplementation to remove them.Perhaps this could take the form of an attribute like
#[Optional]or#[MayBeUninitialized], so that specific properties can be designated as optional.Of course, I can do this with custom constructors, but this becomes very cumbersome with complex models that have a lot of optional fields, particularly when consumers may want to construct the model without providing all the arguments. Valinor appears not to set any fields directly if a constructor is present, so any constructor would have to accept all arguments.