Passed
Branch beta (1b8e35)
by Jon
07:16
created

Concept::statements()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Helpers\Macros\Traits\Languages;
6
use App\Models\Traits\BelongsToVocabulary;
7
use App\Models\Traits\HasStatus;
8
use Culpa\Traits\Blameable;
9
use Culpa\Traits\CreatedBy;
10
use Culpa\Traits\DeletedBy;
11
use Culpa\Traits\UpdatedBy;
12
use Illuminate\Database\Eloquent\Model;
13
use Illuminate\Database\Eloquent\Relations\HasMany;
14
use Illuminate\Database\Eloquent\SoftDeletes;
15
use Laracasts\Matryoshka\Cacheable;
16
17
/**
18
 * App\Models\Concept.
19
 *
20
 * @property int $id
21
 * @property \Carbon\Carbon|null $created_at
22
 * @property \Carbon\Carbon|null $updated_at
23
 * @property \Carbon\Carbon|null $deleted_at
24
 * @property int|null $created_user_id
25
 * @property int|null $updated_user_id
26
 * @property string $uri
27
 * @property string|null $lexical_alias
28
 * @property string $pref_label
29
 * @property int|null $vocabulary_id
30
 * @property int|null $is_top_concept
31
 * @property int|null $pref_label_id
32
 * @property int $status_id
33
 * @property string $language
34
 * @property int|null $created_by
35
 * @property int|null $updated_by
36
 * @property int|null $deleted_by
37
 * @property-read \App\Models\Access\User\User|null $creator
38
 * @property-read \App\Models\Access\User\User|null $eraser
39
 * @property string $label
40
 * @property mixed $languages
41
 * @property string $name
42
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\ConceptAttribute[] $statements
43
 * @property-read \App\Models\Status $status
44
 * @property-read \App\Models\Access\User\User|null $updater
45
 * @property-read \App\Models\Vocabulary|null $vocabulary
46
 * @method static bool|null forceDelete()
47
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Concept onlyTrashed()
48
 * @method static bool|null restore()
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereCreatedAt($value)
50
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereCreatedBy($value)
51
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereCreatedUserId($value)
52
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereDeletedAt($value)
53
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereDeletedBy($value)
54
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereId($value)
55
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereIsTopConcept($value)
56
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereLanguage($value)
57
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereLexicalAlias($value)
58
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept wherePrefLabel($value)
59
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept wherePrefLabelId($value)
60
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereStatusId($value)
61
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereUpdatedAt($value)
62
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereUpdatedBy($value)
63
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereUpdatedUserId($value)
64
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereUri($value)
65
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\Concept whereVocabularyId($value)
66
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Concept withTrashed()
67
 * @method static \Illuminate\Database\Query\Builder|\App\Models\Concept withoutTrashed()
68
 * @mixin \Eloquent
69
 */
