Meta_Box_Validator   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 16
c 2
b 0
f 0
dl 0
loc 67
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A has_required_fields() 0 6 4
A validate() 0 2 1
A verify_meta_box() 0 17 4
A has_valid_view() 0 5 4
1
<?php
2
3
4
declare(strict_types=1);
5
6
/**
7
 * Validates a Post Type model.
8
 *
9
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
10
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
11
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
12
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
13
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
14
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
15
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
16
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
17
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
18
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
19
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
20
 *
21
 * @author Glynn Quelch <[email protected]>
22
 * @license http://www.opensource.org/licenses/mit-license.html  MIT License
23
 * @package PinkCrab\Registerables\Validator
24
 */
25
26
namespace PinkCrab\Registerables\Validator;
27
28
use PinkCrab\Registerables\Meta_Box;
29
use PinkCrab\Registerables\Post_Type;
30
use PinkCrab\Registerables\Validator\Abstract_Validator;
31
use PinkCrab\Registerables\Module\Middleware\Registerable;
32
33
class Meta_Box_Validator extends Abstract_Validator {
34
35
	protected const REQUIRED_FIELDS = array( 'key', 'label' );
36
37
	/**
38
	 * Validates the class passed.
39
	 *
40
	 * @param \PinkCrab\Registerables\Module\Middleware\Registerable $object_instance
41
	 * @return bool
42
	 */
43
	public function validate( Registerable $object_instance ): bool { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClass
44
		return false; //no op
45
	}
46
47
	/**
48
	 * Verifies a metabox
49
	 *
50
	 * @param mixed $meta_box
51
	 * @return bool
52
	 */
53
	public function verify_meta_box( $meta_box ): bool {
54
		// If this is not a valid post type, just bail here.
55
		if ( ! is_object( $meta_box ) || ! is_a( $meta_box, Meta_Box::class ) ) {
56
			$this->add_error( sprintf( '%s is not a valid Meta Box Model', is_object( $meta_box ) ? get_class( $meta_box ) : \gettype( $meta_box ) ) );
57
			return false;
58
		}
59
60
		/* @var Meta_Box $object, already confirmed as a post type */
61
62
		// Ensure all required fields are set.
63
		$this->has_required_fields( $meta_box );
64
65
		// Ensure can render view.
66
		$this->has_valid_view( $meta_box );
67
68
		// Check if the passed object has any errors.
69
		return ! $this->has_errors();
70
	}
71
72
	/**
73
	 * Checks the model has the required fields.
74
	 *
75
	 * @param Meta_Box $meta_box
76
	 * @return void
77
	 */
78
	protected function has_required_fields( Meta_Box $meta_box ): void {
79
		foreach ( self::REQUIRED_FIELDS as $field ) {
80
			if ( ! is_string( $meta_box->{$field} )
81
			|| \mb_strlen( $meta_box->{$field} ) === 0
82
			) {
83
				$this->add_error( sprintf( '%s is not set on %s Meta Box Model', $field, get_class( $meta_box ) ) );
84
			}
85
		}
86
	}
87
88
	/**
89
	 * Checks if the meta box has a valid view callable or a template
90
	 * which can be rendered using VIEW.
91
	 *
92
	 * @param \PinkCrab\Registerables\Meta_Box $meta_box
93
	 * @return void
94
	 */
95
	protected function has_valid_view( Meta_Box $meta_box ): void {
96
		if ( ! \is_callable( $meta_box->view )
97
		&& ( ! is_string( $meta_box->view_template ) || \mb_strlen( $meta_box->view_template ) === 0 )
98
		) {
99
			$this->add_error( sprintf( '%s doesn\'t have a valid view defined.', get_class( $meta_box ) ) );
100
		}
101
	}
102
}
103