SoapClient 3.3.0
: Any hints how to generate class with default value initialization?
#529
-
Are there some best practices on how to generate soap classes with assigning default values? I try to explain the problem with following class as example: class ShipmentMethodTranslations
{
/**
* @var array<int<0,max>, \WebConnector\MasterData\Type\ShipmentMethodTranslation>
*/
private array $Shipment_Method_Translation;
/**
* @return array<int<0,max>, \WebConnector\MasterData\Type\ShipmentMethodTranslation>
*/
public function getShipmentMethodTranslation() : array
{
return $this->Shipment_Method_Translation;
}
/**
* @param array<int<0,max>, \WebConnector\MasterData\Type\ShipmentMethodTranslation> $Shipment_Method_Translation
* @return static
*/
public function withShipmentMethodTranslation(array $Shipment_Method_Translation) : static
{
$new = clone $this;
$new->Shipment_Method_Translation = $Shipment_Method_Translation;
return $new;
}
} We receive ShipmentMethods from API. These ShipmentMethod objects contain a property
But
In my opinion it makes sense to generate the class with property with a default value: private array $Shipment_Method_Translation = [] // define default direct at property; Maybe a Any thoughts on this? Wish a nice day 🌞 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 6 replies
-
Did some debugging and found while generating classes, that the default value
|
Beta Was this translation helpful? Give feedback.
Those rules are being covered by the code generator though:
See https://github.com/phpro/soap-client/blob/v4.x/src/Phpro/SoapClient/CodeGenerator/TypeEnhancer/MetaTypeEnhancer.php
Our assemblers are not the once setting default values.
That is what laminas-code is doing for you. If you want to overwrite the default value - you should be configuring this through a custom property assembler:
https://github.com/laminas/laminas-code/blob/562e02b7d85cb9142b5116cc76c4c7c162a11a1c/src/Generator/PropertyGenerator.php#L42-L44
If done right, it could be added to this package maybe.
However, the problem you are really facing, is the fact that ext-soap is skipping setting the property during hydratio…