1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Phalcon Framework. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Phalcon\Html\Link; |
13
|
|
|
|
14
|
|
|
use Psr\Link\LinkInterface; |
15
|
|
|
use Psr\Link\LinkProviderInterface; |
16
|
|
|
use Traversable; |
17
|
|
|
|
18
|
|
|
use function in_array; |
19
|
|
|
use function spl_object_hash; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Class LinkProvider |
23
|
|
|
* |
24
|
|
|
* @package Phalcon\Link |
25
|
|
|
* |
26
|
|
|
* @property LinkInterface[] $links |
27
|
|
|
*/ |
28
|
|
|
class LinkProvider implements LinkProviderInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var LinkInterface[] |
32
|
|
|
*/ |
33
|
|
|
protected $links = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* LinkProvider constructor. |
37
|
|
|
* |
38
|
|
|
* @param array $links |
39
|
|
|
*/ |
40
|
8 |
|
public function __construct(array $links = []) |
41
|
|
|
{ |
42
|
8 |
|
foreach ($links as $link) { |
43
|
7 |
|
if ($link instanceof LinkInterface) { |
44
|
7 |
|
$this->links[$this->getKey($link)] = $link; |
45
|
|
|
} |
46
|
|
|
} |
47
|
8 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns an iterable of LinkInterface objects. |
51
|
|
|
* |
52
|
|
|
* The iterable may be an array or any PHP \Traversable object. If no links |
53
|
|
|
* are available, an empty array or \Traversable MUST be returned. |
54
|
|
|
* |
55
|
|
|
* @return LinkInterface[]|Traversable |
56
|
|
|
*/ |
57
|
4 |
|
public function getLinks() |
58
|
|
|
{ |
59
|
4 |
|
return $this->links; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Returns an iterable of LinkInterface objects that have a specific |
64
|
|
|
* relationship. |
65
|
|
|
* |
66
|
|
|
* The iterable may be an array or any PHP \Traversable object. If no links |
67
|
|
|
* with that relationship are available, an empty array or \Traversable |
68
|
|
|
* MUST be returned. |
69
|
|
|
* |
70
|
|
|
* @return LinkInterface[]|Traversable |
71
|
|
|
*/ |
72
|
2 |
|
public function getLinksByRel($rel) |
73
|
|
|
{ |
74
|
2 |
|
$links = []; |
75
|
2 |
|
foreach ($this->links as $link) { |
76
|
2 |
|
$rels = $link->getRels(); |
77
|
2 |
|
if (in_array($rel, $rels)) { |
78
|
2 |
|
$links[] = $link; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
return $links; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the object hash key |
87
|
|
|
* |
88
|
|
|
* @param LinkInterface $link |
89
|
|
|
* |
90
|
|
|
* @return string |
91
|
|
|
*/ |
92
|
7 |
|
protected function getKey(LinkInterface $link): string |
93
|
|
|
{ |
94
|
7 |
|
return spl_object_hash($link); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|