Completed
Push — master ( a682b4...c5625a )
by André
14:36
created

Location::getSortClauses()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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