Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 25 |
|
0.00% |
0 / 9 |
CRAP | |
0.00% |
0 / 1 |
Page | |
0.00% |
0 / 25 |
|
0.00% |
0 / 9 |
240 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
validate_parameters | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
set_properties | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
get_title | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_slug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_content | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
is_front_page | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
to_array | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
from_array | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | |
3 | namespace NewfoldLabs\WP\Module\Onboarding\Types; |
4 | |
5 | /** |
6 | * Page data type class. |
7 | * |
8 | * Represents a single page with title, slug, and content. |
9 | * |
10 | * @package NewfoldLabs\WP\Module\Onboarding\Types |
11 | */ |
12 | class Page { |
13 | |
14 | /** |
15 | * Page title. |
16 | * |
17 | * @var string |
18 | */ |
19 | private $title; |
20 | |
21 | /** |
22 | * Page slug. |
23 | * |
24 | * @var string |
25 | */ |
26 | private $slug; |
27 | |
28 | /** |
29 | * Page content. |
30 | * |
31 | * @var string |
32 | */ |
33 | private $content; |
34 | |
35 | /** |
36 | * Whether the page is the front page. |
37 | * |
38 | * @var bool |
39 | */ |
40 | private $is_front_page; |
41 | |
42 | /** |
43 | * Page constructor. |
44 | * |
45 | * @param string $title Page title. |
46 | * @param string $slug Page slug. |
47 | * @param string $content Page content. |
48 | * @param bool $is_front_page Whether the page is the front page. |
49 | * @throws \InvalidArgumentException When parameters are invalid. |
50 | */ |
51 | public function __construct( string $title, string $slug, string $content, bool $is_front_page = false ) { |
52 | $this->validate_parameters( $title, $slug, $content ); |
53 | $this->set_properties( $title, $slug, $content, $is_front_page ); |
54 | } |
55 | |
56 | /** |
57 | * Validate constructor parameters. |
58 | * |
59 | * @param string $title Page title. |
60 | * @param string $slug Page slug. |
61 | * @param string $content Page content. |
62 | * @throws \InvalidArgumentException When parameters are invalid. |
63 | */ |
64 | private function validate_parameters( string $title, string $slug, string $content ): void { |
65 | if ( empty( trim( $title ) ) ) { |
66 | throw new \InvalidArgumentException( 'Title cannot be empty' ); |
67 | } |
68 | |
69 | if ( empty( trim( $slug ) ) ) { |
70 | throw new \InvalidArgumentException( 'Slug cannot be empty' ); |
71 | } |
72 | |
73 | if ( empty( trim( $content ) ) ) { |
74 | throw new \InvalidArgumentException( 'Content cannot be empty' ); |
75 | } |
76 | } |
77 | |
78 | /** |
79 | * Set properties from parameters. |
80 | * |
81 | * @param string $title Page title. |
82 | * @param string $slug Page slug. |
83 | * @param string $content Page content. |
84 | * @param bool $is_front_page Whether the page is the front page. |
85 | */ |
86 | private function set_properties( string $title, string $slug, string $content, bool $is_front_page ): void { |
87 | $this->title = trim( $title ); |
88 | $this->slug = trim( $slug ); |
89 | $this->content = trim( $content ); |
90 | $this->is_front_page = $is_front_page; |
91 | } |
92 | |
93 | /** |
94 | * Get title. |
95 | * |
96 | * @return string |
97 | */ |
98 | public function get_title(): string { |
99 | return $this->title; |
100 | } |
101 | |
102 | /** |
103 | * Get slug. |
104 | * |
105 | * @return string |
106 | */ |
107 | public function get_slug(): string { |
108 | return $this->slug; |
109 | } |
110 | |
111 | /** |
112 | * Get content. |
113 | * |
114 | * @return string |
115 | */ |
116 | public function get_content(): string { |
117 | return $this->content; |
118 | } |
119 | |
120 | /** |
121 | * Get whether the page is the front page. |
122 | * |
123 | * @return bool |
124 | */ |
125 | public function is_front_page(): bool { |
126 | return $this->is_front_page; |
127 | } |
128 | |
129 | /** |
130 | * Convert to array. |
131 | * |
132 | * @return array |
133 | */ |
134 | public function to_array(): array { |
135 | return array( |
136 | 'title' => $this->title, |
137 | 'slug' => $this->slug, |
138 | 'content' => $this->content, |
139 | 'is_front_page' => $this->is_front_page, |
140 | ); |
141 | } |
142 | |
143 | /** |
144 | * Create from array (static factory method). |
145 | * |
146 | * @param array $data Page data array. |
147 | * @return Page |
148 | */ |
149 | public static function from_array( array $data ): Page { |
150 | if ( ! isset( $data['title'] ) || ! isset( $data['slug'] ) || ! isset( $data['content'] ) ) { |
151 | throw new \InvalidArgumentException( 'Array must contain title, slug, and content keys' ); |
152 | } |
153 | |
154 | return new self( $data['title'], $data['slug'], $data['content'], $data['is_front_page'] ?? false ); |
155 | } |
156 | } |