|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the InMemoryList package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace InMemoryList\Infrastructure\Domain\Model; |
|
12
|
|
|
|
|
13
|
|
|
use InMemoryList\Domain\Model\Contracts\ListFactoryInterface as Factory; |
|
14
|
|
|
use InMemoryList\Domain\Model\ListCollection; |
|
15
|
|
|
use InMemoryList\Domain\Model\ListCollectionUuid; |
|
16
|
|
|
use InMemoryList\Domain\Model\ListElement; |
|
17
|
|
|
use InMemoryList\Domain\Model\ListElementUuid; |
|
18
|
|
|
use InMemoryList\Infrastructure\Domain\Model\Exceptions\CreateListFromEmptyArrayException; |
|
19
|
|
|
use InMemoryList\Infrastructure\Domain\Model\Exceptions\NotValidKeyElementInListException; |
|
20
|
|
|
|
|
21
|
|
|
class ListCollectionFactory implements Factory |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $elements |
|
25
|
|
|
* @param array $headers |
|
26
|
|
|
* @param null $uuid |
|
|
|
|
|
|
27
|
|
|
* @param null $elementUuid |
|
|
|
|
|
|
28
|
|
|
* |
|
29
|
|
|
* @return ListCollection |
|
30
|
|
|
* |
|
31
|
|
|
* @throws CreateListFromEmptyArrayException |
|
32
|
|
|
*/ |
|
33
|
|
|
public function create(array $elements, array $headers = [], $uuid = null, $elementUuid = null) |
|
34
|
|
|
{ |
|
35
|
|
|
if (empty($elements)) { |
|
36
|
|
|
throw new CreateListFromEmptyArrayException('Try to create a collection from an empty array.'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$listUuid = new ListCollectionUuid($uuid); |
|
40
|
|
|
$list = new ListCollection($listUuid); |
|
41
|
|
|
|
|
42
|
|
|
foreach ($elements as $element) { |
|
43
|
|
|
$newElementUuid = new ListElementUuid(($elementUuid) ? (string) $this->getValueFromKey($element, $elementUuid) : null); |
|
|
|
|
|
|
44
|
|
|
$list->addElement(new ListElement($newElementUuid, $element)); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
if ($headers) { |
|
|
|
|
|
|
48
|
|
|
$list->setHeaders($headers); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return $list; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param $element |
|
56
|
|
|
* @param $key |
|
57
|
|
|
* |
|
58
|
|
|
* @return mixed |
|
59
|
|
|
* |
|
60
|
|
|
* @throws NotValidKeyElementInListException |
|
61
|
|
|
*/ |
|
62
|
|
|
private function getValueFromKey($element, $key) |
|
63
|
|
|
{ |
|
64
|
|
|
if ((is_object($element) && !$this->getValueKeyFromObject($element, $key)) || (is_array($element) && !isset($element[$key]))) { |
|
65
|
|
|
$getterName = 'get'.str_replace(' ', '', ucwords($key)); |
|
66
|
|
|
|
|
67
|
|
|
throw new NotValidKeyElementInListException($key.' is not a valid key. If your elements are Entities class, please check if you implement '.$getterName.'() method.'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return is_object($element) ? $this->getValueKeyFromObject($element, $key) : $element[$key]; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param $element |
|
75
|
|
|
* @param $key |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
|
|
private function getValueKeyFromObject($element, $key) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!$element instanceof \stdClass) { |
|
82
|
|
|
$getterName = 'get'.str_replace(' ', '', ucwords($key)); |
|
83
|
|
|
|
|
84
|
|
|
return (method_exists($element, $getterName)) ? $element->$getterName() : false; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
return (isset($element->{$key})) ? $element->{$key} : false; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|