1
+ <?php
2
+
3
+ namespace Jenko \Sunscreen \Processor ;
4
+
5
+ class AdapterProcessor implements ProcessorInterface
6
+ {
7
+ /**
8
+ * @var string
9
+ */
10
+ private $ interfaceFqn ;
11
+
12
+ /**
13
+ * @var string
14
+ */
15
+ private $ namespace ;
16
+
17
+ /**
18
+ * @param string $interfaceFqn
19
+ * @param string $namespace
20
+ */
21
+ public function __construct ($ interfaceFqn , $ namespace )
22
+ {
23
+ $ this ->interfaceFqn = $ interfaceFqn ;
24
+ $ this ->namespace = $ namespace ;
25
+ }
26
+
27
+ /**
28
+ * {@inheritdoc}
29
+ */
30
+ public function generate ($ asString = false )
31
+ {
32
+ $ reflected = new \ReflectionClass ($ this ->interfaceFqn );
33
+
34
+ $ interfaceName = $ reflected ->getShortName ();
35
+ $ interfaceProperty = lcfirst ($ interfaceName );
36
+
37
+ // TODO: Inject a factory into class then create methods processor through that.
38
+ $ methodsProcessor = new AdapterMethodsProcessor ($ reflected ->getMethods (), $ interfaceProperty );
39
+
40
+ $ adapterString = strtr (
41
+ file_get_contents (__DIR__ . '/../../template/AdapterTemplate.tpl ' ),
42
+ [
43
+ '<namespace> ' => $ this ->namespace ,
44
+ '<interfaceFqn> ' => $ this ->interfaceFqn ,
45
+ '<className> ' => $ this ->makeClassName ($ interfaceName ),
46
+ '<interfaceName> ' => $ interfaceName ,
47
+ '<interfaceProperty> ' => $ interfaceProperty ,
48
+ '<methods> ' => $ methodsProcessor ->generate (true )
49
+ ]
50
+ );
51
+
52
+ if ($ asString === true ) {
53
+ return $ adapterString ;
54
+ }
55
+
56
+ // TODO: Generate file
57
+ }
58
+
59
+ private function makeClassName ($ interfaceName )
60
+ {
61
+ $ interfaceFqnParts = explode ('\\' , $ this ->interfaceFqn );
62
+
63
+ return reset ($ interfaceFqnParts ) . str_replace ('Interface ' , '' , $ interfaceName );
64
+ }
65
+ }
0 commit comments