|
1
|
|
|
<?php |
|
2
|
|
|
namespace Balloon\Factory; |
|
3
|
|
|
|
|
4
|
|
|
use Balloon\Balloon; |
|
5
|
|
|
use Balloon\Reader\Factory\FileReaderFactory; |
|
6
|
|
|
use Balloon\Reader\Factory\IFileReaderFactory; |
|
7
|
|
|
use Balloon\Reader\IFileReader; |
|
8
|
|
|
use Balloon\Format\Json; |
|
9
|
|
|
use Balloon\Format\Yaml; |
|
10
|
|
|
use Balloon\Mapper\DataMapper; |
|
11
|
|
|
use Balloon\Mapper\DataMapperDecorator; |
|
12
|
|
|
use Balloon\Proxy\FileReaderCache; |
|
13
|
|
|
use Balloon\Proxy\FileReaderProxy; |
|
14
|
|
|
use ICanBoogie\Inflector; |
|
15
|
|
|
use JMS\Serializer\Serializer; |
|
16
|
|
|
use JMS\Serializer\SerializerBuilder; |
|
17
|
|
|
use Symfony\Component\Yaml\Dumper; |
|
18
|
|
|
use Symfony\Component\Yaml\Parser; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class BalloonFactory |
|
22
|
|
|
* @package Balloon\Factory |
|
23
|
|
|
* @author Raphaël Lefebvre <[email protected]> |
|
24
|
|
|
*/ |
|
25
|
|
|
class BalloonFactory |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var IFileReaderFactory |
|
29
|
|
|
*/ |
|
30
|
|
|
private $fileReaderBridgeFactory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var Serializer |
|
34
|
|
|
*/ |
|
35
|
|
|
private $serializer; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param IFileReaderFactory $fileReaderBridgeFactory |
|
|
|
|
|
|
39
|
|
|
* @param Serializer $serializer |
|
|
|
|
|
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(IFileReaderFactory $fileReaderBridgeFactory = null, Serializer $serializer = null) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->fileReaderBridgeFactory = $fileReaderBridgeFactory ? : new FileReaderFactory(); |
|
44
|
|
|
$this->serializer = $serializer ? : SerializerBuilder::create()->build(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param string $filePath |
|
49
|
|
|
* @param string $className |
|
50
|
|
|
* @param string $primaryKey |
|
51
|
|
|
* @return Balloon |
|
52
|
|
|
*/ |
|
53
|
|
|
public function create($filePath, $className = '', $primaryKey = '') |
|
54
|
|
|
{ |
|
55
|
|
|
$format = pathinfo($filePath, PATHINFO_EXTENSION); |
|
56
|
|
|
if(!method_exists($this, $format)){ |
|
57
|
|
|
throw new \InvalidArgumentException(sprintf('Format %s not supported', $format)); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return $this->$format($filePath, $className, $primaryKey); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $filePath |
|
65
|
|
|
* @param string $className |
|
66
|
|
|
* @param string $primaryKey |
|
67
|
|
|
* @param int $flags |
|
|
|
|
|
|
68
|
|
|
* @return Balloon |
|
69
|
|
|
*/ |
|
70
|
|
|
public function json($filePath, $className = '', $primaryKey = '', $flags = null) |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->instantiate( |
|
73
|
|
|
new Json($this->fileReaderBridgeFactory->create($filePath), $flags), |
|
74
|
|
|
$className, |
|
75
|
|
|
$primaryKey |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $filePath |
|
81
|
|
|
* @param string $className |
|
82
|
|
|
* @param string $primaryKey |
|
83
|
|
|
* @param int $dumpLevel |
|
84
|
|
|
* @return Balloon |
|
85
|
|
|
*/ |
|
86
|
|
|
public function yml($filePath, $className = '', $primaryKey = '', $dumpLevel = 0) |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->instantiate( |
|
89
|
|
|
new Yaml( |
|
90
|
|
|
$this->fileReaderBridgeFactory->create($filePath), |
|
91
|
|
|
new Parser(), |
|
92
|
|
|
new Dumper(), |
|
93
|
|
|
$dumpLevel |
|
94
|
|
|
), |
|
95
|
|
|
$className, |
|
96
|
|
|
$primaryKey |
|
97
|
|
|
); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function getClassName() |
|
101
|
|
|
{ |
|
102
|
|
|
return 'Balloon\Balloon'; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param IFileReader $formatDecorator |
|
107
|
|
|
* @param string $className |
|
108
|
|
|
* @param string $primaryKey |
|
109
|
|
|
* @return Balloon |
|
110
|
|
|
*/ |
|
111
|
|
|
private function instantiate(IFileReader $formatDecorator, $className, $primaryKey) |
|
112
|
|
|
{ |
|
113
|
|
|
$balloon = $this->getClassName(); |
|
114
|
|
|
return new $balloon( |
|
115
|
|
|
new FileReaderProxy( |
|
116
|
|
|
new DataMapperDecorator( |
|
117
|
|
|
$formatDecorator, |
|
118
|
|
|
new DataMapper( |
|
119
|
|
|
$this->serializer, |
|
120
|
|
|
Inflector::get(), |
|
121
|
|
|
$className |
|
122
|
|
|
) |
|
123
|
|
|
), |
|
124
|
|
|
new FileReaderCache() |
|
125
|
|
|
), |
|
126
|
|
|
$primaryKey |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.