-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractDataProvider.php
80 lines (64 loc) · 1.9 KB
/
AbstractDataProvider.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
<?php
namespace Kora\DataProvider;
use Kora\DataProvider\OperatorDefinition\FilterOperatorDefinitionInterface;
use Kora\DataProvider\OperatorDefinition\OrderOperatorDefinitionInterface;
use Kora\DataProvider\OperatorDefinition\PagerOperatorDefinitionInterface;
/**
* Class AbstractDataProvider
* @author Paweł Gierlasiński <[email protected]>
*/
abstract class AbstractDataProvider implements DataProviderInterface
{
/**
* @var OperatorImplementationsList
*/
protected $implementationsList;
/**
* @var Mapper
*/
protected $mapper;
/**
* AbstractDataProvider constructor.
* @param OperatorImplementationsList $implementationsList
* @param Mapper $mapper
*/
public function __construct(OperatorImplementationsList $implementationsList, Mapper $mapper)
{
$this->implementationsList = $implementationsList;
$this->mapper = $mapper;
}
/**
* @return Mapper
*/
public function getMapper(): Mapper
{
return $this->mapper;
}
/**
* @param DataProviderOperatorsSetup $setup
*/
protected function applySetup(DataProviderOperatorsSetup $setup)
{
foreach ($setup->getOperators() as $operator) {
$operatorCode = OperatorImplementationsList::getOperatorCode($operator);
if (!$this->implementationsList->hasImplementation($operatorCode)) continue;
$operatorImplementation = $this->implementationsList->getImplementation($operatorCode);
$operatorImplementation->apply($this, $operator);
}
}
/**
* @param DataProviderOperatorsSetup $setup
* @return Result
*/
public function fetchData(DataProviderOperatorsSetup $setup): Result
{
$this->applySetup($setup);
$data = $this->fetchFromDataSource();
$count = $this->count();
return new Result($setup, $data, $count);
}
/**
* @return array
*/
abstract protected function fetchFromDataSource(): array;
}