|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* File containing the eZ\Publish\Core\Repository\Values\Content\Location class. |
|
5
|
|
|
* |
|
6
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
7
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
namespace eZ\Publish\Core\Repository\Values\Content; |
|
10
|
|
|
|
|
11
|
|
|
use Closure; |
|
12
|
|
|
use eZ\Publish\API\Repository\Values\Content\Location as APILocation; |
|
13
|
|
|
use eZ\Publish\API\Repository\Values\Content\LocationList; |
|
14
|
|
|
use eZ\Publish\Core\Repository\LocationListFactory\ChildrenLazySearchQuery; |
|
15
|
|
|
use eZ\Publish\Core\Repository\TreeAccessor\TreeDelegate; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* This class represents a location in the repository. |
|
19
|
|
|
* |
|
20
|
|
|
* @internal Meant for internal use by Repository, type hint against API object instead. |
|
21
|
|
|
*/ |
|
22
|
|
|
class Location extends APILocation |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Content info of the content object of this location. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \eZ\Publish\API\Repository\Values\Content\ContentInfo |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $contentInfo; |
|
30
|
|
|
|
|
31
|
|
|
/** @var array */ |
|
32
|
|
|
protected $path; |
|
33
|
|
|
|
|
34
|
|
|
/** @var \eZ\Publish\Core\Repository\TreeAccessor\TreeDelegate */ |
|
35
|
|
|
private $treeDelegate; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Returns the content info of the content object of this location. |
|
39
|
|
|
* |
|
40
|
|
|
* @return \eZ\Publish\API\Repository\Values\Content\ContentInfo |
|
41
|
|
|
*/ |
|
42
|
|
|
public function getContentInfo() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->contentInfo; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Function where list of properties are returned. |
|
49
|
|
|
* |
|
50
|
|
|
* Override to add dynamic properties |
|
51
|
|
|
* |
|
52
|
|
|
* @uses \parent::getProperties() |
|
53
|
|
|
* |
|
54
|
|
|
* @param array $dynamicProperties |
|
55
|
|
|
* |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function getProperties($dynamicProperties = ['contentId']) |
|
59
|
|
|
{ |
|
60
|
|
|
return parent::getProperties($dynamicProperties); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Magic getter for retrieving convenience properties. |
|
65
|
|
|
* |
|
66
|
|
|
* @param string $property The name of the property to retrieve |
|
67
|
|
|
* |
|
68
|
|
|
* @return mixed |
|
69
|
|
|
*/ |
|
70
|
|
|
public function __get($property) |
|
71
|
|
|
{ |
|
72
|
|
|
switch ($property) { |
|
73
|
|
|
case 'contentId': |
|
74
|
|
|
return $this->contentInfo->id; |
|
75
|
|
|
case 'path': |
|
76
|
|
|
if ($this->path !== null) { |
|
77
|
|
|
return $this->path; |
|
78
|
|
|
} |
|
79
|
|
|
if (isset($this->pathString[1]) && $this->pathString[0] === '/') { |
|
80
|
|
|
return $this->path = explode('/', trim($this->pathString, '/')); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $this->path = []; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return parent::__get($property); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Magic isset for signaling existence of convenience properties. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $property |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool |
|
95
|
|
|
*/ |
|
96
|
|
|
public function __isset($property) |
|
97
|
|
|
{ |
|
98
|
|
|
if ($property === 'contentId' || $property === 'path') { |
|
99
|
|
|
return true; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return parent::__isset($property); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function getChildren(): iterable |
|
106
|
|
|
{ |
|
107
|
|
|
return $this->treeDelegate->getChildren($this); |
|
|
|
|
|
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
public function getSiblings(): iterable |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->treeDelegate->getSiblings($this); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getAncestors(): iterable |
|
116
|
|
|
{ |
|
117
|
|
|
return $this->treeDelegate->getAncestors($this); |
|
118
|
|
|
} |
|
119
|
|
|
} |
|
120
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.