Stencil_Subclass_Factory::create_null()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6667
cc 3
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Subclass factory
4
 *
5
 * @package Stencil
6
 */
7
8
/**
9
 * Class SubclassFactory
10
 */
11
class Stencil_Subclass_Factory {
12
	const CLASS_FORMAT = '%s_%s';
13
14
	/**
15
	 * Create Null object
16
	 *
17
	 * @param string $prefix Prefix for the Class to use (directory).
18
	 *
19
	 * @return mixed
20
	 * @throws InvalidArgumentException When something else but a non-empty string is passed.
21
	 */
22
	public static function create_null( $prefix ) {
23
		if ( ! is_string( $prefix ) || empty( $prefix ) ) {
24
			throw new InvalidArgumentException( 'Expected a non-empty string.' );
25
		}
26
27
		$class = sprintf( self::CLASS_FORMAT, $prefix, 'Null' );
28
29
		return new $class;
30
	}
31
32
	/**
33
	 * Create a new class if it exists
34
	 *
35
	 * @param string $class Class name.
36
	 * @param string $prefix Prefix of the class (directory).
37
	 *
38
	 * @return null
39
	 * @throws InvalidArgumentException For invalid class or prefix parameters.
40
	 */
41
	public static function create_if_exists( $class, $prefix ) {
42
		if ( ! is_string( $class ) || empty( $class ) ) {
43
			throw new InvalidArgumentException( 'Expected non-empty string for $class.' );
44
		}
45
46
		if ( ! is_string( $prefix ) || empty( $prefix ) ) {
47
			throw new InvalidArgumentException( 'Expected non-empty string for $prefix.' );
48
		}
49
50
		$class = self::format_class_name( $class );
51
		$class = sprintf( self::CLASS_FORMAT, $prefix, $class );
52
53
		if ( class_exists( $class ) ) {
54
			return new $class;
55
		}
56
57
		return null;
58
	}
59
60
	/**
61
	 * Create new class or Null object if it does not exist
62
	 *
63
	 * @param string $class Name of the class.
64
	 * @param string $prefix Prefix of the class.
65
	 *
66
	 * @return null
67
	 */
68
	public static function create_or_null( $class, $prefix ) {
69
		$result = self::create_if_exists( $class, $prefix );
70
71
		if ( is_null( $result ) ) {
72
			$result = self::create_null( $prefix );
73
		}
74
75
		return $result;
76
	}
77
78
	/**
79
	 * Uniform class name
80
	 *
81
	 * Class format: UpperCaseWords
82
	 *
83
	 * @param string $source Class name to format.
84
	 *
85
	 * @return string
86
	 */
87
	private static function format_class_name( $source ) {
88
89
		$class = strtolower( $source );
90
		$class = preg_replace( '~[^a-z]~', '_', $class );
91
92
		// Replace spaces with uppercase next character.
93
		if ( false !== strpos( $class, ' ' ) ) {
94
95
			/**
96
			 * Using callback via class because Anonymous functions are not guaranteed to be present
97
			 *
98
			 * @see: http://php.net/manual/en/functions.anonymous.php
99
			 * 5.3.0    Anonymous functions become available.
100
			 */
101
			$class = preg_replace_callback(
102
				'~_[a-z]~',
103
				array( __CLASS__, 'camel_case_callback' ),
104
				$class
105
			);
106
107
			$class = trim( $class );
108
		}
109
110
		$class = ucfirst( $class );
111
112
		return $class;
113
	}
114
115
	/**
116
	 * Replace ' {alphanum}' with uppercase of alphanum
117
	 *
118
	 * Example: 'stencil example' => 'stencilExample'
119
	 *
120
	 * @param array $matches Matched items in string.
121
	 *
122
	 * @return string
123
	 */
124
	private function camel_case_callback( $matches ) {
125
		return '_' . trim( strtoupper( $matches[0] ) );
126
	}
127
}
128