Completed
Push — dependabot/npm_and_yarn/vue-se... ( e78fac...845b60 )
by
unknown
358:07 queued 338:52
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
    private $fullUrl;
14
15 21
    public function setUrlRecord(UrlRecord $urlRecord)
16
    {
17 21
        $this->urlRecord = $urlRecord;
18
19 21
        return $this;
20
    }
21
22
    public function getUrlRecordId(): int
23
    {
24
        return $this->urlRecord->id;
25
    }
26
27 74
    public function setBaseUrlSegment($baseUrlSegment = null)
28
    {
29 74
        $this->baseUrlSegment = $baseUrlSegment;
30
31 74
        return $this;
32
    }
33
34 4
    public function value()
35
    {
36 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...
37
    }
38
39
    public function fullUrl(): string
40
    {
41
        return $this->fullUrl
42
            ? $this->fullUrl
43
            : $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...
44
    }
45
46
    public function setFullUrl(string $fullUrl)
47
    {
48
        $this->fullUrl = $fullUrl;
49
50
        return $this;
51
    }
52
53 4
    private function rawSlugValue(): string
54
    {
55 4
        if (!$this->urlRecord) {
56
            return '';
57
        }
58
59 4
        $slug = $this->urlRecord->slug;
60
61
        // If this is a '/' slug, it indicates the homepage for this locale. In this case,
62
        // we wont be trimming the slash
63 4
        if ($slug === '/') {
64
            return $slug;
65
        }
66
67 4
        if ($this->startsWithBaseUrlSegment($slug)) {
68 2
            $slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/');
69
        }
70
71 4
        return $slug;
72
    }
73
74
    /**
75
     * @param $value
76
     * @return bool
77
     */
78 4
    private function startsWithBaseUrlSegment($value): bool
79
    {
80 4
        return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment));
81
    }
82
83 1
    public function toArray(): array
84
    {
85 1
        return array_merge($this->values, [
86 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...
87 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...
88 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...
89 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...
90 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...
91 1
            'value' => $this->value(),
92 1
            'baseUrlSegment' => $this->baseUrlSegment,
93
            'hint' => null, // Hint placeholder to show url hint when it already exists
94 1
            'is_homepage' => ($this->value() === '/'),
95 1
            'show' => !!$this->value(),// show input field or not
96
        ]);
97
    }
98
}
99