Passed
Push — 0.3 ( 9a1703...7b1086 )
by Philippe
42:07
created

UrlSlugField::getUrlRecordId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 2
cp 0
rs 10
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Thinktomorrow\Chief\Fields\Types\InputField;
6
7
class UrlSlugField extends InputField
8
{
9
    private $urlRecord;
10
11
    private $baseUrlSegment;
12
13 21
    public function setUrlRecord(UrlRecord $urlRecord)
14
    {
15 21
        $this->urlRecord = $urlRecord;
16
17 21
        return $this;
18
    }
19
20
    public function getUrlRecordId(): int
21
    {
22
        return $this->urlRecord->id;
23
    }
24
25 66
    public function setBaseUrlSegment($baseUrlSegment = null)
26
    {
27 66
        $this->baseUrlSegment = $baseUrlSegment;
28
29 66
        return $this;
30
    }
31
32 4
    public function value()
33
    {
34 4
        return old($this->key, $this->rawSlugValue());
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
35
    }
36
37
    public function fullUrl(): string
38
    {
39
        return $this->prepend.$this->value();
0 ignored issues
show
Bug Best Practice introduced by
The property prepend does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
40
    }
41
42 4
    private function rawSlugValue(): string
43
    {
44 4
        if (!$this->urlRecord) {
45
            return '';
46
        }
47
48 4
        $slug = $this->urlRecord->slug;
49
50
        // If this is a '/' slug, it indicates the homepage for this locale. In this case,
51
        // we wont be trimming the slash
52 4
        if($slug === '/') return $slug;
53
54 4
        if ($this->startsWithBaseUrlSegment($slug)) {
55 2
            $slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/');
56
        }
57
58 4
        return $slug;
59
    }
60
61
    /**
62
     * @param $value
63
     * @return bool
64
     */
65 4
    private function startsWithBaseUrlSegment($value): bool
66
    {
67 4
        return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment));
68
    }
69
70 1
    public function toArray(): array
71
    {
72 1
        return array_merge($this->values, [
73 1
            'key' => $this->key,
0 ignored issues
show
Bug Best Practice introduced by
The property key does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
74 1
            'prepend' => $this->prepend,
0 ignored issues
show
Bug Best Practice introduced by
The property prepend does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
75 1
            'label' => $this->label,
0 ignored issues
show
Bug Best Practice introduced by
The property label does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
76 1
            'placeholder' => $this->placeholder,
0 ignored issues
show
Bug Best Practice introduced by
The property placeholder does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
77 1
            'description' => $this->description,
0 ignored issues
show
Bug Best Practice introduced by
The property description does not exist on Thinktomorrow\Chief\Urls\UrlSlugField. Since you implemented __get, consider adding a @property annotation.
Loading history...
78 1
            'value' => $this->value(),
79 1
            'baseUrlSegment' => $this->baseUrlSegment,
80
            'hint' => null, // Hint placeholder to show url hint when it already exists
81 1
            'is_homepage' => ($this->value() === '/'),
82
        ]);
83
    }
84
}
85