1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Okipa\LaravelModelJsonStorage; |
4
|
|
|
|
5
|
|
|
use File; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
|
8
|
|
|
trait ModelJsonStorage |
9
|
|
|
{ |
10
|
|
|
use BuilderOverride; |
11
|
|
|
use BuildsQueriesOverride; |
12
|
|
|
use ModelOverride; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Make the given, typically hidden, attributes visible. |
16
|
|
|
* |
17
|
|
|
* @param array|string $attributes |
18
|
|
|
* |
19
|
|
|
* @return $this |
20
|
|
|
*/ |
21
|
|
|
public abstract function makeVisible($attributes); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Set the array of model attributes. No checking is done. |
25
|
|
|
* |
26
|
|
|
* @param array $attributes |
27
|
|
|
* @param bool $sync |
28
|
|
|
* |
29
|
|
|
* @return $this |
30
|
|
|
*/ |
31
|
|
|
public abstract function setRawAttributes(array $attributes, $sync = false); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the hidden attributes for the model. |
35
|
|
|
* |
36
|
|
|
* @return array |
37
|
|
|
*/ |
38
|
|
|
abstract public function getHidden(); |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Decode the given JSON back into an array or object. |
42
|
|
|
* |
43
|
|
|
* @param string $value |
44
|
|
|
* @param bool $asObject |
45
|
|
|
* |
46
|
|
|
* @return mixed |
47
|
|
|
*/ |
48
|
|
|
abstract public function fromJson($value, $asObject = false); |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get the class name for polymorphic relations. |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
abstract public function getMorphClass(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Load all of the models from the json file in the "modelsFromJson" variable. |
59
|
|
|
* |
60
|
|
|
* @return Collection |
61
|
|
|
*/ |
62
|
22 |
|
protected function loadModelsFromJson() |
63
|
|
|
{ |
64
|
22 |
|
$modelsArray = $this->getRawArrayFromJson(); |
65
|
22 |
|
foreach ($modelsArray as $key => $modelArray) { |
66
|
21 |
|
$modelsArray[$key] = app($this->getMorphClass())->setRawAttributes($modelArray); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
22 |
|
return collect($modelsArray); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get an array containing all of the models from the json file. |
74
|
|
|
* |
75
|
|
|
* @return array |
76
|
|
|
*/ |
77
|
22 |
|
protected function getRawArrayFromJson() |
78
|
|
|
{ |
79
|
22 |
|
$modelsArray = []; |
80
|
22 |
|
if (file_exists($this->getJsonStoragePath())) { |
81
|
21 |
|
$modelsArray = $this->fromJson(File::get($this->getJsonStoragePath())); |
82
|
|
|
} |
83
|
|
|
|
84
|
22 |
|
return $modelsArray; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.