1+ <?php
2+
3+ namespace NewfoldLabs \WP \Module \Onboarding \Types ;
4+
5+ /**
6+ * ColorPalette data type class.
7+ *
8+ * Represents a collection of Color objects with a unique slug identifier.
9+ *
10+ * @package NewfoldLabs\WP\Module\Onboarding\Types
11+ *
12+ * @example
13+ * slug: palette_1
14+ * palette: [
15+ * { name: "Accent 1", slug: "accent_1", color: "#F27121" },
16+ * { name: "Accent 2", slug: "accent_2", color: "#8A4D76" }
17+ * ]
18+ */
19+ class ColorPalette {
20+
21+ /**
22+ * Color palette slug.
23+ *
24+ * @var string
25+ */
26+ private $ slug ;
27+
28+ /**
29+ * Array of Color objects.
30+ *
31+ * @var array
32+ */
33+ private $ palette ;
34+
35+ /**
36+ * ColorPalette constructor.
37+ *
38+ * @param string $slug Color palette slug.
39+ * @param array $palette Array of Color objects (optional).
40+ * @throws \InvalidArgumentException When parameters are invalid.
41+ */
42+ public function __construct ( string $ slug , array $ palette = array () ) {
43+ $ this ->validate_parameters ( $ slug , $ palette );
44+ $ this ->set_properties ( $ slug , $ palette );
45+ }
46+
47+ /**
48+ * Validate constructor parameters.
49+ *
50+ * @param string $slug Color palette slug.
51+ * @param array $palette Array of Color objects.
52+ * @throws \InvalidArgumentException When parameters are invalid.
53+ */
54+ private function validate_parameters ( string $ slug , array $ palette ): void {
55+ if ( empty ( trim ( $ slug ) ) ) {
56+ throw new \InvalidArgumentException ( 'Slug cannot be empty ' );
57+ }
58+
59+ // Validate that all palette items are Color objects.
60+ foreach ( $ palette as $ index => $ color ) {
61+ if ( ! $ color instanceof Color ) {
62+ throw new \InvalidArgumentException ( "Palette item at index {$ index } must be a Color object " );
63+ }
64+ }
65+ }
66+
67+ /**
68+ * Set properties from parameters.
69+ *
70+ * @param string $slug Color palette slug.
71+ * @param array $palette Array of Color objects.
72+ */
73+ private function set_properties ( string $ slug , array $ palette ): void {
74+ $ this ->slug = trim ( $ slug );
75+ $ this ->palette = $ palette ;
76+ }
77+
78+ /**
79+ * Add a Color object to the palette.
80+ *
81+ * @param Color $color Color object to add.
82+ * @return ColorPalette
83+ */
84+ public function add_color ( Color $ color ): ColorPalette {
85+ $ this ->palette [] = $ color ;
86+ return $ this ;
87+ }
88+
89+ /**
90+ * Add multiple Color objects to the palette.
91+ *
92+ * @param array $colors Array of Color objects.
93+ * @return ColorPalette
94+ */
95+ public function add_colors ( array $ colors ): ColorPalette {
96+ foreach ( $ colors as $ color ) {
97+ if ( ! $ color instanceof Color ) {
98+ throw new \InvalidArgumentException ( 'All items must be Color objects ' );
99+ }
100+ $ this ->palette [] = $ color ;
101+ }
102+ return $ this ;
103+ }
104+
105+ /**
106+ * Remove a Color object from the palette by slug.
107+ *
108+ * @param string $color_slug The color slug to remove.
109+ * @return ColorPalette
110+ */
111+ public function remove_color_by_slug ( string $ color_slug ): ColorPalette {
112+ foreach ( $ this ->palette as $ index => $ color ) {
113+ if ( $ color ->get_slug () === $ color_slug ) {
114+ unset( $ this ->palette [ $ index ] );
115+ break ;
116+ }
117+ }
118+ // Re-index array after removal.
119+ $ this ->palette = array_values ( $ this ->palette );
120+ return $ this ;
121+ }
122+
123+ /**
124+ * Get slug.
125+ *
126+ * @return string
127+ */
128+ public function get_slug (): string {
129+ return $ this ->slug ;
130+ }
131+
132+ /**
133+ * Get palette.
134+ *
135+ * @return array
136+ */
137+ public function get_palette (): array {
138+ return $ this ->palette ;
139+ }
140+
141+ /**
142+ * Get color by slug.
143+ *
144+ * @param string $color_slug The color slug to find.
145+ * @return Color|null
146+ */
147+ public function get_color_by_slug ( string $ color_slug ) {
148+ foreach ( $ this ->palette as $ color ) {
149+ if ( $ color ->get_slug () === $ color_slug ) {
150+ return $ color ;
151+ }
152+ }
153+ return null ;
154+ }
155+
156+ /**
157+ * Check if palette is empty.
158+ *
159+ * @return bool
160+ */
161+ public function is_empty (): bool {
162+ return empty ( $ this ->palette );
163+ }
164+
165+ /**
166+ * Get the number of colors in the palette.
167+ *
168+ * @return int
169+ */
170+ public function count (): int {
171+ return count ( $ this ->palette );
172+ }
173+
174+ /**
175+ * Convert to array.
176+ *
177+ * @return array
178+ */
179+ public function to_array (): array {
180+ $ palette_array = array ();
181+ foreach ( $ this ->palette as $ color ) {
182+ $ palette_array [] = $ color ->to_array ();
183+ }
184+
185+ return array (
186+ 'slug ' => $ this ->slug ,
187+ 'palette ' => $ palette_array ,
188+ );
189+ }
190+
191+ /**
192+ * Create from array (static factory method).
193+ *
194+ * @param array $data ColorPalette data array.
195+ * @return ColorPalette
196+ */
197+ public static function from_array ( array $ data ): ColorPalette {
198+ if ( ! isset ( $ data ['slug ' ] ) ) {
199+ throw new \InvalidArgumentException ( 'Array must contain slug key ' );
200+ }
201+
202+ $ palette = new self ( $ data ['slug ' ] );
203+
204+ if ( isset ( $ data ['palette ' ] ) && is_array ( $ data ['palette ' ] ) ) {
205+ foreach ( $ data ['palette ' ] as $ color_data ) {
206+ $ palette ->add_color ( Color::from_array ( $ color_data ) );
207+ }
208+ }
209+
210+ return $ palette ;
211+ }
212+ }
0 commit comments