Passed
Push — ft/urls ( e40d31...cdb8a8 )
by Ben
37:03
created

UrlSlugField::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 9
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
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 70
    public function setBaseUrlSegment($baseUrlSegment = null)
21
    {
22 70
        $this->baseUrlSegment = $baseUrlSegment;
23
24 70
        return $this;
25
    }
26
27 5
    public function value()
28
    {
29 5
        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...
30
    }
31
32 5
    private function rawSlugValue(): string
33
    {
34 5
        if(!$this->urlRecord) return '';
35
36 4
        $slug = $this->urlRecord->slug;
37
38 4
        if($this->startsWithBaseUrlSegment($slug)){
39 2
            $slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/');
40
        }
41
42 4
        return $slug;
43
    }
44
45
    /**
46
     * @param $value
47
     * @return bool
48
     */
49 4
    private function startsWithBaseUrlSegment($value): bool
50
    {
51 4
        return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment));
52
    }
53
54 2
    public function toArray(): array
55
    {
56 2
        return array_merge($this->values, [
57 2
            '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...
58 2
            '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...
59 2
            '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...
60 2
            '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...
61 2
            '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...
62 2
            'value' => $this->value(),
63
        ]);
64
    }
65
}