1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Models\MediaProperties; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Collections\Collection; |
6
|
|
|
use Nip\Records\AbstractModels\Record; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Class MediaProperty |
10
|
|
|
* @package ByTIC\MediaLibrary\Models\MediaRecords |
11
|
|
|
* |
12
|
|
|
* @property string $model |
13
|
|
|
* @property int $model_id |
14
|
|
|
* @property string $collection_name |
15
|
|
|
* @property string $custom_properties |
16
|
|
|
*/ |
17
|
|
|
class MediaProperty extends Record |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var null|array |
21
|
|
|
*/ |
22
|
|
|
protected $customPropertiesArray = null; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param Record $model |
26
|
|
|
*/ |
27
|
|
|
public function populateFromModel(Record $model) |
28
|
|
|
{ |
29
|
|
|
$this->model = $model->getManager()->getMorphName(); |
|
|
|
|
30
|
|
|
$this->model_id = $model->getPrimaryKey(); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param Collection|string $collection |
35
|
|
|
*/ |
36
|
|
|
public function populateFromCollection($collection) |
37
|
|
|
{ |
38
|
|
|
$name = is_object($collection) ? $collection->getName() : $collection; |
39
|
|
|
$this->collection_name = $name; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
*/ |
45
|
|
|
public function getCustomProperties() |
46
|
|
|
{ |
47
|
|
|
$this->checkCustomProperties(); |
48
|
|
|
return $this->customPropertiesArray; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
protected function checkCustomProperties() |
52
|
|
|
{ |
53
|
|
|
if ($this->customPropertiesArray == null) { |
54
|
|
|
$this->initCustomProperties(); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
|
59
|
|
|
public function initCustomProperties() |
60
|
|
|
{ |
61
|
|
|
$properties = json_decode($this->custom_properties, true); |
62
|
|
|
$properties = (is_array($properties)) ? $properties : []; |
63
|
|
|
$this->customPropertiesArray = $properties; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param $name |
68
|
|
|
* @param null $default |
|
|
|
|
69
|
|
|
* @return mixed |
70
|
|
|
*/ |
71
|
|
|
public function getCustomProperty($name, $default = null) |
72
|
|
|
{ |
73
|
|
|
$this->checkCustomProperties(); |
74
|
|
|
return isset($this->customPropertiesArray[$name]) ? $this->customPropertiesArray[$name] : $default; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param $name |
79
|
|
|
* @param $value |
80
|
|
|
*/ |
81
|
|
|
public function setCustomPropery($name, $value) |
82
|
|
|
{ |
83
|
|
|
$this->checkCustomProperties(); |
84
|
|
|
$this->customPropertiesArray[$name] = $value; |
85
|
|
|
$this->setCustomPropertiesAttribute($this->getCustomProperties()); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param bool $value |
90
|
|
|
*/ |
91
|
|
|
public function saveDbLoaded($value) |
92
|
|
|
{ |
93
|
|
|
$this->dbLoaded($value); |
94
|
|
|
$this->save(); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param bool|null $value |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function dbLoaded($value = null) |
102
|
|
|
{ |
103
|
|
|
if ($value == null) { |
104
|
|
|
return $this->getCustomProperty('dbLoaded', false); |
105
|
|
|
} |
106
|
|
|
$value = ($value == true); |
|
|
|
|
107
|
|
|
$this->setCustomPropery('dbLoaded', $value); |
108
|
|
|
return $value; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param $value |
113
|
|
|
*/ |
114
|
|
|
public function setCustomPropertiesAttribute($value) |
115
|
|
|
{ |
116
|
|
|
if (is_string($value)) { |
117
|
|
|
$this->customPropertiesArray = null; |
118
|
|
|
} else { |
119
|
|
|
$this->customPropertiesArray = $value; |
120
|
|
|
$value = json_encode($value); |
121
|
|
|
} |
122
|
|
|
$this->setDataValue('custom_properties', $value); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..