-
Notifications
You must be signed in to change notification settings - Fork 23
/
ColourSwatches.php
62 lines (51 loc) · 1.39 KB
/
ColourSwatches.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace percipioglobal\colourswatches\models;
use craft\base\Model;
class ColourSwatches extends Model
{
/**
* @var string
*/
public $label = '';
public $color = '';
public function __construct($value)
{
if($this->validateJson($value)){
// typecast our object to an array
$colorData = (array)json_decode($value);
$value = null;
$color = array_filter($colorData);
// if our array has usable data
if (!empty($colorData['label']))
{
$this->label = $colorData['label'];
$this->color = $colorData['color'];
}
// else ensure we return a null value (only really needed for old data stores)
else {
$value = null;
}
// else ensure we return a null value
} else {
$value = null;
}
}
// making sure we have json data, returns boolean(true) if this is the case
public function validateJson($value)
{
$json = json_decode($value);
return $json && $value != $json;
}
public function __toString()
{
return $this->label;
}
public function colors()
{
return $this->color;
}
public function labels()
{
return $this->label;
}
}