Skip to content

Commit eb7a24a

Browse files
committed
Types
1 parent 40daef5 commit eb7a24a

File tree

6 files changed

+1053
-0
lines changed

6 files changed

+1053
-0
lines changed

includes/Types/Color.php

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
<?php
2+
3+
namespace NewfoldLabs\WP\Module\Onboarding\Types;
4+
5+
/**
6+
* Color data type class.
7+
*
8+
* Represents a WordPress compatible color with name, slug, and hex color value.
9+
*
10+
* @package NewfoldLabs\WP\Module\Onboarding\Types
11+
*
12+
* @example
13+
* name: Accent 1
14+
* slug: accent_1
15+
* color: #F27121
16+
*/
17+
class Color {
18+
19+
/**
20+
* Color name.
21+
*
22+
* @var string
23+
*/
24+
private $name;
25+
26+
/**
27+
* Color slug.
28+
*
29+
* @var string
30+
*/
31+
private $slug;
32+
33+
/**
34+
* Color hex value.
35+
*
36+
* @var string
37+
*/
38+
private $color;
39+
40+
/**
41+
* Color constructor.
42+
*
43+
* @param string $name Color name - e.g. "Accent 1".
44+
* @param string $slug Color slug - e.g. "accent_1".
45+
* @param string $color Color hex value - e.g. "#F27121".
46+
* @throws \InvalidArgumentException When parameters are invalid.
47+
*/
48+
public function __construct( string $name, string $slug, string $color ) {
49+
$this->validate_parameters( $name, $slug, $color );
50+
$this->set_properties( $name, $slug, $color );
51+
}
52+
53+
/**
54+
* Validate constructor parameters.
55+
*
56+
* @param string $name Color name.
57+
* @param string $slug Color slug.
58+
* @param string $color Color hex value.
59+
* @throws \InvalidArgumentException When parameters are invalid.
60+
*/
61+
private function validate_parameters( string $name, string $slug, string $color ): void {
62+
if ( empty( trim( $name ) ) ) {
63+
throw new \InvalidArgumentException( 'Name cannot be empty' );
64+
}
65+
66+
if ( empty( trim( $slug ) ) ) {
67+
throw new \InvalidArgumentException( 'Slug cannot be empty' );
68+
}
69+
70+
if ( empty( trim( $color ) ) ) {
71+
throw new \InvalidArgumentException( 'Color cannot be empty' );
72+
}
73+
}
74+
75+
/**
76+
* Set properties from parameters.
77+
*
78+
* @param string $name Color name.
79+
* @param string $slug Color slug.
80+
* @param string $color Color hex value.
81+
*/
82+
private function set_properties( string $name, string $slug, string $color ): void {
83+
$this->name = trim( $name );
84+
$this->slug = trim( $slug );
85+
$this->color = trim( $color );
86+
}
87+
88+
/**
89+
* Get name.
90+
*
91+
* @return string
92+
*/
93+
public function get_name(): string {
94+
return $this->name;
95+
}
96+
97+
/**
98+
* Get slug.
99+
*
100+
* @return string
101+
*/
102+
public function get_slug(): string {
103+
return $this->slug;
104+
}
105+
106+
/**
107+
* Get color.
108+
*
109+
* @return string
110+
*/
111+
public function get_color(): string {
112+
return $this->color;
113+
}
114+
115+
/**
116+
* Convert to array.
117+
*
118+
* @return array
119+
*/
120+
public function to_array(): array {
121+
return array(
122+
'name' => $this->name,
123+
'slug' => $this->slug,
124+
'color' => $this->color,
125+
);
126+
}
127+
128+
/**
129+
* Create from array (static factory method).
130+
*
131+
* @param array $data Color data array.
132+
* @return Color
133+
*/
134+
public static function from_array( array $data ): Color {
135+
if ( ! isset( $data['name'] ) || ! isset( $data['slug'] ) || ! isset( $data['color'] ) ) {
136+
throw new \InvalidArgumentException( 'Array must contain name, slug, and color keys' );
137+
}
138+
139+
return new self( $data['name'], $data['slug'], $data['color'] );
140+
}
141+
}

includes/Types/ColorPalette.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
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

Comments
 (0)