|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Soap\WsdlReader\Metadata\Converter\Methods\Configurator\Operation; |
| 5 | + |
| 6 | +use Soap\Engine\Metadata\Model\Method; |
| 7 | +use Soap\Engine\Metadata\Model\MethodMeta; |
| 8 | +use Soap\WsdlReader\Model\Definitions\BindingOperation; |
| 9 | +use Soap\WsdlReader\Model\Definitions\BindingStyle; |
| 10 | +use Soap\WsdlReader\Model\Definitions\BindingUse; |
| 11 | +use Soap\WsdlReader\Model\Definitions\EncodingStyle; |
| 12 | +use Soap\WsdlReader\Model\Definitions\Implementation\Message\HttpMessage; |
| 13 | +use Soap\WsdlReader\Model\Definitions\Implementation\Operation\HttpOperation; |
| 14 | +use Soap\WsdlReader\Model\Definitions\SoapVersion; |
| 15 | + |
| 16 | +final class HttpBindingOperationConfigurator |
| 17 | +{ |
| 18 | + public function __invoke(Method $method, BindingOperation $operation): Method |
| 19 | + { |
| 20 | + $implementation = $operation->implementation; |
| 21 | + if (!$implementation instanceof HttpOperation) { |
| 22 | + return $method; |
| 23 | + } |
| 24 | + |
| 25 | + if (!$this->messageIsConsideredSoap($operation)) { |
| 26 | + return $method; |
| 27 | + } |
| 28 | + |
| 29 | + $guessedSoapVersion = $implementation->transport->guessSoapVersion(); |
| 30 | + $guessedBindingUse = $this->guessBindingUse($guessedSoapVersion); |
| 31 | + $guessedEncodingStyle = $this->guessEncodingStyle($guessedSoapVersion); |
| 32 | + |
| 33 | + return $method->withMeta( |
| 34 | + static fn (MethodMeta $meta): MethodMeta => $meta |
| 35 | + ->withSoapVersion($guessedSoapVersion?->value) |
| 36 | + ->withAction('') // Soap Action is considered empty for HTTP binding. |
| 37 | + ->withOperationName($operation->name) |
| 38 | + ->withBindingStyle(BindingStyle::RPC->value) |
| 39 | + ->withInputBindingUsage($guessedBindingUse?->value) |
| 40 | + ->withInputEncodingStyle($guessedEncodingStyle?->value) |
| 41 | + ->withOutputBindingUsage($guessedBindingUse?->value) |
| 42 | + ->withOutputEncodingStyle($guessedEncodingStyle?->value) |
| 43 | + ); |
| 44 | + } |
| 45 | + |
| 46 | + private function messageIsConsideredSoap(BindingOperation $operation): bool |
| 47 | + { |
| 48 | + $input = $operation->input?->implementation; |
| 49 | + if (!$input instanceof HttpMessage) { |
| 50 | + return false; |
| 51 | + } |
| 52 | + |
| 53 | + return $input->isConsideredSoapContentType(); |
| 54 | + } |
| 55 | + |
| 56 | + private function guessEncodingStyle(?SoapVersion $soapVersion): ?EncodingStyle |
| 57 | + { |
| 58 | + return match ($soapVersion) { |
| 59 | + SoapVersion::SOAP_11 => EncodingStyle::SOAP_11, |
| 60 | + SoapVersion::SOAP_12 => EncodingStyle::SOAP_12_2003_05, |
| 61 | + default => null, |
| 62 | + }; |
| 63 | + } |
| 64 | + |
| 65 | + private function guessBindingUse(?SoapVersion $soapVersion): ?BindingUse |
| 66 | + { |
| 67 | + return match ($soapVersion) { |
| 68 | + SoapVersion::SOAP_11 => BindingUse::ENCODED, |
| 69 | + SoapVersion::SOAP_12 => BindingUse::LITERAL, |
| 70 | + default => null, |
| 71 | + }; |
| 72 | + } |
| 73 | +} |
0 commit comments