Passed
Push — ft/field-contract ( 898325 )
by Ben
10:44
created

UrlSlugField   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 14
eloc 35
c 2
b 1
f 0
dl 0
loc 89
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setBaseUrlSegment() 0 5 1
A setUrlRecord() 0 5 1
A rawSlugValue() 0 19 4
A fullUrl() 0 5 2
A setFullUrl() 0 5 1
A getValue() 0 3 1
A toArray() 0 13 1
A getUrlRecordId() 0 3 1
A startsWithBaseUrlSegment() 0 3 2
1
<?php declare(strict_types=1);
2
3
namespace Thinktomorrow\Chief\Urls;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Thinktomorrow\Chief\Fields\Types\Field;
7
use Thinktomorrow\Chief\Fields\Types\InputField;
8
9
class UrlSlugField extends InputField implements Field
10
{
11
    private $urlRecord;
12
13
    private $baseUrlSegment;
14
15
    private $fullUrl;
16
17
    public function setUrlRecord(UrlRecord $urlRecord)
18
    {
19
        $this->urlRecord = $urlRecord;
20
21
        return $this;
22
    }
23
24
    public function getUrlRecordId(): int
25
    {
26
        return $this->urlRecord->id;
27
    }
28
29
    public function setBaseUrlSegment($baseUrlSegment = null)
30
    {
31
        $this->baseUrlSegment = $baseUrlSegment;
32
33
        return $this;
34
    }
35
36
    public function getValue(Model $model = null, ?string $locale = null)
37
    {
38
        return old($this->key, $this->rawSlugValue());
39
    }
40
41
    public function fullUrl(): string
42
    {
43
        return $this->fullUrl
44
            ? $this->fullUrl
45
            : $this->prepend.$this->getValue();
0 ignored issues
show
Bug introduced by
Are you sure $this->prepend of type array|null|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
            : /** @scrutinizer ignore-type */ $this->prepend.$this->getValue();
Loading history...
46
    }
47
48
    public function setFullUrl(string $fullUrl)
49
    {
50
        $this->fullUrl = $fullUrl;
51
52
        return $this;
53
    }
54
55
    private function rawSlugValue(): string
56
    {
57
        if (!$this->urlRecord) {
58
            return '';
59
        }
60
61
        $slug = $this->urlRecord->slug;
62
63
        // If this is a '/' slug, it indicates the homepage for this locale. In this case,
64
        // we wont be trimming the slash
65
        if ($slug === '/') {
66
            return $slug;
67
        }
68
69
        if ($this->startsWithBaseUrlSegment($slug)) {
70
            $slug = trim(substr($slug, strlen($this->baseUrlSegment)), '/');
71
        }
72
73
        return $slug;
74
    }
75
76
    /**
77
     * @param $value
78
     * @return bool
79
     */
80
    private function startsWithBaseUrlSegment($value): bool
81
    {
82
        return ($this->baseUrlSegment && 0 === strpos($value, $this->baseUrlSegment));
83
    }
84
85
    public function toArray(): array
86
    {
87
        return [
88
            'key' => $this->key,
89
            'prepend' => $this->prepend,
90
            'label' => $this->label,
91
            'placeholder' => $this->placeholder,
92
            'description' => $this->description,
93
            'value' => $this->getValue(),
94
            'baseUrlSegment' => $this->baseUrlSegment,
95
            'hint' => null, // Hint placeholder to show url hint when it already exists
96
            'is_homepage' => ($this->getValue() === '/'),
97
            'show' => !!$this->getValue(),// show input field or not
98
        ];
99
    }
100
}
101