Country   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 26
c 2
b 1
f 0
dl 0
loc 91
rs 10
wmc 6

4 Methods

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