Type   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
dl 0
loc 84
c 1
b 0
f 0
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNameAttribute() 0 7 3
A __construct() 0 5 1
A addresses() 0 3 1
1
<?php
2
3
namespace PWWEB\Localisation\Models\Address;
4
5
use Eloquent as Model;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\SoftDeletes;
8
use PWWEB\Core\Traits\Migratable;
9
use PWWEB\Localisation\Contracts\Address\Type as AddressTypeContract;
10
11
/**
12
 * PWWEB\Localisation\Models\Address\Type Model.
13
 *
14
 * Standard Address Type Model.
15
 *
16
 * @author    Frank Pillukeit <[email protected]>
17
 * @author    Richard Browne <[email protected]>
18
 * @copyright 2020 pw-websolutions.com
19
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
20
 * @property  \Illuminate\Database\Eloquent\Collection addresses
21
 * @property  string name
22
 * @property  bool global
23
 */
24
class Type extends Model implements AddressTypeContract
25
{
26
    use Migratable;
27
    use SoftDeletes;
28
29
    const CREATED_AT = 'created_at';
30
    const UPDATED_AT = 'updated_at';
31
32
    /**
33
     * The attributes that should be casted to Carbon dates.
34
     *
35
     * @var string[]
36
     */
37
    protected $dates = [
38
        'deleted_at',
39
    ];
40
41
    /**
42
     * The attributes that can be filled.
43
     *
44
     * @var string[]
45
     */
46
    public $fillable = [
47
        'name',
48
        'global',
49
    ];
50
51
    /**
52
     * The attributes that should be casted to native types.
53
     *
54
     * @var array
55
     */
56
    protected $casts = [
57
        'id' => 'integer',
58
        'name' => 'string',
59
        'global' => 'boolean',
60
    ];
61
62
    /**
63
     * Validation rules.
64
     *
65
     * @var array
66
     */
67
    public static $rules = [
68
        'name' => 'string|required',
69
        'global' => 'boolean|required',
70
    ];
71
72
    /**
73
     * Constructor.
74
     *
75
     * @param array $attributes additional attributes for model initialisation
76
     */
77
    public function __construct(array $attributes = [])
78
    {
79
        parent::__construct($attributes);
80
81
        $this->setTable(config('pwweb.localisation.table_names.address_types'));
82
    }
83
84
    /**
85
     * Accessor for linked Address model.
86
     *
87
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
88
     **/
89
    public function addresses(): HasMany
90
    {
91
        return $this->hasMany(config('pwweb.localisation.models.address'), 'type_id');
92
    }
93
94
    /**
95
     * Obtain the localised name of a country.
96
     *
97
     * @param string $value Original value of the country
98
     *
99
     * @return string|array|null
100
     */
101
    public function getNameAttribute($value)
102
    {
103
        if (null === $value || '' === $value) {
104
            return '';
105
        }
106
107
        return __('pwweb::localisation.'.$value);
108
    }
109
}
110