Completed
Push — develop ( 483098...51eed3 )
by Paul
02:15
created

SiteMeta::__call()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
1
<?php
2
3
namespace GeminiLabs\Castor\Helpers;
4
5
/**
6
 * SiteMeta::all();
7
 * SiteMeta::group();
8
 * SiteMeta::group('option','fallback');
9
 * SiteMeta::get('group');
10
 * SiteMeta::get('group','option','fallback');
11
 *
12
 * @property object all
13
 */
14
class SiteMeta
15
{
16
	protected $options;
17
18
	public function __construct()
19
	{
20
		$this->options = get_option( apply_filters( 'pollux/settings/id', 'pollux_settings' ), [] );
21
	}
22
23
	/**
24
	 * @param string $group
25
	 * @return object|array|null
26
	 */
27
	public function __call( $group, $args )
28
	{
29
		$args = array_pad( $args, 2, null );
30
		$group = $this->$group;
31
		if( is_object( $group )) {
32
			return $group;
33
		}
34
		return $this->get( $group, $args[0], $args[1] );
35
	}
36
37
	/**
38
	 * @param string $group
39
	 * @return object|array|null
40
	 */
41
	public function __get( $group )
42
	{
43
		if( $group == 'all' ) {
44
			return (object) $this->options;
45
		}
46
		if( empty( $group )) {
47
			$group = $this->getDefaultGroup();
48
		}
49
		return isset( $this->options[$group] )
50
			? $this->options[$group]
51
			: null;
52
	}
53
54
	/**
55
	 * @param string $group
56
	 * @param string|null $key
57
	 * @param mixed $fallback
58
	 * @return mixed
59
	 */
60
	public function get( $group = '', $key = '', $fallback = null )
61
	{
62
		if( func_num_args() < 1 ) {
63
			return $this->all;
64
		}
65
		if( is_string( $group )) {
66
			$group = $this->$group;
67
		}
68
		if( !is_array( $group )) {
69
			return $fallback;
70
		}
71
		if( is_null( $key )) {
72
			return $group;
73
		}
74
		return $this->getValue( $group, $key, $fallback );
75
	}
76
77
	/**
78
	 * @return string
79
	 */
80
	protected function getDefaultGroup()
81
	{
82
		return '';
83
	}
84
85
	/**
86
	 * @param string $key
87
	 * @param mixed $fallback
88
	 * @return mixed
89
	 */
90
	protected function getValue( array $group, $key, $fallback )
91
	{
92
		if( !array_key_exists( $key, $group )) {
93
			return $fallback;
94
		}
95
		return empty( $group[$key] ) && !is_null( $fallback )
96
			? $fallback
97
			: $group[$key];
98
	}
99
}
100