-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
MethodsParser.php
82 lines (66 loc) · 2.41 KB
/
MethodsParser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
declare(strict_types=1);
namespace Soap\ExtSoapEngine\Metadata;
use Soap\Engine\Metadata\Collection\MethodCollection;
use Soap\Engine\Metadata\Collection\ParameterCollection;
use Soap\Engine\Metadata\Collection\XsdTypeCollection;
use Soap\Engine\Metadata\Model\Method;
use Soap\Engine\Metadata\Model\Parameter;
use Soap\Engine\Metadata\Model\XsdType;
use SoapClient;
final class MethodsParser
{
private XsdTypeCollection $xsdTypes;
public function __construct(XsdTypeCollection $xsdTypes)
{
$this->xsdTypes = $xsdTypes;
}
public function parse(SoapClient $client): MethodCollection
{
return new MethodCollection(...array_map(
fn (string $methodString) => $this->parseMethodFromString($methodString),
array_values((array)$client->__getFunctions())
));
}
private function parseMethodFromString(string $methodString): Method
{
$methodString = $this->transformListResponseToArray($methodString);
return new Method(
$this->parseName($methodString),
$this->parseParameters($methodString),
$this->parseReturnType($methodString)
);
}
private function transformListResponseToArray(string $methodString): string
{
return preg_replace('/^list\(([^\)]*)\)(.*)/i', 'array$2', $methodString);
}
private function parseParameters(string $methodString): ParameterCollection
{
preg_match('/\((.*)\)/', $methodString, $properties);
if (!$properties[1]) {
return new ParameterCollection();
}
$parameters = preg_split('/,\s?/', $properties[1]);
return new ParameterCollection(...array_map(
function (string $parameter): Parameter {
[$type, $name] = explode(' ', trim($parameter));
return new Parameter(
ltrim($name, '$'),
$this->xsdTypes->fetchByNameWithFallback($type)
);
},
$parameters
));
}
private function parseName(string $methodString): string
{
preg_match('/^\w+ (?P<name>\w+)/', $methodString, $matches);
return $matches['name'];
}
private function parseReturnType(string $methodString): XsdType
{
preg_match('/^(?P<returnType>\w+)/', $methodString, $matches);
return $this->xsdTypes->fetchByNameWithFallback($matches['returnType']);
}
}