|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Paymaxi\FractalBundle\Resolver; |
|
5
|
|
|
|
|
6
|
|
|
use League\Fractal\TransformerAbstract; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class DirectoryResolver |
|
10
|
|
|
* |
|
11
|
|
|
* @package Paycore\Bundle\PaycoreBundle\Service |
|
12
|
|
|
*/ |
|
13
|
|
|
final class DirectoryResolver implements ResolverInterface |
|
14
|
|
|
{ |
|
15
|
|
|
/** @var string */ |
|
16
|
|
|
private $suffix; |
|
17
|
|
|
|
|
18
|
|
|
/** @var array */ |
|
19
|
|
|
private $map = []; |
|
20
|
|
|
|
|
21
|
|
|
/** @var string */ |
|
22
|
|
|
private $namespaceSuffix; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* DirectoryResolver constructor. |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $classSuffix |
|
28
|
|
|
* @param string $namespaceSuffix |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(string $classSuffix = 'Transformer', string $namespaceSuffix = 'Transformer') |
|
31
|
|
|
{ |
|
32
|
|
|
$this->suffix = $classSuffix; |
|
33
|
|
|
$this->namespaceSuffix = $namespaceSuffix; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $class |
|
39
|
|
|
* |
|
40
|
|
|
* @param $resourceTransformer |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
|
|
public function supports(string $class, $resourceTransformer): bool |
|
45
|
|
|
{ |
|
46
|
|
|
if (array_key_exists($class, $this->map)) { |
|
47
|
|
|
return true; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$parts = explode('\\', $class); |
|
51
|
|
|
$className = array_pop($parts); |
|
52
|
|
|
$transformerClassName = $className . $this->suffix; |
|
53
|
|
|
|
|
54
|
|
|
array_pop($parts); // remove class directory |
|
55
|
|
|
$parts[] = $this->namespaceSuffix; |
|
56
|
|
|
$parts[] = $transformerClassName; |
|
57
|
|
|
|
|
58
|
|
|
$newClass = implode('\\', $parts); |
|
59
|
|
|
|
|
60
|
|
|
if (class_exists($newClass)) { |
|
61
|
|
|
$reflection = new \ReflectionClass($newClass); |
|
62
|
|
|
if ($reflection->hasMethod('__construct') |
|
63
|
|
|
&& 0 !== $reflection->getMethod('__construct')->getNumberOfRequiredParameters() |
|
64
|
|
|
) { |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->map[$class] = new $newClass; |
|
69
|
|
|
return true; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return false; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $class |
|
77
|
|
|
* |
|
78
|
|
|
* @param $resourceTransformer |
|
79
|
|
|
* |
|
80
|
|
|
* @return TransformerAbstract |
|
81
|
|
|
*/ |
|
82
|
|
|
public function resolve(string $class, $resourceTransformer): TransformerAbstract |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->map[$class]; |
|
85
|
|
|
} |
|
86
|
|
|
} |