-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathArrayMapper.php
More file actions
74 lines (65 loc) · 1.56 KB
/
ArrayMapper.php
File metadata and controls
74 lines (65 loc) · 1.56 KB
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
<?php
/**
* Created by PhpStorm.
* User: dam
* Date: 03/12/15
* Time: 10:38
*/
namespace Mouf\Utils\DataSource\Mappers;
use Mouf\Utils\DataSource\Interfaces\DataSourceInterface;
use Mouf\Utils\DataSource\Interfaces\PickerInterface;
use Mouf\Utils\DataSource\Row;
class ArrayMapper implements DataSourceInterface
{
/**
* @var PickerInterface[]
*/
private $matchers;
/**
* @var DataSourceInterface
*/
private $dataSource;
/**
* @var array
*/
private $params;
/**
* ArrayMapper constructor.
* @Important
* @param array<string,PickerInterface> $matchers
* @param DataSourceInterface $dataSource
* @param array $params
*/
public function __construct(array $matchers, DataSourceInterface $dataSource, array $params = array())
{
$this->matchers = $matchers;
$this->dataSource = $dataSource;
$this->params = $params;
}
/**
* @return array[]
*/
public function getData()
{
$data = [];
foreach ($this->dataSource->getData() as $row) {
foreach ($this->matchers as $picker){
if (!($return = $picker->pick(new Row($row)))) {
continue;
}
$data [] = $return;
}
}
return $data;
}
/**
* @param array $params
*/
public function setParameters(array $params)
{
if(empty($this->params)) {
$this->params = $params;
}
$this->dataSource->setParameters($this->params);
}
}