Completed
Push — ezp_30981_tree_walking ( b1bcb5 )
by
unknown
13:48
created

Location::getChildren()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the eZ\Publish\API\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\API\Repository\Values\Content;
10
11
use eZ\Publish\API\Repository\Exceptions\NotImplementedException;
12
use eZ\Publish\API\Repository\Values\ValueObject;
13
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
14
15
/**
16
 * This class represents a location in the repository.
17
 *
18
 * @property-read \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo calls getContentInfo()
19
 * @property-read mixed $contentId calls getContentInfo()->id
20
 * @property-read mixed $id the id of the location
21
 * @property-read int $priority Position of the Location among its siblings when sorted using priority
22
 * @property-read bool $hidden Indicates that the Location entity is hidden (explicitly or hidden by content).
23
 * @property-read bool $invisible  Indicates that the Location is implicitly marked as hidden by a parent location
24
 * @property-read bool $explicitlyHidden Indicates that the Location entity has been explicitly marked as hidden.
25
 * @property-read string $remoteId a global unique id of the content object
26
 * @property-read mixed $parentLocationId the id of the parent location
27
 * @property-read string $pathString the path to this location e.g. /1/2/4/23 where 23 is current id.
28
 * @property-read array $path Same as $pathString but as array, e.g. [ 1, 2, 4, 23 ]
29
 * @property-read int $depth Depth location has in the location tree
30
 *
31
 * @property-read int $sortField Specifies which property the child locations should be sorted on. Valid values are found at {@link Location::SORT_FIELD_*}
32
 * @property-read int $sortOrder Specifies whether the sort order should be ascending or descending. Valid values are {@link Location::SORT_ORDER_*}
33
 */
