Completed
Push — location_sortclause_mapping_ta... ( c25359 )
by André
14:26
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;
14
use eZ\Publish\API\Repository\Values\Content\Query\SortClause;
15
16
/**
17
 * This class represents a location in the repository.
18
 *
19
 * @property-read \eZ\Publish\API\Repository\Values\Content\ContentInfo $contentInfo calls getContentInfo()
20
 * @property-read mixed $contentId calls getContentInfo()->id
21
 * @property-read mixed $id the id of the location
22
 * @property-read int $priority Position of the Location among its siblings when sorted using priority
23
 * @property-read boolean $hidden Indicates that the Location is explicitly marked as hidden.
24
 * @property-read boolean $invisible  Indicates that the Location is implicitly marked as hidden by a parent location
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 has been explicitly marked as hidden.
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
     * Remote ID.
134
     *
135
     * A universally unique identifier.
136
     *
137
     * @var mixed
138
     */
139
    protected $remoteId;
140
141
    /**
142
     * Returns the content info of the content object of this location.
143
     *
144
     * @return \eZ\Publish\API\Repository\Values\Content\ContentInfo
145
     */
146
    abstract public function getContentInfo();
147
148
    /**
149
     * Returns true if current location is a draft.
150
     *
151
     * @return bool
152
     */
153
    public function isDraft()
154
    {
155
        return $this->status === self::STATUS_DRAFT;
156
    }
157
158
    /**
159
     * Parent ID.
160
     *
161
     * @var mixed Location ID.
162
     */
163
    protected $parentLocationId;
164
165
    /**
166
     * The materialized path of the location entry, eg: /1/2/.
167
     *
168
     * @var string
169
     */
170
    protected $pathString;
171
172
    /**
173
     * Depth location has in the location tree.
174
     *
175
     * @var int
176
     */
177
    protected $depth;
178
179
    /**
180
     * Specifies which property the child locations should be sorted on.
181
     *
182
     * Valid values are found at {@link Location::SORT_FIELD_*}
183
     *
184
     * @var mixed
185
     */
186
    protected $sortField;
187
188
    /**
189
     * Specifies whether the sort order should be ascending or descending.
190
     *
191
     * Valid values are {@link Location::SORT_ORDER_*}
192
     *
193
     * @var mixed
194
     */
195
    protected $sortOrder;
196
197
    /**
198
     * Get SortClause objects built from Locations's sort options.
199
     *
200
     * @throws NotImplementedException If sort field has a deprecated/unsupported value which does not have a Sort Clause.
201
     *
202
     * @return \eZ\Publish\API\Repository\Values\Content\Query\SortClause[]
203
     */
204
    public function getSortClauses()
205
    {
206
        $map = self::SORT_FIELD_MAP;
207
        if (!isset($map[$this->sortField])) {
208
            throw new NotImplementedException(
209
                "Sort clause not implemented for Location sort field with value {$this->sortField}"
210
            );
211
        }
212
213
        $sortClause = new $map[$this->sortField]();
214
        $sortClause->direction = self::SORT_ORDER_MAP[$this->sortOrder];
215
216
        return [$sortClause];
217
    }
218
}
219