1
|
|
|
<?php |
2
|
|
|
namespace Aeq\Hal\Explorer; |
3
|
|
|
|
4
|
|
|
use Aeq\Hal\Explorer; |
5
|
|
|
use Aeq\Hal\Utils\ArrayUtils; |
6
|
|
|
|
7
|
|
|
class LinkFactory |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param Explorer $explorer |
11
|
|
|
* @param string $name |
12
|
|
|
* @param array $data |
13
|
|
|
* @param EmbeddableInterface $parent |
14
|
|
|
* @return LinkInterface |
15
|
|
|
*/ |
16
|
5 |
|
public static function create(Explorer $explorer, $name, array $data, EmbeddableInterface $parent) |
17
|
|
|
{ |
18
|
5 |
|
if (ArrayUtils::isNumericArray($data)) { |
19
|
1 |
|
return self::createCollection($explorer, $name, $data, $parent); |
20
|
|
|
} |
21
|
5 |
|
return self::createLink($explorer, $name, $data, $parent); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Explorer $explorer |
26
|
|
|
* @param string $name |
27
|
|
|
* @param array $data |
28
|
|
|
* @param EmbeddableInterface $parent |
29
|
|
|
* @return Link |
30
|
|
|
*/ |
31
|
5 |
|
public static function createLink(Explorer $explorer, $name, array $data, EmbeddableInterface $parent) |
32
|
|
|
{ |
33
|
5 |
|
return new Link($explorer, $name, $data, $parent); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Explorer $explorer |
38
|
|
|
* @param string $name |
39
|
|
|
* @param array $data |
40
|
|
|
* @param EmbeddableInterface $parent |
41
|
|
|
* @return LinkCollection |
42
|
|
|
*/ |
43
|
1 |
|
public static function createCollection(Explorer $explorer, $name, array $data, EmbeddableInterface $parent) |
44
|
|
|
{ |
45
|
1 |
|
$collection = new LinkCollection($explorer, $name, $parent); |
46
|
1 |
|
foreach ($data as $itemData) { |
47
|
1 |
|
$collection->addLink(self::createLink($explorer, $name, $itemData, $collection)); |
48
|
1 |
|
} |
49
|
1 |
|
return $collection; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|