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

Commit f6ef1da

Browse files
author
Felix Arntz
committed
Introduce Abstract_Admin_Page class.
1 parent 8f4f979 commit f6ef1da

File tree

3 files changed

+138
-1
lines changed

3 files changed

+138
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
}
2828
},
2929
"require": {
30-
"php": ">=7.0"
30+
"php": ">=7.0",
31+
"brightnucleus/config": "~0.4"
3132
},
3233
"require-dev": {
3334
"codeclimate/php-test-reporter": "dev-master",
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* Abstract_Admin_Page class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Admin_Page
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Admin_Page;
10+
11+
use Leaves_And_Love\OOP_Admin_Pages\Admin_Page;
12+
use Leaves_And_Love\OOP_Admin_Pages\Exception\Config_Invalid_Exception;
13+
use BrightNucleus\Config\ConfigInterface as Config;
14+
use BrightNucleus\Config\ConfigTrait;
15+
16+
/**
17+
* Abstract class representing an admin page.
18+
*
19+
* @since 1.0.0
20+
*/
21+
abstract class Abstract_Admin_Page implements Admin_Page {
22+
23+
use ConfigTrait;
24+
25+
const SLUG = 'slug';
26+
const TITLE = 'title';
27+
const RENDER_CALLBACK = 'render_callback';
28+
const INITIALIZE_CALLBACK = 'initialize_callback';
29+
30+
/**
31+
* Constructor.
32+
*
33+
* Sets the configuration.
34+
*
35+
* @since 1.0.0
36+
*
37+
* @param Config $config Admin page configuration. Must contain keys 'slug' (admin page identifier), 'title'
38+
* and 'render_callback' (callable that receives the configuration as sole parameter).
39+
* May also contain an 'initialize_callback' key.
40+
*
41+
* @throws Config_Invalid_Exception Thrown when the configuration is invalid.
42+
*/
43+
public function __construct( Config $config ) {
44+
$this->processConfig( $config );
45+
46+
$required_keys = $this->get_required_config_keys();
47+
48+
foreach ( $required_keys as $required_key ) {
49+
if ( ! $this->hasConfigKey( $required_key ) ) {
50+
throw new Config_Invalid_Exception( sprintf( 'The required configuration key %s is missing.', $required_key ) );
51+
}
52+
53+
$value = $this->getConfigKey( $required_key );
54+
if ( empty( $value ) ) {
55+
throw new Config_Invalid_Exception( sprintf( 'The required configuration key %s is empty.', $required_key ) );
56+
}
57+
}
58+
}
59+
60+
/**
61+
* Gets the slug of the admin page.
62+
*
63+
* @since 1.0.0
64+
*
65+
* @return string Admin page slug.
66+
*/
67+
public function get_slug() : string {
68+
return $this->getConfigKey( self::SLUG );
69+
}
70+
71+
/**
72+
* Gets the title of the admin page.
73+
*
74+
* @since 1.0.0
75+
*
76+
* @return string Admin page title.
77+
*/
78+
public function get_title() : string {
79+
return $this->getConfigKey( self::TITLE );
80+
}
81+
82+
/**
83+
* Initializes the admin page on pageload.
84+
*
85+
* This method must be called before any output is printed.
86+
*
87+
* @since 1.0.0
88+
*/
89+
public function initialize() {
90+
if ( ! $this->hasConfigKey( self::INITIALIZE_CALLBACK ) ) {
91+
return;
92+
}
93+
94+
call_user_func( $this->getConfigKey( self::INITIALIZE_CALLBACK ), $this->config );
95+
}
96+
97+
/**
98+
* Renders the admin page content.
99+
*
100+
* @since 1.0.0
101+
*/
102+
public function render() {
103+
call_user_func( $this->getConfigKey( self::RENDER_CALLBACK ), $this->config );
104+
}
105+
106+
/**
107+
* Gets the configuration keys that are required for an admin page.
108+
*
109+
* @since 1.0.0
110+
*
111+
* @return array Array of configuration keys.
112+
*/
113+
protected function get_required_config_keys() {
114+
return array( self::SLUG, self::TITLE, self::RENDER_CALLBACK );
115+
}
116+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
/**
3+
* Config_Invalid_Exception class
4+
*
5+
* @package Leaves_And_Love\OOP_Admin_Pages\Exception
6+
* @since 1.0.0
7+
*/
8+
9+
namespace Leaves_And_Love\OOP_Admin_Pages\Exception;
10+
11+
use Exception;
12+
13+
/**
14+
* Exception thrown when a configuration is invalid.
15+
*
16+
* @since 1.0.0
17+
*/
18+
class Config_Invalid_Exception extends Exception {
19+
20+
}

0 commit comments

Comments
 (0)