Completed
Branch master (6321d1)
by Gawain
10:09 queued 03:32
created

Content::setSlug()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Bolt\Storage\Entity;
3
4
use Bolt\Storage\Collection;
5
use Bolt\Storage\ContentLegacyService;
6
use Bolt\Storage\Mapping\ContentTypeTitleTrait;
7
use Carbon\Carbon;
8
9
/**
10
 * Entity for Content.
11
 */
12
class Content extends Entity
13
{
14
    use ContentRouteTrait;
15
    use ContentTypeTitleTrait;
0 ignored issues
show
Deprecated Code introduced by
The trait Bolt\Storage\Mapping\ContentTypeTitleTrait has been deprecated with message: Find something less fugly for v3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
16
17
    protected $contenttype;
18
    /** @var ContentLegacyService */
19
    protected $_legacy;
20
    /** @var int */
21
    protected $id;
22
    /** @var string */
23
    protected $slug;
24
    /** @var \DateTime */
25
    protected $datecreated;
26
    /** @var \DateTime */
27
    protected $datechanged;
28
    /** @var \DateTime */
29
    protected $datepublish;
30
    /** @var \DateTime */
31
    protected $datedepublish;
32
    /** @var int */
33
    protected $ownerid;
34
    /** @var string */
35
    protected $status;
36
    /** @var Collection\Relations */
37
    protected $relation;
38
    /** @var Collection\Taxonomy */
39
    protected $taxonomy;
40
41
    /** @var array @deprecated Deprecated since 3.0, to be removed in 4.0. */
42
    protected $group;
43
    /** @var int */
44
    protected $sortorder;
45
46
    /**
47
     * Getter for templates using {{ content.get(title) }} functions.
48
     *
49
     * @param string $key
50
     *
51
     * @return mixed
52
     */
53
    public function get($key)
54
    {
55
        if ($key === 'title') {
56
            return $this->getTitle();
57
        }
58
59
        return $this->$key;
60
    }
61
62
    /**
63
     * Setter for content values.
64
     *
65
     * @param string $key
66
     * @param mixed  $value
67
     */
68
    public function set($key, $value)
69
    {
70
        $setter = 'set' . ucfirst($key);
71
        if (is_array($value)) {
72
            $value = array_filter($value);
73
        }
74
        $this->$setter($value);
75
    }
76
77
    /**
78
     * @return int
79
     */
80
    public function getSortorder()
81
    {
82
        return $this->sortorder;
83
    }
84
85
    /**
86
     * @param int $sortorder
87
     */
88
    public function setSortorder($sortorder)
89
    {
90
        $this->sortorder = $sortorder;
91
    }
92
93
    /**
94
     * @return int
95
     */
96
    public function getId()
97
    {
98
        return $this->id;
99
    }
100
101
    /**
102
     * @param int $id
103
     */
104
    public function setId($id)
105
    {
106
        $this->id = $id;
107
    }
108
109
    /**
110
     * @return string
111
     */
112
    public function getSlug()
113
    {
114
        return $this->slug;
115
    }
116
117
    /**
118
     * @param string $slug
119
     */
120
    public function setSlug($slug)
121
    {
122
        $this->slug = $slug;
123
    }
124
125
    /**
126
     * Get creation date.
127
     *
128
     * @return \DateTime
129
     */
130
    public function getDatecreated()
131
    {
132
        if (!$this->datecreated) {
133
            return new Carbon();
134
        }
135
136
        return $this->datecreated;
137
    }
138
139
    /**
140
     * Set creation date.
141
     *
142
     * @param \DateTime|string|null $date
143
     */
144
    public function setDatecreated($date)
145
    {
146
        $this->datecreated = $this->getValidDateObject($date);
147
    }
148
149
    /**
150
     * Get change date.
151
     *
152
     * @return \DateTime
153
     */
154
    public function getDatechanged()
155
    {
156
        if (!$this->datechanged) {
157
            return new Carbon();
158
        }
159
160
        return $this->datechanged;
161
    }
162
163
    /**
164
     * Set change date.
165
     *
166
     * @param \DateTime|string|null $date
167
     */
168
    public function setDatechanged($date)
169
    {
170
        $this->datechanged = $this->getValidDateObject($date);
171
    }
172
173
    /**
174
     * @return \DateTime
175
     */
176
    public function getDatepublish()
177
    {
178
        return $this->datepublish;
179
    }
180
181
    /**
182
     * Set published date.
183
     *
184
     * @param \DateTime|string|null $date
185
     */
186
    public function setDatepublish($date)
187
    {
188
        $this->datepublish = $this->getValidDateObject($date);
189
    }
190
191
    /**
192
     * @return \DateTime
193
     */
194
    public function getDatedepublish()
195
    {
196
        return $this->datedepublish;
197
    }
198
199
    /**
200
     * Set depublished date.
201
     *
202
     * @param \DateTime|string|null $date
203
     */
204
    public function setDatedepublish($date)
205
    {
206
        $this->datedepublish = $this->getValidDateObject($date);
207
    }
208
209
    /**
210
     * @return int
211
     */
212
    public function getOwnerid()
213
    {
214
        return $this->ownerid;
215
    }
216
217
    /**
218
     * @param int $ownerid
219
     */
220
    public function setOwnerid($ownerid)
221
    {
222
        $this->ownerid = $ownerid;
223
    }
224
225
    /**
226
     * @return string
227
     */
228
    public function getStatus()
229
    {
230
        return $this->status;
231
    }
232
233
    /**
234
     * @param string $status
235
     */
236
    public function setStatus($status)
237
    {
238
        $this->status = $status;
239
    }
240
241
    /**
242
     * @return Collection\Relations
243
     */
244
    public function getRelation()
245
    {
246
        if (!$this->relation instanceof Collection\Relations) {
247
            $this->relation = new Collection\Relations();
248
        }
249
250
        return $this->relation;
251
    }
252
253
    /**
254
     * @param Collection\Relations $rel
255
     */
256
    public function setRelation(Collection\Relations $rel)
257
    {
258
        $this->relation = $rel;
259
    }
260
261
    /**
262
     * @return Collection\Taxonomy
263
     */
264
    public function getTaxonomy()
265
    {
266
        if (!$this->taxonomy instanceof Collection\Taxonomy) {
267
            $this->taxonomy = new Collection\Taxonomy();
268
        }
269
270
        return $this->taxonomy;
271
    }
272
273
    /**
274
     * @param Collection\Taxonomy $taxonomy
275
     */
276
    public function setTaxonomy(Collection\Taxonomy $taxonomy)
277
    {
278
        $this->taxonomy = $taxonomy;
279
    }
280
281
    /**
282
     * @return array
283
     */
284
    public function getGroup()
285
    {
286
        return $this->group;
287
    }
288
289
    /**
290
     * @param array $group
291
     */
292
    public function setGroup($group)
293
    {
294
        $this->group = $group;
295
    }
296
297
    /**
298
     * Helper to set an array of values
299
     *
300
     * @param array $values
301
     */
302
    public function setValues(array $values)
303
    {
304
        foreach ($values as $key => $value) {
305
            $this->set($key, $value);
306
        }
307
    }
308
309
    /**
310
     * Getter for a record's 'title' field.
311
     *
312
     * If there is no field called 'title' then we just return the first text
313
     * type field.
314
     *
315
     * @return string
316
     */
317
    public function getTitle()
318
    {
319
        if (isset($this->_fields['title'])) {
320
            return $this->_fields['title'];
321
        }
322
323
        $fieldName = $this->getTitleColumnName($this->contenttype);
324
325
        return $this->$fieldName;
326
    }
327
328
    public function getContenttype()
329
    {
330
        return $this->contenttype;
331
    }
332
333
    public function setContenttype($value)
334
    {
335
        $this->contenttype = $value;
336
    }
337
338
    public function getTemplatefields()
339
    {
340
        return $this->templatefields;
0 ignored issues
show
Documentation introduced by
The property templatefields does not exist on object<Bolt\Storage\Entity\Content>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
341
    }
342
343
    public function setTemplatefields($value)
344
    {
345
        $this->templatefields = $value;
0 ignored issues
show
Documentation introduced by
The property templatefields does not exist on object<Bolt\Storage\Entity\Content>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
346
    }
347
348
    /**
349
     * @return ContentLegacyService
350
     */
351
    public function getLegacy()
352
    {
353
        return $this->_legacy;
354
    }
355
356
    /**
357
     * @param ContentLegacyService $service
358
     */
359
    public function setLegacyService(ContentLegacyService $service)
360
    {
361
        $this->_legacy = $service;
362
        $this->_legacy->initialize($this);
363
    }
364
365
    /**
366
     * Get a valid date property to persist.
367
     *
368
     * @param \DateTime|string|null $date
369
     *
370
     * @return \DateTime|null
371
     */
372
    protected function getValidDateObject($date)
373
    {
374
        if (empty($date)) {
375
            return null;
376
        } elseif (is_string($date)) {
377
            return new Carbon($date);
378
        }
379
380
        return $date;
381
    }
382
}
383