Skip to content
This repository was archived by the owner on Sep 26, 2019. It is now read-only.

Commit e7e6a21

Browse files
author
Felix Arntz
committed
Introduce Base_Admin_Page_Collection class.
1 parent 84b6d68 commit e7e6a21

1 file changed

Lines changed: 126 additions & 0 deletions

File tree

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
<?php
2+
/**
3+
* Base_Admin_Page_Collection class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page_Collection
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page_Collection;
10+
11+
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page;
12+
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page_Collection;
13+
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page_Factory;
14+
use Leaves_And_Love\OOP_Admin_Pages\Exception\Admin_Page_Not_Found_Exception;
15+
use Leaves_And_Love\OOP_Admin_Pages\Exception\Config_Invalid_Exception;
16+
use BrightNucleus\Config\ConfigInterface as Config;
17+
use BrightNucleus\Config\ConfigTrait;
18+
use BrightNucleus\Config\Config as BaseConfig;
19+
20+
/**
21+
* Class to manage admin page instances.
22+
*
23+
* @since 1.0.0
24+
*/
25+
class Base_Admin_Page_Collection implements Admin_Page_Collection {
26+
27+
use ConfigTrait;
28+
29+
const PAGES = 'pages';
30+
const FACTORY = 'factory';
31+
32+
/**
33+
* Admin pages in the collection.
34+
*
35+
* @since 1.0.0
36+
* @var array
37+
*/
38+
protected $pages = array();
39+
40+
/**
41+
* Factory to create admin pages for this collection.
42+
*
43+
* @since 1.0.0
44+
* @var Admin_Page_Factory
45+
*/
46+
protected $factory;
47+
48+
/**
49+
* Constructor.
50+
*
51+
* Sets the configuration and instantiates the admin page factory for the collection.
52+
*
53+
* @since 1.0.0
54+
*
55+
* @param Config $config Admin page collection configuration. Must contain the keys 'pages' (array of admin page
56+
* configurations) and 'factory' (admin page factory instance to use).
57+
*
58+
* @throws Config_Invalid_Exception Thrown when the configuration is invalid.
59+
*/
60+
public function __construct( Config $config ) {
61+
$this->processConfig( $config );
62+
63+
if ( ! $this->hasConfigKey( self::FACTORY ) ) {
64+
throw new Config_Invalid_Exception( sprintf( 'The required configuration key %s is missing.', self::FACTORY ) );
65+
}
66+
67+
$factory = $this->getConfigKey( self::FACTORY );
68+
69+
if ( ! is_a( $factory, Admin_Page_Factory::class ) ) {
70+
throw new Config_Invalid_Exception( sprintf( 'The class %1$s does not implement the interface %2$s.', $factory_class, Admin_Page_Factory::class ) );
71+
}
72+
73+
$this->factory = $factory;
74+
}
75+
76+
/**
77+
* Registers the admin pages in the collection within the environment.
78+
*
79+
* @since 1.0.0
80+
*/
81+
public function register() {
82+
if ( ! $this->hasConfigKey( self::PAGES ) ) {
83+
return;
84+
}
85+
86+
$pages = $this->getConfigKey( self::PAGES );
87+
88+
foreach ( $pages as $page_config ) {
89+
$page = $this->factory->create_page( new BaseConfig( $page_config ) );
90+
91+
$this->pages[ $page->get_slug() ] = $page;
92+
93+
$page->register();
94+
}
95+
}
96+
97+
/**
98+
* Checks whether an admin page is part of the collection.
99+
*
100+
* @since 1.0.0
101+
*
102+
* @param string $slug Identifier of the admin page to check for.
103+
* @return bool True if the admin page is part of the collection, false otherwise.
104+
*/
105+
public function has_page( string $slug ) : bool {
106+
return isset( $this->pages[ $slug ] );
107+
}
108+
109+
/**
110+
* Gets an admin page instance that is part of the collection.
111+
*
112+
* @since 1.0.0
113+
*
114+
* @param string $slug Identifier of the admin page to get.
115+
* @return Admin_Page Admin page instance.
116+
*
117+
* @throws Admin_Page_Not_Found_Exception Thrown when the admin page is not found.
118+
*/
119+
public function get_page( string $slug ) : Admin_Page {
120+
if ( ! $this->has_page( $slug ) ) {
121+
throw new Admin_Page_Not_Found_Exception( sprintf( 'No admin page with the identifier %s exists in the collection.', $slug ) );
122+
}
123+
124+
return $this->pages[ $slug ];
125+
}
126+
}

0 commit comments

Comments
 (0)