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