Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
Pages
0.00% covered (danger)
0.00%
0 / 63
0.00% covered (danger)
0.00%
0 / 15
1260
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 validate_parameters
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 set_properties
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 add_page
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 add_pages
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
 remove_page_by_slug
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
12
 get_pages
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 get_front_page
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 get_page_by_slug
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 get_page_content_by_slug
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 is_empty
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 count
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 has_page
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 to_array
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 to_associative_array
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace NewfoldLabs\WP\Module\Onboarding\Types;
4
5/**
6 * Pages data type class.
7 *
8 * Represents a collection of Page objects.
9 *
10 * @package NewfoldLabs\WP\Module\Onboarding\Types
11 */
12class Pages {
13
14    /**
15     * Array of Page objects.
16     *
17     * @var array
18     */
19    private $pages;
20
21    /**
22     * Pages constructor.
23     *
24     * @param array $pages Array of Page objects (optional).
25     * @throws \InvalidArgumentException When parameters are invalid.
26     */
27    public function __construct( array $pages = array() ) {
28        $this->validate_parameters( $pages );
29        $this->set_properties( $pages );
30    }
31
32    /**
33     * Validate constructor parameters.
34     *
35     * @param array $pages Array of Page objects.
36     * @throws \InvalidArgumentException When parameters are invalid or duplicate slugs exist.
37     */
38    private function validate_parameters( array $pages ): void {
39        // Validate that all pages items are Page objects and check for duplicate slugs.
40        $slugs = array();
41        foreach ( $pages as $index => $page ) {
42            if ( ! $page instanceof Page ) {
43                throw new \InvalidArgumentException( "Pages item at index {$index} must be a Page object" );
44            }
45            
46            $slug = $page->get_slug();
47            if ( in_array( $slug, $slugs, true ) ) {
48                throw new \InvalidArgumentException( "Duplicate slug '{$slug}' found at index {$index}" );
49            }
50            
51            $slugs[] = $slug;
52        }
53    }
54
55    /**
56     * Set properties from parameters.
57     *
58     * @param array $pages Array of Page objects.
59     */
60    private function set_properties( array $pages ): void {
61        $this->pages = $pages;
62    }
63
64    /**
65     * Add a Page object to the collection.
66     *
67     * @param Page $page Page object to add.
68     * @return Pages
69     * @throws \InvalidArgumentException When a page with the same slug already exists.
70     */
71    public function add_page( Page $page ): Pages {
72        // Check if a page with the same slug already exists.
73        if ( $this->has_page( $page->get_slug() ) ) {
74            throw new \InvalidArgumentException( "A page with slug '{$page->get_slug()}' already exists" );
75        }
76        
77        $this->pages[] = $page;
78        return $this;
79    }
80
81    /**
82     * Add multiple Page objects to the collection.
83     *
84     * @param array $pages Array of Page objects.
85     * @return Pages
86     * @throws \InvalidArgumentException When parameters are invalid or duplicate slugs exist.
87     */
88    public function add_pages( array $pages ): Pages {
89        // First, validate all pages are Page objects and check for duplicates.
90        $slugs_to_add = array();
91        foreach ( $pages as $page ) {
92            if ( ! $page instanceof Page ) {
93                throw new \InvalidArgumentException( 'All items must be Page objects' );
94            }
95            $slug = $page->get_slug();
96            
97            // Check for duplicates within the new pages array.
98            if ( in_array( $slug, $slugs_to_add, true ) ) {
99                throw new \InvalidArgumentException( "Duplicate slug '{$slug}' found in the pages array" );
100            }
101            
102            // Check for duplicates with existing pages.
103            if ( $this->has_page( $slug ) ) {
104                throw new \InvalidArgumentException( "A page with slug '{$slug}' already exists" );
105            }
106            
107            $slugs_to_add[] = $slug;
108        }
109        
110        // If all validations pass, add all pages.
111        foreach ( $pages as $page ) {
112            $this->pages[] = $page;
113        }
114        
115        return $this;
116    }
117
118    /**
119     * Remove a Page object from the collection by slug.
120     *
121     * @param string $page_slug The page slug to remove.
122     * @return Pages
123     */
124    public function remove_page_by_slug( string $page_slug ): Pages {
125        foreach ( $this->pages as $index => $page ) {
126            if ( $page->get_slug() === $page_slug ) {
127                unset( $this->pages[ $index ] );
128                break;
129            }
130        }
131        // Re-index array after removal.
132        $this->pages = array_values( $this->pages );
133        return $this;
134    }
135
136    /**
137     * Get all pages.
138     *
139     * @return array
140     */
141    public function get_pages(): array {
142        return $this->pages;
143    }
144
145    /**
146     * Get the front page.
147     *
148     * @return Page
149     */
150    public function get_front_page() {
151        $front_page = null;
152        // Check if there is a page with is_front_page set to true.
153        foreach ( $this->pages as $page ) {
154            if ( $page->is_front_page() ) {
155                $front_page = $page;
156                break;
157            }
158        }
159
160        // If no front page is found, check if there is a page with slug 'home'.
161        if ( ! $front_page ) {
162            $front_page = $this->get_page_by_slug( 'home' );
163            if ( ! $front_page ) {
164                throw new \RuntimeException( 'Front page not found in pages collection' );
165            }
166        }
167
168        return $front_page;
169    }
170
171    /**
172     * Get page by slug.
173     *
174     * @param string $page_slug The page slug to find.
175     * @return Page|null
176     */
177    public function get_page_by_slug( string $page_slug ) {
178        foreach ( $this->pages as $page ) {
179            if ( $page->get_slug() === $page_slug ) {
180                return $page;
181            }
182        }
183        return null;
184    }
185
186    /**
187     * Get page content by slug.
188     *
189     * @param string $page_slug The page slug to find.
190     * @return string|null
191     */
192    public function get_page_content_by_slug( string $page_slug ) {
193        $page = $this->get_page_by_slug( $page_slug );
194        return $page ? $page->get_content() : null;
195    }
196
197    /**
198     * Check if collection is empty.
199     *
200     * @return bool
201     */
202    public function is_empty(): bool {
203        return empty( $this->pages );
204    }
205
206    /**
207     * Get the number of pages in the collection.
208     *
209     * @return int
210     */
211    public function count(): int {
212        return count( $this->pages );
213    }
214
215    /**
216     * Check if a page exists by slug.
217     *
218     * @param string $page_slug The page slug to check.
219     * @return bool
220     */
221    public function has_page( string $page_slug ): bool {
222        return $this->get_page_by_slug( $page_slug ) !== null;
223    }
224
225    /**
226     * Convert to array.
227     *
228     * @return array
229     */
230    public function to_array(): array {
231        $pages_array = array();
232        foreach ( $this->pages as $page ) {
233            $pages_array[] = $page->to_array();
234        }
235
236        return $pages_array;
237    }
238
239    /**
240     * Convert to associative array with slug as key.
241     *
242     * @return array
243     */
244    public function to_associative_array(): array {
245        $pages_array = array();
246        foreach ( $this->pages as $page ) {
247            $pages_array[ $page->get_slug() ] = $page->get_content();
248        }
249
250        return $pages_array;
251    }
252}