34
abstract class Location extends ValueObject
35
{
36
    // @todo Rename these to better fit current naming, also reuse these in Persistence or copy the change over.
37
    const SORT_FIELD_PATH = 1;
38
    const SORT_FIELD_PUBLISHED = 2;
39
    const SORT_FIELD_MODIFIED = 3;
40
    const SORT_FIELD_SECTION = 4;
41
    const SORT_FIELD_DEPTH = 5;
42
    const SORT_FIELD_CLASS_IDENTIFIER = 6;
43
    const SORT_FIELD_CLASS_NAME = 7;
44
    const SORT_FIELD_PRIORITY = 8;
45
    const SORT_FIELD_NAME = 9;
46
47
    /**
48
     * @deprecated
49
     */
50
    const SORT_FIELD_MODIFIED_SUBNODE = 10;
51
52
    const SORT_FIELD_NODE_ID = 11;
53
    const SORT_FIELD_CONTENTOBJECT_ID = 12;
54
55
    const SORT_ORDER_DESC = 0;
56
    const SORT_ORDER_ASC = 1;
57
58
    const STATUS_DRAFT = 0;
59
    const STATUS_PUBLISHED = 1;
60
61
    /**
62
     * Map for Location sort fields to their respective SortClauses.
63
     *
64
     * Those not here (class name/identifier and modified subnode) are
65
     * missing/deprecated and will most likely be removed in the future.
66
     */
67
    const SORT_FIELD_MAP = [
68
        self::SORT_FIELD_PATH => SortClause\Location\Path::class,
69
        self::SORT_FIELD_PUBLISHED => SortClause\DatePublished::class,
70
        self::SORT_FIELD_MODIFIED => SortClause\DateModified::class,
71
        self::SORT_FIELD_SECTION => SortClause\SectionIdentifier::class,
72
        self::SORT_FIELD_DEPTH => SortClause\Location\Depth::class,
73
        //self::SORT_FIELD_CLASS_IDENTIFIER => false,
74
        //self::SORT_FIELD_CLASS_NAME => false,
75
        self::SORT_FIELD_PRIORITY => SortClause\Location\Priority::class,
76
        self::SORT_FIELD_NAME => SortClause\ContentName::class,
77
        //self::SORT_FIELD_MODIFIED_SUBNODE => false,
78
        self::SORT_FIELD_NODE_ID => SortClause\Location\Id::class,
79
        self::SORT_FIELD_CONTENTOBJECT_ID => SortClause\ContentId::class,
80
    ];
81
82
    /**
83
     * Map for Location sort order to their respective Query SORT constants.
84
     */
85
    const SORT_ORDER_MAP = [
86
        self::SORT_ORDER_DESC => Query::SORT_DESC,
87
        self::SORT_ORDER_ASC => Query::SORT_ASC,
88
    ];
89
90
    /**
91
     * Location ID.
92
     *
93
     * @var mixed Location ID.
94
     */
95
    protected $id;
96
97
    /**
98
     * the status of the location.
99
     *
100
     * a location gets the status DRAFT on newly created content which is not published. When content is published the
101
     * location gets the status STATUS_PUBLISHED
102
     *
103
     * @var int
104
     */
105
    public $status = self::STATUS_PUBLISHED;
106
107
    /**
108
     * Location priority.
109
     *
110
     * Position of the Location among its siblings when sorted using priority
111
     * sort order.
112
     *
113
     * @var int
114
     */
115
    protected $priority;
116
117
    /**
118
     * Indicates that the Location entity is hidden (explicitly or hidden by content).
119
     *
120
     * @var bool
121
     */
122
    protected $hidden;
123
124
    /**
125
     * Indicates that the Location is implicitly marked as hidden by a parent
126
     * location.
127
     *
128
     * @var bool
129
     */
130
    protected $invisible;
131
132
    /**
133
     * Indicates that the Location entity has been explicitly marked as hidden.
134
     *
135
     * @var bool
136
     */
137
    protected $explicitlyHidden;
138
139
    /**
140
     * Remote ID.
141
     *
142
     * A universally unique identifier.
143
     *
144
     * @var string
145
     */
146
    protected $remoteId;
147
148
    /**
149
     * Returns the content info of the content object of this location.
150
     *
151
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
152
     */
153
    abstract public function getContentInfo();
154
155
    /**
156
     * Returns true if current location is a draft.
157
     *
158
     * @return bool
159
     */
160
    public function isDraft()
161
    {
162
        return $this->status === self::STATUS_DRAFT;
163
    }
164
165
    /**
166
     * Parent ID.
167
     *
168
     * @var mixed Location ID.
169
     */
170
    protected $parentLocationId;
171
172
    /**
173
     * The materialized path of the location entry, eg: /1/2/.
174
     *
175
     * @var string
176
     */
177
    protected $pathString;
178
179
    /**
180
     * Depth location has in the location tree.
181
     *
182
     * @var int
183
     */
184
    protected $depth;
185
186
    /**
187
     * Specifies which property the child locations should be sorted on.
188
     *
189
     * Valid values are found at {@link Location::SORT_FIELD_*}
190
     *
191
     * @var int
192
     */
193
    protected $sortField;
194
195
    /**
196
     * Specifies whether the sort order should be ascending or descending.
197
     *
198
     * Valid values are {@link Location::SORT_ORDER_*}
199
     *
200
     * @var int
201
     */
202
    protected $sortOrder;
203
204
    /** @var \eZ\Publish\API\Repository\Values\Content\Content */
205
    protected $content;
206
207
    /**
208
     * @return \eZ\Publish\API\Repository\Values\Content\Content
209
     */
210
    public function getContent(): Content
211
    {
212
        return $this->content;
213
    }
214
215
    /**
216
     * Get SortClause objects built from Locations's sort options.
217
     *
218
     * @throws NotImplementedException If sort field has a deprecated/unsupported value which does not have a Sort Clause.
219
     *
220
     * @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause[]
221
     */
222
    public function getSortClauses()
223
    {
224
        $map = self::SORT_FIELD_MAP;
225
        if (!isset($map[$this->sortField])) {
226
            throw new NotImplementedException(
227
                "Sort Clause not implemented for Location sort Field {$this->sortField}"
228
            );
229
        }
230
231
        $sortClause = new $map[$this->sortField]();
232
        $sortClause->direction = self::SORT_ORDER_MAP[$this->sortOrder];
233
234
        return [$sortClause];
235
    }
236
237
    public function getChildren(): iterable
238
    {
239
        return [];
240
    }
241
}
242