|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the puli/repository package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Puli\Repository\Resource; |
|
13
|
|
|
|
|
14
|
|
|
use Puli\Repository\Api\Resource\PuliResource; |
|
15
|
|
|
use Puli\Repository\Api\ResourceNotFoundException; |
|
16
|
|
|
use Puli\Repository\Resource\Collection\ArrayResourceCollection; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* A link resource targeting to another resource. |
|
20
|
|
|
* |
|
21
|
|
|
* @since 1.0 |
|
22
|
|
|
* |
|
23
|
|
|
* @author Bernhard Schussek <[email protected]> |
|
24
|
|
|
* @author Titouan Galopin <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class LinkResource extends GenericResource implements PuliResource |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $targetPath; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $targetPath |
|
35
|
|
|
* @param string|null $path |
|
36
|
|
|
*/ |
|
37
|
58 |
|
public function __construct($targetPath, $path = null) |
|
38
|
|
|
{ |
|
39
|
58 |
|
parent::__construct($path); |
|
40
|
|
|
|
|
41
|
58 |
|
$this->targetPath = $targetPath; |
|
42
|
58 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
24 |
|
public function getTargetPath() |
|
48
|
|
|
{ |
|
49
|
24 |
|
return $this->targetPath; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* {@inheritdoc} |
|
54
|
|
|
*/ |
|
55
|
8 |
|
public function getTarget() |
|
56
|
|
|
{ |
|
57
|
8 |
|
if (!$this->getRepository()) { |
|
58
|
1 |
|
throw ResourceNotFoundException::forPath($this->getTargetPath()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
7 |
|
return $this->getRepository()->get($this->getTargetPath()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
4 |
View Code Duplication |
public function getChild($relPath) |
|
68
|
|
|
{ |
|
69
|
4 |
|
if (!$this->getRepository()) { |
|
70
|
2 |
|
throw ResourceNotFoundException::forPath($this->getTargetPath().'/'.$relPath); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
return $this->getRepository()->get($this->getTargetPath().'/'.$relPath); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* {@inheritdoc} |
|
78
|
|
|
*/ |
|
79
|
3 |
|
public function hasChild($relPath) |
|
80
|
|
|
{ |
|
81
|
3 |
|
if (!$this->getRepository()) { |
|
82
|
1 |
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return $this->getRepository()->contains($this->getTargetPath().'/'.$relPath); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
11 |
|
public function hasChildren() |
|
92
|
|
|
{ |
|
93
|
11 |
|
if (!$this->getRepository()) { |
|
94
|
9 |
|
return false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
2 |
|
return $this->getRepository()->hasChildren($this->getRepositoryPath()); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
5 |
View Code Duplication |
public function listChildren() |
|
104
|
|
|
{ |
|
105
|
5 |
|
$children = new ArrayResourceCollection(); |
|
106
|
|
|
|
|
107
|
5 |
|
if (!$this->getRepository()) { |
|
108
|
3 |
|
return $children; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
2 |
|
foreach ($this->getRepository()->listChildren($this->getTargetPath()) as $child) { |
|
112
|
2 |
|
$children[$child->getName()] = $child; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
2 |
|
return $children; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
7 |
|
protected function preSerialize(array &$data) |
|
119
|
|
|
{ |
|
120
|
7 |
|
parent::preSerialize($data); |
|
121
|
|
|
|
|
122
|
7 |
|
$data[] = $this->targetPath; |
|
123
|
7 |
|
} |
|
124
|
|
|
|
|
125
|
7 |
|
protected function postUnserialize(array $data) |
|
126
|
|
|
{ |
|
127
|
7 |
|
$this->targetPath = array_pop($data); |
|
128
|
|
|
|
|
129
|
7 |
|
parent::postUnserialize($data); |
|
130
|
7 |
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|