70
class Concept extends Model
71
{
72
    const TABLE           = 'reg_concept';
73
    const FORM_PROPERTIES = [45, 49, 59, 62, 74];
74
    protected $table      = self::TABLE;
75
    use SoftDeletes, Blameable, CreatedBy, UpdatedBy, DeletedBy;
76
    use Cacheable;
77
    use Languages, BelongsToVocabulary, HasStatus;
78
    protected $blameable = [
79
        'created' => 'created_user_id',
80
        'updated' => 'updated_user_id',
81
        'deleted' => 'deleted_by',
82
    ];
83
    protected $dates   = ['deleted_at'];
84
    protected $guarded = ['id'];
85
86
    /*
87
    |--------------------------------------------------------------------------
88
    | FUNCTIONS
89
    |--------------------------------------------------------------------------
90
    */
91
92
    /**
93
     * @param int $projectId
94
     *
95
     * @return array
96
     */
97
    public static function selectConceptsByProject($projectId): array
98
    {
99
        return \DB::table(ConceptAttribute::TABLE)
100
            ->join(self::TABLE,
101
                self::TABLE . '.id',
102
                '=',
103
                ConceptAttribute::TABLE . '.concept_id')
104
            ->join(Vocabulary::TABLE,
105
                Vocabulary::TABLE . '.id',
106
                '=',
107
                self::TABLE . '.vocabulary_id')
108
            ->select(ConceptAttribute::TABLE . '.concept_id as id',
109
                Vocabulary::TABLE . '.name as vocabulary',
110
                ConceptAttribute::TABLE . '.language',
111
                ConceptAttribute::TABLE . '.object as label')
112
            ->where([
113
                [ConceptAttribute::TABLE . '.profile_property_id', 45],
114
                [Vocabulary::TABLE . '.agent_id', $projectId],
115
            ])
116
            ->orderBy(Vocabulary::TABLE . '.name')
117
            ->orderBy(ConceptAttribute::TABLE . '.language')
118
            ->orderBy(ConceptAttribute::TABLE . '.object')
119
            ->get()
120
            ->mapWithKeys(function ($item) {
121
                return [
122
                    $item->id . '_' . $item->language => $item->vocabulary .
123
                        ' - (' .
124
                        $item->language .
125
                        ') ' .
126
                        $item->label,
127
                ];
128
            })
129
            ->toArray();
130
    }
131
132
    public function updateFromStatements(array $statements = null): self
133
    {
134
        $language   = $this->language;
135
        if (! $statements) {
136
            $s          = collect($this->statements->whereIn('profile_property_id', self::FORM_PROPERTIES)->toArray());
137
            $s          = $s->filter(function ($item) use ($language) {
138
                return $item['language'] === $language || $item['language'] === '';
139
            })->keyBy(function ($item) {
140
                return $item['profile_property_id'];
141
            });
142
            $prefLabelId = $s->where('profile_property_id', 45)->first()['id'];
143
            $statements  = $s->map(function ($item) {
144
                return $item['object'];
145
            });
146
            $statements['45-id'] = $prefLabelId;
147
        }
148
149
        if (isset($statements['62'])) {
150
            $this->uri = $statements['62'];
151
        }
152
        if (isset($statements['74'])) {
153
            $this->lexical_alias = $statements['74'];
154
        }
155
        if (isset($statements['45'])) {
156
            $this->pref_label = $statements['45'];
157
            if (isset($statements['45-id'])) {
158
                $this->pref_label_id = $statements['45-id'];
159
            }
160
        }
161
        if (isset($statements['59'])) {
162
            $this->status_id =
163
                is_numeric($statements['59']) ? $statements['59'] :
164
                    Status::getByName($statements['59'])->id;
165
        }
166
167
        $this->save();
168
169
        return $this;
170
    }
171
172
    /*
173
    |--------------------------------------------------------------------------
174
    | RELATIONS
175
    |--------------------------------------------------------------------------
176
    */
177
178
    public function statements(): ?HasMany
179
    {
180
        return $this->hasMany(ConceptAttribute::class, 'concept_id');
181
    }
182
183
    /*
184
    |--------------------------------------------------------------------------
185
    | SCOPES
186
    |--------------------------------------------------------------------------
187
    */
188
189
    /*
190
    |--------------------------------------------------------------------------
191
    | ACCESSORS
192
    |--------------------------------------------------------------------------
193
    */
194
195
    /**
196
     * @return string
197
     */
198
    public function getNameAttribute(): string
199
    {
200
        return $this->pref_label;
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getLabelAttribute(): string
207
    {
208
        return $this->pref_label;
209
    }
210
211
    /*
212
    |--------------------------------------------------------------------------
213
    | MUTATORS
214
    |--------------------------------------------------------------------------
215
    */
216
217
    /**
218
     * @param string $value
219
     *
220
     * @return string|void
221
     */
222
    public function setNameAttribute(string $value): void
223
    {
224
        $this->attributes['pref_label'] = $value;
0 ignored issues
show
Bug introduced by
The property attributes does not seem to exist on App\Models\Concept. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
225
    }
226
227
    /**
228
     * @param string $value
229
     *
230
     * @return string|void
231
     */
232
    public function setLabelAttribute(string $value): void
233
    {
234
        $this->attributes['pref_label'] = $value;
0 ignored issues
show
Bug introduced by
The property attributes does not seem to exist on App\Models\Concept. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
235
    }
236
}
237