### V4 Configuration Example Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Example of how configuration was set up in v4 using individual methods for destinations and namespaces. ```php use Phpro\SoapClient\CodeGenerator\Config\Config; return Config::create() ->setEngine($engine = DefaultEngineFactory::create( EngineOptions::defaults('your.wsdl') )) ->setTypeDestination('src/Type') ->setTypeNamespace('App\\Type') ->setClientName('MyClient') ->setClientNamespace('App') ->setClientDestination('src') ->setClassMapName('MyClassmap') ->setClassMapNamespace('App') ->setClassMapDestination('src'); ``` -------------------------------- ### V5 Configuration Example with Value Objects Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Demonstrates the new v5 configuration approach using value objects like Destination, ClientConfig, and ClassMapConfig for a more structured setup. ```php use Phpro\SoapClient\CodeGenerator\Config\ClassMapConfig; use Phpro\SoapClient\CodeGenerator\Config\ClientConfig; use Phpro\SoapClient\CodeGenerator\Config\Config; use Phpro\SoapClient\CodeGenerator\Config\Destination; use Phpro\SoapClient\CodeGenerator\Config\TypeNamespaceMap; return Config::create() ->setEngine($engine = DefaultEngineFactory::create( EngineOptions::defaults('your.wsdl') )) ->setTypeNamespaceMap( TypeNamespaceMap::create(new Destination('src/Type', 'App\\Type')) ) ->setClient(new ClientConfig('MyClient', new Destination('src', 'App'))) ->setClassMap(new ClassMapConfig('MyClassmap', new Destination('src', 'App'))); ``` -------------------------------- ### Install Example HTTP Client and PSR-7 Implementation Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md If your application does not already have a PSR-18 client, you need to install one along with a PSR-7 implementation. This example uses symfony/http-client and nyholm/psr7. ```bash composer require symfony/http-client nyholm/psr7 ``` -------------------------------- ### Full Code Generation Configuration Example Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/configuration.md This example demonstrates a comprehensive configuration for the SOAP client code generator, including engine setup, type namespace mapping, client configuration, and assembly rules for various code elements like getters, setters, requests, and results. ```php setEngine(DefaultEngineFactory::create( EngineOptions::defaults($wsdl) ->withWsdlLoader(new FlatteningLoader(new StreamWrapperLoader())) ->withEncoderRegistry( EncoderRegistry::default() ->addClassMapCollection(SomeClassmap::types()) ->addBackedEnumClassMapCollection(SomeClassmap::enums()) ) )) ->setTypeNamespaceMap( TypeNamespaceMap::create(new Destination('SoapTypes', 'src/SoapTypes')) // You can add explicit XML xmlns -> PHP namespace mappings here: ->withMapping('http://www.xmlns.mapping', new Destination('src/Type/OtherDir', 'App\\Type\\OtherDir')) // Or use a strategy to automatically resolve destinations from xmlns prefixes: // This strategy will only be called for XML namespaces that don't have an explicit mapping configured. ->withStrategy(new PrefixBasedTypeNamespaceStrategy($config->getCodingStandards())) ) ->setClient(new ClientConfig('MySoapClient', new Destination('SoapClient', 'src/SoapClient'))) ->setClassMap(new ClassMapConfig('AcmeClassmap', new Destination('Acme\\Classmap', 'src/acme/classmap'))) ->addRule(new Rules\AssembleRule(new Assembler\GetterAssembler(new Assembler\GetterAssemblerOptions()))) ->addRule(new Rules\AssembleRule(new Assembler\ImmutableSetterAssembler( new Assembler\ImmutableSetterAssemblerOptions() ))) ->addRule( new Rules\IsRequestRule( $engine->getMetadata(), new Rules\MultiRule([ new Rules\AssembleRule(new Assembler\RequestAssembler()), new Rules\AssembleRule(new Assembler\ConstructorAssembler(new Assembler\ConstructorAssemblerOptions())), ]) ) ) ->addRule( new Rules\IsResultRule( $engine->getMetadata(), new Rules\MultiRule([ new Rules\AssembleRule(new Assembler\ResultAssembler()), ]) ) ) ->addRule( new Rules\IsExtendingTypeRule( $engine->getMetadata(), new Rules\AssembleRule(new Assembler\ExtendingTypeAssembler()) ) ) ->addRule( new Rules\IsAbstractTypeRule( $engine->getMetadata(), new Rules\AssembleRule(new Assembler\AbstractClassAssembler()) ) ) ; ``` -------------------------------- ### Install phpro/soap-client and Dependencies Source: https://context7.com/phpro/soap-client/llms.txt Install the core package along with a PSR-18 HTTP client and PSR-7 implementation using Composer. ```bash composer require phpro/soap-client symfony/http-client nyholm/psr7 symfony/cache ``` -------------------------------- ### Install phpro/soap-client Source: https://github.com/phpro/soap-client/blob/v6.x/README.md Install the phpro/soap-client package using Composer. This is the primary command to add the SOAP client to your project. ```sh $ composer require phpro/soap-client ``` -------------------------------- ### Advanced Calculator Client Factory Example Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-clientfactory.md This example demonstrates a more advanced client factory that includes custom configurations for the engine, such as encoder registries, transport plugins, WSDL loading, caching, and service selection criteria. It utilizes various components from the php-soap ecosystem. ```php withEncoderRegistry( EncoderRegistry::default() ->addClassMapCollection(CalculatorClassmap::getCollection()) ) ->withTransport( Psr18Transport::createForClient( new PluginClient( Psr18ClientDiscovery::find(), [$plugin1, $plugin2] ) ) ) ->withWsdlLoader( new FlatteningLoader( new Psr18Loader(Psr18ClientDiscovery::find()) ) ) ->withCache( new RedisAdapter(RedisAdapter::createConnection('redis://localhost')), new CacheConfig('my-wsdl-cache-key', ttlInSeconds: 3600) ) ->withWsdlServiceSelectionCriteria( ServiceSelectionCriteria::defaults() ->withPreferredSoapVersion(SoapVersion::SOAP_12) ->withServiceName('SpecificServiceName') ->withPortName('SpecificPortName') ) ); $eventDispatcher = new EventDispatcher(); $caller = new EventDispatchingCaller(new EngineCaller($engine), $eventDispatcher); return new CalculatorClient($caller); } } ``` -------------------------------- ### Generated Calculator Client Example Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Example of a generated client class that injects a `Caller` for handling requests. The `add` method demonstrates how the caller is invoked with the operation name and parameters. ```php use Calculator\Type\Add; use Calculator\Type\AddResponse; use Phpro\SoapClient\Caller\Caller; class CalculatorClient { /** * @var Caller */ private $caller; public function __construct(\Phpro\SoapClient\Caller\Caller $caller) { $this->caller = $caller; } public function add(Add $parameters): AddResponse { return ($this->caller)('Add', $parameters); } } ``` -------------------------------- ### Install PSR-3 Logger Implementation Source: https://github.com/phpro/soap-client/blob/v6.x/docs/event-subscribers/logger.md Before registering the LogSubscriber, ensure you have a PSR-3 compatible logging library installed via Composer. ```bash composer require psr/log-implementation ``` -------------------------------- ### Advanced Client Factory Implementation Source: https://context7.com/phpro/soap-client/llms.txt A detailed example of a manually customized client factory, demonstrating how to configure the engine, transport, caching, and service selection. ```php withEncoderRegistry( EncoderRegistry::default() ->addClassMapCollection(MyServiceClassmap::getCollection()) ) ->withTransport( Psr18Transport::createForClient( new PluginClient($httpClient, [/* $authPlugin, $loggingPlugin */]) ) ) ->withWsdlLoader(new FlatteningLoader(new Psr18Loader($httpClient))) ->withCache( new RedisAdapter(RedisAdapter::createConnection('redis://localhost')), new CacheConfig('my-service-wsdl', ttlInSeconds: 3600) ) ->withWsdlServiceSelectionCriteria( ServiceSelectionCriteria::defaults() ->withPreferredSoapVersion(SoapVersion::SOAP_12) ) ); $eventDispatcher = new EventDispatcher(); $caller = new EventDispatchingCaller(new EngineCaller($engine), $eventDispatcher); return new MyServiceClient($caller); } } ``` -------------------------------- ### Generated Typed SOAP Client Example Source: https://context7.com/phpro/soap-client/llms.txt An example of a generated client class that provides strongly-typed methods for interacting with SOAP operations. ```php caller)('HelloWorld', $parameters); \Psl\Type\instance_of(HelloWorldResponse::class)->assert($response); return $response; } } ``` -------------------------------- ### Configure DefaultEngineFactory with Transport and Metadata Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Example of a personalized factory class configuring the DefaultEngineFactory with custom WSDL provider, transport, and metadata options. ```php use Http\Client\Common\PluginClient; use Http\Discovery\Psr18ClientDiscovery; use Phpro\SoapClient\Soap\DefaultEngineFactory; use Phpro\SoapClient\Soap\Metadata\Manipulators\DuplicateTypes\RemoveDuplicateTypesStrategy; use Phpro\SoapClient\Soap\Metadata\Manipulators\TypesManipulatorChain; use Phpro\SoapClient\Soap\Metadata\MetadataOptions; use Soap\ExtSoapEngine\ExtSoapOptions; use Soap\ExtSoapEngine\Wsdl\Naming\Md5Strategy; use Soap\ExtSoapEngine\Wsdl\PermanentWsdlLoaderProvider; use Soap\Psr18Transport\Psr18Transport; use Soap\Psr18Transport\Wsdl\Psr18Loader; $httpClient = Psr18ClientDiscovery::find(); $engine = DefaultEngineFactory::create( ExtSoapOptions::defaults($wsdl, []) ->withClassMap(CalculatorClassmap::getCollection()) ->withWsdlProvider( new PermanentWsdlLoaderProvider( Psr18Loader::createForClient($httpClient), new Md5Strategy(), 'target/location' ) ), Psr18Transport::createForClient( new PluginClient( $httpClient, [ $middleware1, $middleware2, ] ) ), MetadataOptions::empty()->withTypesManipulator( new TypesManipulatorChain( new RemoveDuplicateTypesStrategy() ) ) ); ``` -------------------------------- ### Install HTTP Client and PSR-7 Implementation Source: https://github.com/phpro/soap-client/blob/v6.x/README.md Install a PSR-18 compatible HTTP client and a PSR-7/17 implementation like Symfony HTTP client and Nyholm PSR7. These are required for the SOAP client to function. ```sh $ composer require symfony/http-client nyholm/psr7 ``` -------------------------------- ### Class Map Configuration Example Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-classmap.md These values need to be set in your configuration file to generate a class map. Specify the desired class name, namespace, and destination path. ```php ->setClassMapName('MyClassmap') ->setClassMapNamespace('Myapp\MyclassMap') ->setClassMapDestination('src/myapp/myclassmap') ``` -------------------------------- ### FinalClassAssembler Example Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Marks a generated class as final. ```php final class MyType { } ``` -------------------------------- ### Custom Assembler Implementation Source: https://context7.com/phpro/soap-client/llms.txt Shows how to implement the AssemblerInterface to inject custom code into generated classes. This example adds a 'has' method for properties, utilizing coding standards for naming. ```php getCodeGeneratorContext()->codingStandards; $property = $context->getProperty(); // Use coding standards for consistent naming $methodName = $codingStandards->generatePropertyAccessorMethodName('has', $property->getName()); // Or use convenience method: // $methodName = $property->methodName('has'); $method = new MethodGenerator( $methodName, [], MethodGenerator::FLAG_PUBLIC, sprintf('return $this->%s !== null;', $property->getName()), '@return bool' ); $context->getClassGenerator()->addMethodFromGenerator($method); } } ``` -------------------------------- ### ExtendAssembler Example Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Adds a specified parent class to the generated class. ```php class MyType extends DType { } ``` -------------------------------- ### Configure V4 Client Factory Engine Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Modify the generated ClientFactory in V4 to use `DefaultEngineFactory` with `EngineOptions`. This example shows how to configure encoder registries and optionally enable WSDL caching or alternate HTTP settings. ```php $engine = DefaultEngineFactory::create( EngineOptions::defaults($wsdl) ->withEncoderRegistry( EncoderRegistry::default() ->addClassMapCollection(CalcClassmap::types()) ->addBackedEnumClassmapCollection(CalcClassmap::enums()) ) // If you want to enable WSDL caching: // ->withCache($yourPsr6CachePool) // If you want to use Alternate HTTP settings: // ->withWsdlLoader() // ->withTransport() // If you want specific SOAP setting: // ->withWsdlParserContext() // ->withWsdlServiceSelectionCriteria() ); ``` -------------------------------- ### Configure V4 Metadata Strategy Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md In V4, non-default metadata strategies can be configured directly in the code generation configuration. This example shows how to set a strategy to remove duplicate types. ```php use Phpro\SoapClient\CodeGenerator\Config\Config; use Phpro\SoapClient\Soap\Metadata\Manipulators\DuplicateTypes\RemoveDuplicateTypesStrategy; return Config::create() ->setDuplicateTypeIntersectStrategy( new RemoveDuplicateTypesStrategy() ) ``` -------------------------------- ### Generated HelloWorldRequest Class Source: https://context7.com/phpro/soap-client/llms.txt Example of a generated PHP class representing a SOAP request. It implements `RequestInterface` and includes a constructor and getter for its properties. ```php name = $name; } public function getName(): string { return $this->name; } } ``` -------------------------------- ### Customizing Generated Value-Objects Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-types.md After generating value-objects, you can customize them by adding required interfaces and methods. This example shows how to implement `RequestInterface`, `ResponseProviderInterface`, and `ResponseInterface`. ```php class HelloWorldRequest implements RequestInterface { public function __construct($name) { $this->name = $name; } // Generated code } class HelloWorldResponse implements ResponseProviderInterface { // Generated code public function getResponse() { return $this->greeting; } } class Greeting implements ResponseInterface { // Generated code public function getGreeting() { return $this->greeting; } } ``` -------------------------------- ### ExtendingTypeAssembler Example Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Adds a parent class to the generated class based on WSDL metadata. ```php class MyType extends BaseType { } ``` -------------------------------- ### Configuring Coding Standards Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/coding-standards.md Configure custom coding standards using `Config::setCodingStandards()`. This example shows how to set a custom strategy and configure type namespace mapping using the same strategy. ```php use Phpro\SoapClient\CodeGenerator\Config\Config; use Phpro\SoapClient\CodeGenerator\Config\Destination; use Phpro\SoapClient\CodeGenerator\Config\TypeNamespaceMap; use Phpro\SoapClient\CodeGenerator\TypeNamespaceMap\Strategy\PrefixBasedTypeNamespaceStrategy; return ($config = Config::create()) ->setCodingStandards(new MyCodingStandards()) ->setEngine(...) ->setTypeNamespaceMap( TypeNamespaceMap::create(new Destination('src/Type', 'App\\Type')) ->withStrategy(new PrefixBasedTypeNamespaceStrategy($config->getCodingStandards())) ) ->setClient(...) ->setClassMap(...) ; ``` -------------------------------- ### PropertyAssembler: Default Private Visibility Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md The default PropertyAssembler creates private properties. This example shows the default property declaration. ```php /** * @var string */ private string $prop1 = ''; ``` -------------------------------- ### Generated SOAP Class Map Structure Source: https://context7.com/phpro/soap-client/llms.txt An example of a generated class map collection that maps WSDL types to their corresponding PHP classes. ```php client->helloWorld(new HelloWorldRequest('name')); $this->assertEquals('Hello name', $response->getGreeting()); } ``` -------------------------------- ### Require PSR Cache Implementation Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Install a PSR-6 cache implementation for caching engine and WSDL parsing. Symfony Cache is a common choice. ```bash composer require symfony/cache ``` -------------------------------- ### Map Types to Specific Rules with TypeMapRule Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/rules.md Configure specific rules for particular SOAP types using TypeMapRule. Unmatched types fall back to a default rule. This example applies a ResultProviderInterface for 'SomeType' and no actions for 'NullType', with a default ResultInterface for others. ```php use Phpro\SoapClient\CodeGenerator\Assembler; use Phpro\SoapClient\CodeGenerator\Rules; $resultProviderRule = new Rules\AssembleRule(new Assembler\ResultProviderAssembler()); $defaultRule = new Rules\AssembleRule(new Assembler\ResultAssembler()); new Rules\TypeMapRule( [ 'SomeType' => $resultProviderRule, 'NullType' => null ], $defaultRule ) ``` -------------------------------- ### Basic Client Usage with Factory Source: https://context7.com/phpro/soap-client/llms.txt Demonstrates how to instantiate a SOAP client using the generated factory and make a strongly-typed call to a SOAP operation. ```php helloWorld(new HelloWorldRequest('Alice')); echo $response->getGreeting(); // "Hello Alice" } catch (SoapException $e) { echo 'SOAP error: ' . $e->getMessage(); } ``` -------------------------------- ### Configure Client and Classmap Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/configuration.md Configure the generated client class using `setClient()` with `ClientConfig`. Similarly, configure the classmap using `setClassMap()` with `ClassMapConfig`. ```php Config::create() ->setClient(new ClientConfig($name, $destination)) ->setClassMap(new ClassMapConfig($name, $destination)) ``` -------------------------------- ### Run SOAP Client Scaffolding Wizard Source: https://github.com/phpro/soap-client/blob/v6.x/README.md Execute the SOAP client scaffolding wizard to quickly set up communication with your SOAP server. This command automates initial configuration. ```sh ./vendor/bin/soap-client wizard ``` -------------------------------- ### Generate Client Factory Command Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-clientfactory.md Use this command to generate a base client factory. A configuration file is required to build the classmap. The generated factory will be placed in the same namespace and directory as the client, using the client's name appended with 'Factory'. ```bash vendor/bin/soap-client generate:clientfactory Usage: generate:clientfactory [options] Options: --config=CONFIG The location of the soap code-generator config file -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ``` -------------------------------- ### Generate SOAP Client Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-client.md Use this command to generate a SOAP client. A configuration file is required to define the client settings and build the classmap. ```bash $ ./vendor/bin/soap-client generate:client [16:13:31] Usage: generate:client [options] Options: --config=CONFIG The location of the soap code-generator config file ``` -------------------------------- ### Implement a Type Replacer for GUID Source: https://github.com/phpro/soap-client/blob/v6.x/docs/drivers/metadata.md Implement the `TypeReplacer` interface to map a specific XSD type (like 'guid') to a fully qualified class name from a third-party library (e.g., `Ramsey\Uuid\UuidInterface`). This ensures generated code uses the specified external class. Remember to handle the actual type conversion in a custom SOAP encoder. ```php use Phpro\SoapClient\Soap\Metadata\Manipulators\TypeReplacer\TypeReplacer; use Soap\Engine\Metadata\Model\XsdType; use Soap\WsdlReader\Metadata\Predicate\IsOfType; class GuidTypeReplacer implements TypeReplacer { public function __invoke(XsdType $xsdType): XsdType { $check = new IsOfType('http://microsoft.com/wsdl/types/', 'guid'); if (!$check($xsdType)) { return $xsdType; } return $xsdType->copy("Ramsey\Uuid\UuidInterface::class"); } } ``` -------------------------------- ### Configure and Enable php-vcr Source: https://github.com/phpro/soap-client/blob/v6.x/docs/testing.md This snippet shows the basic configuration for php-vcr, including setting the cassette path and enabling specific library hooks like 'soap' and 'curl'. Call `turnOn()` to activate VCR. ```php \VCR\VCR::configure() ->setCassettePath('test/fixtures/vcr') ->enableLibraryHooks(['soap', 'curl']) ; \VCR\VCR::turnOn(); ``` -------------------------------- ### Implement a Custom TypeReplacerInterface Source: https://github.com/phpro/soap-client/blob/v6.x/docs/drivers/metadata.md Define a custom type replacer by implementing the TypeReplacerInterface. This example replaces the XSD 'date' type with 'int'. ```php use Phpro\SoapClient\Soap\Metadata\Manipulators\TypeReplacer\TypeReplacer; use Soap\Engine\Metadata\Model\XsdType; use Soap\WsdlReader\Metadata\Predicate\IsOfType; use Soap\Xml\Xmlns; final class MyDateReplacer implements TypeReplacer { public function __invoke(XsdType $xsdType): XsdType { $check = new IsOfType(Xmlns::xsd()->value(), 'date'); if (!$check($xsdType)) { return $xsdType; } return $xsdType->copy('int')->withBaseType('int'); } } ``` -------------------------------- ### Initialize and Use SoapClient Source: https://github.com/phpro/soap-client/blob/v6.x/docs/usage.md Initialize the client using the factory and make a request. The client uses generated value-objects for requests and returns a ResultInterface. ```php $client = MyclientFactory::factory($wsdl = 'http://path.to/your.wsdl'); $response = $client->helloWorld(new HelloWorldRequest('name')); echo $response->getGreeting(); ``` -------------------------------- ### Generate Soap Client Configuration Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Run the `generate:config` command to automatically detect XML namespaces from the WSDL. The command outputs commented-out `withMapping()` and `withStrategy()` suggestions in the generated configuration file. ```bash ./vendor/bin/soap-client generate:config --config=config/soap-client.php ``` -------------------------------- ### Generate Client Factory Source: https://context7.com/phpro/soap-client/llms.txt Generates a factory class that simplifies client instantiation by wiring together the engine, encoder registry, event dispatcher, and caller. ```bash ./vendor/bin/soap-client generate:clientfactory --config=config/soap-client.php ``` -------------------------------- ### GetterAssembler: Add Getter Method Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Adds a getter method to the generated class. For boolean types, the 'is' prefix can be used instead of 'get'. ```php /** * @return string */ public function getProp1() { return $this->prop1; } ``` -------------------------------- ### Generated Class Map Structure Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-classmap.md This is an example of the PHP class map that is generated. It defines a collection of class mappings between WSDL types and your PHP classes. ```php setRuleSet( new Rules\RuleSet( [ new Rules\AssembleRule(new Assembler\PropertyAssembler(Assembler\PropertyAssemblerOptions::create()->withVisibility(PropertyGenerator::VISIBILITY_PROTECTED))), new Rules\AssembleRule(new Assembler\ClassMapAssembler()), ] ) ) ``` -------------------------------- ### ConstructorAssembler with Type Hints Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Enables generating type hints for constructor parameters. Requires passing ConstructorAssemblerOptions with withTypeHints set to true. ```php new ConstructorAssembler((new ConstructorAssemblerOptions())->withTypeHints()) ``` ```php /** * Constructor * * @var string $prop1 * @var int $prop2 */ public function __construct(string $prop1, int $prop2) { $this->prop1 = $prop1; $this->prop2 = $prop2; } ``` -------------------------------- ### ConstructorAssembler with Doc Blocks Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Adds a constructor with class properties. Doc blocks are enabled by default. ```php /** * Constructor * * @var string $prop1 * @var int $prop2 */ public function __construct($prop1, $prop2) { $this->prop1 = $prop1; $this->prop2 = $prop2; } ``` -------------------------------- ### Configure Constructor Default Values with DefaultValuesStrategy Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Utilize the `withDefaultValues()` method on `ConstructorAssemblerOptions` and pass a `DefaultValuesStrategy` to control how default values are applied to constructor parameters. Strategies include `OptionalOnly`, `All`, and `None`. ```php use Phpro\SoapClient\CodeGenerator\Config\DefaultValuesStrategy; (new ConstructorAssemblerOptions())->withDefaultValues(DefaultValuesStrategy::OptionalOnly) (new ConstructorAssemblerOptions())->withDefaultValues(DefaultValuesStrategy::All) (new ConstructorAssemblerOptions())->withDefaultValues(DefaultValuesStrategy::None) ``` -------------------------------- ### Configure SetterAssembler Options Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Configure SetterAssembler by disabling doc blocks and enabling type hints using SetterAssemblerOptions. ```php new SetterAssembler((new SetterAssemblerOptions())->withDocBlocks(false)->withTypeHints()) ``` -------------------------------- ### Match Type Names with TypenameMatchesRule Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/rules.md Apply a sub-rule to SOAP types whose names match a regular expression. This example adds a RequestInterface to classes if their type name ends with 'Request'. ```php use Phpro\SoapClient\CodeGenerator\Assembler; use Phpro\SoapClient\CodeGenerator\Rules; new Rules\TypenameMatchesRule( new Rules\AssembleRule(new Assembler\RequestAssembler()), '/Request$/' ) ``` -------------------------------- ### Create CalculatorClient with EventDispatchingCaller Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Factory method to create a CalculatorClient, injecting an EventDispatchingCaller for event handling. ```php use Symfony\Component\EventDispatcher\EventDispatcher; use Phpro\SoapClient\Soap\DefaultEngineFactory; use Soap\ExtSoapEngine\ExtSoapOptions; use Phpro\SoapClient\Caller\EventDispatchingCaller; use Phpro\SoapClient\Caller\EngineCaller; class CalculatorClientFactory { public static function factory(string $wsdl): CalculatorClient { $engine = DefaultEngineFactory::create( ExtSoapOptions::defaults($wsdl, []) ->withClassMap(CalculatorClassmap::getCollection()) ); $eventDispatcher = new EventDispatcher(); $caller = new EventDispatchingCaller(new EngineCaller($engine), $eventDispatcher); return new CalculatorClient($caller); } } ``` -------------------------------- ### Configure Enumeration Generation Strategy Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/configuration.md Customize how the code generator handles XSD enumeration types using `setEnumerationGenerationStrategy()`. This example opts in to generating PHP Enums for both local and global enumerations. ```php use Phpro\SoapClient\CodeGenerator\Config\Config; use Phpro\SoapClient\CodeGenerator\Config\EnumerationGenerationStrategy; Config::create() ->setEnumerationGenerationStrategy(EnumerationGenerationStrategy::LocalAndGlobal) ``` -------------------------------- ### Configure ImmutableSetterAssembler Options Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Configure ImmutableSetterAssembler by disabling doc blocks and enabling type hints using ImmutableSetterAssemblerOptions. ```php new ImmutableSetterAssembler((new ImmutableSetterAssemblerOptions())->withDocBlocks(false)->withTypeHints()) ``` -------------------------------- ### Configure Custom Type Replacements Source: https://github.com/phpro/soap-client/blob/v6.x/docs/drivers/metadata.md Set up custom type replacements to substitute default XSD type mappings, for example, replacing 'date' with 'int' if an encoder doesn't support DateTimeImmutable. ```php use Phpro\SoapClient\CodeGenerator\Config\Config; use Phpro\SoapClient\Soap\Metadata\Manipulators\TypeReplacer\TypeReplacers; return Config::create() //... ->setTypeReplacements( TypeReplacers::defaults() ->add(new MyDateReplacer()) ) // ... ``` -------------------------------- ### Configure Type Namespace Map with Strategy Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/configuration.md Use `TypeNamespaceMap::create()` with `withStrategy()` to automatically resolve type destinations based on XML namespace prefixes. A strategy is invoked for namespaces without explicit mappings. ```php use Phpro\SoapClient\CodeGenerator\Config\Destination; use Phpro\SoapClient\CodeGenerator\TypeNamespaceMap\Strategy\PrefixBasedTypeNamespaceStrategy; TypeNamespaceMap::create(new Destination('src/Type', 'App\Type')) ->withStrategy(new PrefixBasedTypeNamespaceStrategy()) ``` -------------------------------- ### Upgrade soap-client to v2.0 Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Use Composer to require the latest version of the soap-client package. ```bash composer require phpro/soap-client:^2.0 ``` -------------------------------- ### Built-in Assemblers for Code Generation Source: https://context7.com/phpro/soap-client/llms.txt Demonstrates the usage of various built-in assemblers for generating constructors, setters, getters, properties, and other class features. Configure options like type hints, doc blocks, and default values. ```php withTypeHints() ->withDocBlocks(false) ->withDefaultValues(DefaultValuesStrategy::OptionalOnly) // nullable params get = null ); // Immutable setter (returns clone): ->withProp1($val) new Assembler\ImmutableSetterAssembler( (new Assembler\ImmutableSetterAssemblerOptions())->withTypeHints()->withDocBlocks(false) ); // Fluent setter (returns $this): ->setProp1($val) new Assembler\FluentSetterAssembler( (new Assembler\FluentSetterAssemblerOptions())->withTypeHints() ); // Getter: ->getProp1() new Assembler\GetterAssembler( (new Assembler\GetterAssemblerOptions())->withTypeHints()->withDocBlocks(false) ); // Property with visibility control new Assembler\PropertyAssembler( \Phpro\SoapClient\CodeGenerator\Config\PropertyAssemblerOptions::create() ->withVisibility(\Laminas\Code\Generator\PropertyGenerator::VISIBILITY_PROTECTED) ->withDefaultValues(DefaultValuesStrategy::All) ); // Mark class final or abstract new Assembler\FinalClassAssembler(); new Assembler\AbstractClassAssembler(); // Make class JSON-serializable new Assembler\JsonSerializableAssembler(); // Make class iterable over a list property new Assembler\IteratorAssembler(); // Add strict_types=1 to the file new Assembler\StrictTypesAssembler(); // Add interface / trait / use statement new Assembler\InterfaceAssembler(\App\MyInterface::class); new Assembler\TraitAssembler(\App\MyTrait::class); new Assembler\UseAssembler(\App\SomeClass::class, 'Alias'); // Mark as request / result types new Assembler\RequestAssembler(); new Assembler\ResultAssembler(); new Assembler\ResultProviderAssembler(\Phpro\SoapClient\Type\MixedResult::class); ``` -------------------------------- ### Test SOAP Client with php-vcr Source: https://context7.com/phpro/soap-client/llms.txt Record and replay SOAP HTTP interactions using `php-vcr` for testing. The first run records the fixture; subsequent runs replay it without network calls. Ensure `VCR::configure` is called once, typically in a bootstrap file. ```php setCassettePath('test/fixtures/vcr') ->enableLibraryHooks(['soap', 'curl']); \VCR\VCR::turnOn(); class HelloWorldClientTest extends TestCase { private MyServiceClient $client; protected function setUp(): void { $this->client = MyServiceClientFactory::factory('https://example.com/service.wsdl'); } /** * @test * @vcr hello-world.yml */ public function it_returns_a_greeting(): void { $response = $this->client->helloWorld(new HelloWorldRequest('Alice')); self::assertSame('Hello Alice', $response->getGreeting()); // First run: records test/fixtures/vcr/hello-world.yml // Subsequent runs: replays from fixture, no network call made } } ``` -------------------------------- ### Generate Class Map CLI Command Source: https://github.com/phpro/soap-client/blob/v6.x/docs/cli/generate-classmap.md Use this command to generate a class map. A configuration file is required to set the class map name, namespace, and destination. ```bash $ ./vendor/bin/soap-client generate:classmap [16:13:31] Usage: generate:classmap [options] Options: --config=CONFIG The location of the soap code-generator config file -h, --help Display this help message -q, --quiet Do not output any message -V, --version Display this application version --ansi Force ANSI output --no-ansi Disable ANSI output -n, --no-interaction Do not ask any interactive question -v|vv|vvv, --verbose Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug ``` -------------------------------- ### Replace XSD Types with Custom PHP Types Source: https://context7.com/phpro/soap-client/llms.txt Define custom replacers to map XSD types to specific PHP classes. Useful for mapping `xsd:date` to `DateTimeImmutable` or `xsd:guid` to a UUID object. Ensure the target PHP classes are available in your project. ```php copy("Ramsey\Uuid\UuidInterface::class"); } } // Replace xsd:date with int (unix timestamp) for a custom encoder class UnixDateReplacer implements TypeReplacer { public function __invoke(XsdType $xsdType): XsdType { $check = new IsOfType('http://www.w3.org/2001/XMLSchema', 'date'); if (!$check($xsdType)) { return $xsdType; } return $xsdType->copy('int')->withBaseType('int'); } } Config::create() ->setTypeReplacements( TypeReplacers::defaults() ->add(new GuidTypeReplacer()) ->add(new UnixDateReplacer()) ); ``` -------------------------------- ### Regenerate Classes with V3 Config Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Execute the commands to regenerate client classes, classmaps, and types after updating the configuration for V3. ```bash ./vendor/bin/soap-client generate:client --config=config/soap-client.php ./vendor/bin/soap-client generate:classmap --config=config/soap-client.php ./vendor/bin/soap-client generate:types --config=config/soap-client.php ``` -------------------------------- ### Configuring the SOAP Engine Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/configuration.md Specify how the code generation tool interacts with SOAP by configuring the engine. This includes WSDL loading, encoder registries, and optional settings like WSDL caching or alternate HTTP configurations. ```php use Phpro\SoapClient\Soap\EngineOptions; use Phpro\SoapClient\Soap\DefaultEngineFactory; DefaultEngineFactory::create( EngineOptions::defaults($wsdl) ->withWsdlLoader(new FlatteningLoader(new StreamWrapperLoader())) ->withEncoderRegistry( EncoderRegistry::default() ->addClassMapCollection(SomeClassmap::types()) ->addBackedEnumClassMapCollection(SomeClassmap::enums()) ) // If you want to enable WSDL caching: // ->withCache() // If you want to use Alternate HTTP settings: // ->withWsdlLoader() // ->withTransport() // If you want specific SOAP setting: // ->withWsdlParserContext() // ->withWsdlServiceSelectionCriteria() ); ``` -------------------------------- ### ConstructorAssembler with Disabled Doc Blocks and Type Hints Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Disables doc block generation while enabling type hints. Pass ConstructorAssemblerOptions with withDocBlocks(false) and withTypeHints(). ```php new ConstructorAssembler((new ConstructorAssemblerOptions())->withDocBlocks(false)->withTypeHints()) ``` ```php public function __construct(string $prop1, int $prop2) { $this->prop1 = $prop1; $this->prop2 = $prop2; } ``` -------------------------------- ### FluentSetterAssembler with Doc Blocks Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/assemblers.md Adds a fluent setter method to the generated class, returning the current instance for chaining. Doc blocks are enabled by default. ```php /** * @param string $prop1 * @return $this */ public function setProp1($prop1) { $this->prop1 = $prop1; return $this; } ``` -------------------------------- ### Generate Typed SOAP Client Source: https://context7.com/phpro/soap-client/llms.txt Generates a client class with explicitly typed methods for each SOAP operation. Uses generated value-objects for parameter and return types. ```bash ./vendor/bin/soap-client generate:client --config=config/soap-client.php ``` -------------------------------- ### Regenerate Soap Client Classes Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md After upgrading, regenerate all client-related classes using the provided `generate:` commands. This ensures that the generated code reflects the latest configuration and improvements. ```bash ./vendor/bin/soap-client generate:client --config=config/soap-client.php ``` ```bash ./vendor/bin/soap-client generate:classmap --config=config/soap-client.php ``` ```bash ./vendor/bin/soap-client generate:types --config=config/soap-client.php ``` ```bash ./vendor/bin/soap-client generate:clientfactory --config=config/soap-client.php ``` -------------------------------- ### Implement Custom RuleInterface Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/rules.md Create custom code generation rules by implementing the RuleInterface. This interface requires defining the 'appliesToContext' and 'apply' methods. ```php /** * Interface RuleInterface * * @package Phpro\SoapClient\CodeGenerator\Rules */ interface RuleInterface { /** * @param ContextInterface $context * * @return bool */ public function appliesToContext(ContextInterface $context); /** * @param ContextInterface $context */ public function apply(ContextInterface $context); } ``` -------------------------------- ### Configure Coding Standards Strategy Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Customize naming conventions for generated code by setting a new coding standards strategy. The default strategy matches previous behavior. ```php use Phpro\SoapClient\CodeGenerator\CodingStandards\CodingStandardsStrategyInterface; use Phpro\SoapClient\CodeGenerator\Config\Config; return ($config = Config::create()) ->setCodingStandards(new MyCodingStandards()) ->setEngine(...) ->setTypeNamespaceMap(...) // ... ``` -------------------------------- ### Implement Custom Type Namespace Strategy Source: https://github.com/phpro/soap-client/blob/v6.x/docs/code-generation/configuration.md Create a custom strategy for type namespace mapping by implementing the `TypeNamespaceMapStrategyInterface`. This allows for custom resolution logic when determining the destination for types. ```php use Phpro\SoapClient\CodeGenerator\Config\Destination; use Phpro\SoapClient\CodeGenerator\TypeNamespaceMap\Strategy\TypeNamespaceMapStrategyInterface; final readonly class MyCustomStrategy implements TypeNamespaceMapStrategyInterface { public function __invoke(string $xmlns, string $xmlNamespaceName, Destination $fallback): Destination { // Your custom resolution logic here return $fallback; } } ``` -------------------------------- ### Combine Explicit Mappings with Strategy-Based Resolution Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Combine explicit XML namespace mappings with a strategy-based resolution for comprehensive type organization. Explicit mappings override strategy-based resolutions when conflicts arise. ```php TypeNamespaceMap::create(new Destination('src/Type', 'App\Type')) ->withMapping('http://special.example.com', new Destination('src/Type/Special', 'App\Type\Special')) ->withStrategy(new PrefixBasedTypeNamespaceStrategy($config->getCodingStandards())) ``` -------------------------------- ### Update Soap Client to V3 Source: https://github.com/phpro/soap-client/blob/v6.x/UPGRADING.md Update the soap-client package to version 3.0.0 or higher, including its dependencies. ```bash composer require 'phpro/soap-client:^3.0.0' --update-with-dependencies ```