1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SixtyNine\Cloud\Builder; |
4
|
|
|
|
5
|
|
|
use SixtyNine\Cloud\Model\Palette; |
6
|
|
|
use Symfony\Component\Yaml\Yaml; |
7
|
|
|
|
8
|
|
|
class PalettesBuilder |
9
|
|
|
{ |
10
|
|
|
protected $palettes = array(); |
11
|
|
|
|
12
|
3 |
|
protected function __construct() |
13
|
|
|
{ |
14
|
3 |
|
} |
15
|
|
|
|
16
|
3 |
|
public static function create() |
17
|
|
|
{ |
18
|
3 |
|
return new self(); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $name |
23
|
|
|
* @param array $colors |
24
|
|
|
* @return PalettesBuilder |
25
|
|
|
*/ |
26
|
|
|
public function addPalette($name, array $colors) |
27
|
|
|
{ |
28
|
|
|
$palette = new Palette(); |
29
|
|
|
$palette->setName($name)->setColors($colors); |
|
|
|
|
30
|
|
|
$this->palettes[$name] = $palette; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $file |
37
|
|
|
* @return PalettesBuilder |
38
|
|
|
* @throws \InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
public function importPalettes($file) |
41
|
|
|
{ |
42
|
|
|
if (!file_exists($file)) { |
43
|
|
|
throw new \InvalidArgumentException('File not found: ' . $file); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$yml = Yaml::parse(file_get_contents($file)); |
47
|
|
|
|
48
|
|
|
if (!array_key_exists('palettes', $yml)) { |
49
|
|
|
throw new \InvalidArgumentException('Invalid palettes YAML'); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
foreach ($yml['palettes'] as $name => $colors) { |
53
|
|
|
$this->addPalette($name, $colors); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $name |
61
|
|
|
* @return Palette |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
|
|
public function getNamedPalette($name) |
65
|
|
|
{ |
66
|
|
|
if (!array_key_exists($name, $this->palettes)) { |
67
|
|
|
throw new \InvalidArgumentException('Palette not found: ' . $name); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $this->palettes[$name]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param int $count |
75
|
|
|
* @return Palette |
76
|
|
|
*/ |
77
|
3 |
|
public function getRandomPalette($count = 5) |
78
|
|
|
{ |
79
|
3 |
|
$colors = array(); |
80
|
3 |
|
for ($i = 0; $i < $count; $i++) { |
81
|
3 |
|
$part1 = dechex(rand(0, 255)); |
82
|
3 |
|
$part2 = dechex(rand(0, 255)); |
83
|
3 |
|
$part3 = dechex(rand(0, 255)); |
84
|
|
|
$color = '#' |
85
|
3 |
|
. (strlen($part1) >= 2 ? $part1 : '0' . $part1) |
86
|
3 |
|
. (strlen($part2) >= 2 ? $part2 : '0' . $part2) |
87
|
3 |
|
. (strlen($part3) >= 2 ? $part3 : '0' . $part3) |
88
|
|
|
; |
89
|
3 |
|
$colors[] = $color; |
90
|
|
|
} |
91
|
3 |
|
$palette = new Palette(); |
92
|
3 |
|
$palette->setName('random')->setColors($colors); |
|
|
|
|
93
|
3 |
|
return $palette; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $colors |
98
|
|
|
*/ |
99
|
|
|
public function getPalette(array $colors) |
100
|
|
|
{ |
101
|
|
|
$palette = new Palette(); |
102
|
|
|
$palette->setName('palette')->setColors($colors); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return array |
107
|
|
|
*/ |
108
|
|
|
public function getPalettes() |
109
|
|
|
{ |
110
|
|
|
return $this->palettes; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: