1
|
|
|
<?php |
2
|
|
|
namespace Netdudes\DataSourceryBundle\DataSource; |
3
|
|
|
|
4
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Configuration\DataSourceConfigurationInterface; |
5
|
|
|
use Netdudes\DataSourceryBundle\DataSource\Driver\DriverInterface; |
6
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
7
|
|
|
|
8
|
|
|
class DataSourceFactory implements DataSourceFactoryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var DataSourceBuilderFactory |
12
|
|
|
*/ |
13
|
|
|
private $dataSourceBuilderFactory; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param DriverInterface $driver |
17
|
|
|
* @param DataSourceBuilderFactory $dataSourceBuilderFactory |
18
|
|
|
*/ |
19
|
|
|
public function __construct(DriverInterface $driver, DataSourceBuilderFactory $dataSourceBuilderFactory) |
20
|
|
|
{ |
21
|
|
|
$this->driver = $driver; |
|
|
|
|
22
|
|
|
$this->dataSourceBuilderFactory = $dataSourceBuilderFactory; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param DataSourceConfigurationInterface $configuration |
27
|
|
|
* |
28
|
|
|
* @return DataSourceInterface |
29
|
|
|
*/ |
30
|
|
|
public function createFromConfiguration(DataSourceConfigurationInterface $configuration) |
31
|
|
|
{ |
32
|
|
|
$builder = $this->createBuilder($configuration->getEntityClass()); |
33
|
|
|
$configuration->buildDataSource($builder); |
34
|
|
|
|
35
|
|
|
return $builder->build(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param $entityClass |
40
|
|
|
* @param $fields |
41
|
|
|
* @param $transformers |
42
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
43
|
|
|
* |
44
|
|
|
* @return DataSource |
45
|
|
|
*/ |
46
|
|
|
public function create($entityClass, array $fields, array $transformers, EventDispatcherInterface $eventDispatcher) |
47
|
|
|
{ |
48
|
|
|
return new DataSource($entityClass, $fields, $transformers, $eventDispatcher, $this->driver); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $entityClass |
53
|
|
|
* |
54
|
|
|
* @return DataSourceBuilder |
55
|
|
|
*/ |
56
|
|
|
public function createBuilder($entityClass) |
57
|
|
|
{ |
58
|
|
|
return $this->dataSourceBuilderFactory->create($entityClass, $this); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: