|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReliqArts\Scavenger\Model; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Model as EloquentModel; |
|
8
|
|
|
use Illuminate\Support\Facades\Schema; |
|
9
|
|
|
use JsonException; |
|
10
|
|
|
use ReliqArts\Scavenger\Service\ConfigProvider; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Scavenger Scrap. |
|
14
|
|
|
* |
|
15
|
|
|
* @property string $hash |
|
16
|
|
|
* @property string $model |
|
17
|
|
|
* @property string $source |
|
18
|
|
|
* @property bool|string $data |
|
19
|
|
|
* @property string $title |
|
20
|
|
|
* @property mixed $related |
|
21
|
|
|
* |
|
22
|
|
|
* @method static firstOrCreate(array $array, array $array1 = []) |
|
23
|
|
|
* @method static firstOrNew(array $array, array $array1 = []) |
|
24
|
|
|
*/ |
|
25
|
|
|
class Scrap extends EloquentModel |
|
26
|
|
|
{ |
|
27
|
|
|
private const SOFT_DELETES_TRAIT = 'Illuminate\Database\Eloquent\SoftDeletes'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var array |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $guarded = []; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Get the scraps table. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getTable(): string |
|
38
|
|
|
{ |
|
39
|
|
|
return ConfigProvider::getScrapsTable(); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Convert scrap to target model. |
|
44
|
|
|
* |
|
45
|
|
|
* @param bool $convertDuplicates whether to force conversion even if model already exists |
|
46
|
|
|
* @param bool $storeRelatedReference Whether to update relation field on scrap (self) |
|
47
|
|
|
* N.B. if reference is stored the scrap will be saved. |
|
48
|
|
|
* |
|
49
|
|
|
* @throws JsonException |
|
50
|
|
|
*/ |
|
51
|
|
|
public function convert(bool $convertDuplicates = false, bool $storeRelatedReference = false): ?EloquentModel |
|
52
|
|
|
{ |
|
53
|
|
|
$targetObject = null; |
|
54
|
|
|
$convert = true; |
|
55
|
|
|
|
|
56
|
|
|
if (!empty($this->model)) { |
|
57
|
|
|
$existingRelated = $this->getRelated(); |
|
58
|
|
|
|
|
59
|
|
|
if ($existingRelated !== null && !$convertDuplicates) { |
|
60
|
|
|
return $existingRelated; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ($convert) { |
|
|
|
|
|
|
64
|
|
|
/** @var EloquentModel $targetObject */ |
|
65
|
|
|
$targetObject = new $this->model(); |
|
66
|
|
|
$targetTable = $targetObject->getTable(); |
|
67
|
|
|
|
|
68
|
|
|
// Fill model data with scrap data if attributes exist |
|
69
|
|
|
foreach (json_decode($this->data, true, 512, JSON_THROW_ON_ERROR) as $attr => $val) { |
|
70
|
|
|
if (!ConfigProvider::isSpecialKey($attr) && Schema::hasColumn($targetTable, $attr)) { |
|
71
|
|
|
$targetObject->{$attr} = $val; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// save related model |
|
76
|
|
|
$targetObject->save(); |
|
77
|
|
|
|
|
78
|
|
|
if ($storeRelatedReference) { |
|
79
|
|
|
$this->related = $targetObject->getKey(); |
|
80
|
|
|
$this->save(); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $targetObject; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Convert scrap to target model. |
|
90
|
|
|
* |
|
91
|
|
|
* @noinspection PhpUndefinedMethodInspection |
|
92
|
|
|
*/ |
|
93
|
|
|
public function getRelated(): ?EloquentModel |
|
94
|
|
|
{ |
|
95
|
|
|
if ($this->model && $this->related) { |
|
96
|
|
|
// find relation |
|
97
|
|
|
return $this->relatedModelUsesSoftDeletes() |
|
98
|
|
|
? $this->model::withTrashed()->find($this->related) |
|
99
|
|
|
: $this->model::find($this->related); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return null; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Whether related model uses eloquent's SoftDeletes trait. |
|
107
|
|
|
* |
|
108
|
|
|
* @see \Illuminate\Database\Eloquent\SoftDeletes |
|
109
|
|
|
*/ |
|
110
|
|
|
public function relatedModelUsesSoftDeletes(): bool |
|
111
|
|
|
{ |
|
112
|
|
|
return in_array(self::SOFT_DELETES_TRAIT, class_uses($this->model, true), true); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|