Completed
Push — master ( 6c8698...221303 )
by ruthger
01:38
created

UrlRewrite::getPossibleTypesArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RuthgerIdema\UrlRewrite\Entities;
6
7
use Illuminate\Database\Eloquent\Model;
8
9
class UrlRewrite extends Model
10
{
11
    /** @var int */
12
    public const FORWARD = 0;
13
14
    /** @var int */
15
    public const PERMANENT = 1;
16
17
    /** @var int */
18
    public const TEMPORARY = 2;
19
20
    /** @var array */
21
    protected $fillable = [
22
        'type',
23
        'type_attributes',
24
        'request_path',
25
        'target_path',
26
        'redirect_type',
27
        'description',
28
    ];
29
30
    /** @var array */
31
    protected $casts = [
32
        'type_attributes' => 'array',
33
    ];
34
35
    public function __construct(?array $attributes = [])
36
    {
37
        if (! isset($this->table)) {
38
            $this->setTable(config('url-rewrite.table-name'));
39
        }
40
41
        parent::__construct($attributes);
42
    }
43
44
    public function isForward(): bool
45
    {
46
        return $this->redirect_type === static::FORWARD;
47
    }
48
49
    public function isRedirect(): bool
50
    {
51
        return $this->redirect_type !== static::FORWARD;
52
    }
53
54
    public function getRedirectType(): int
55
    {
56
        return $this->redirect_type === static::PERMANENT ? 301 : 302;
57
    }
58
59
    public function getByTypeAndAttributes(string $type, array $attributes)
60
    {
61
        $query = $this->where('type', $type);
62
63
        foreach ($attributes as $key => $attribute) {
64
            $query = $query->where("type_attributes->$key", (string) $attribute);
65
        }
66
67
        return $query;
68
    }
69
70
    public static function getRedirectTypeOptionsArray(): array
71
    {
72
        return [
73
            static::FORWARD => trans('urlrewrites::translations.forward'),
74
            static::PERMANENT => trans('urlrewrites::translations.permanent'),
75
            static::TEMPORARY => trans('urlrewrites::translations.temporary'),
76
        ];
77
    }
78
79
    public static function getPossibleTypesArray(): array
80
    {
81
        return array_keys(config('url-rewrite.types'));
82
    }
83
}
84