Filter::validate_integer()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
namespace Intraxia\Jaxion\Http;
3
4
use Intraxia\Jaxion\Contract\Http\Filter as FilterContract;
5
6
/**
7
 * Class Filter
8
 *
9
 * Generates the rules used by the WP-API to validate and sanitize and
10
 *
11
 * @package Intraxia\Jaxion
12
 * @subpackage Http
13
 */
14
class Filter implements FilterContract {
15
	/**
16
	 * Filter rules.
17
	 *
18
	 * @var array
19
	 */
20
	protected $rules;
21
22
	/**
23
	 * Instantiates a new filter with the provided rules array.
24
	 *
25
	 * @param array $rules
26
	 */
27 36
	public function __construct( $rules = array() ) {
28 36
		$this->rules = $rules;
29 36
	}
30
31
	/**
32
	 * Generates argument rules.
33
	 *
34
	 * Returns an array matching the WP-API format for argument rules,
35
	 * including sanitization, validation, required, or defaults.
36
	 *
37
	 * @return array
38
	 */
39 21
	public function rules() {
40 21
		$args = array();
41
42 21
		foreach ( $this->rules as $arg => $validation ) {
43 21
			if ( ! $validation || ! is_string( $validation ) ) {
44 3
				continue;
45
			}
46
47 18
			$args[ $arg ] = $this->parse_validation( $validation );
48 14
		}
49
50 21
		return $args;
51
	}
52
53
	/**
54
	 * Parses a validation string into a WP-API compatible rule.
55
	 *
56
	 * @param string $validation
57
	 *
58
	 * @return array
59
	 *
60
	 * @todo The next rule added needs to refactor this process.
61
	 */
62 18
	protected function parse_validation( $validation ) {
63 18
		$validation = explode( '|', $validation );
64
65 18
		$rules = array();
66
67 18
		foreach ( $validation as $rule ) {
68 18
			if ( 0 === strpos( $rule, 'default' ) ) {
69 9
				$rule_arr = explode( ':', $rule );
70
71 9
				$rules['default'] = count( $rule_arr ) === 2 ? array_pop( $rule_arr ) : '';
72 6
			}
73
74 18
			if ( 0 === strpos( $rule, 'oneof' ) ) {
75 3
				list( $rule, $values ) = explode( ':', $rule );
76
77 3
				$values   = explode( ',', $values );
78 1
				$callback = function ( $value ) use ( $values ) {
79 3
					if ( in_array( $value, $values, true ) ) {
80 3
						return true;
81
					}
82
83 3
					return false;
84 3
				};
85
86 3
				$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
87 2
			}
88
89 6
			switch ( $rule ) {
90 18
				case 'required':
91 3
					$rules['required'] = true;
92 3
					break;
93 15
				case 'integer':
94 6
					$callback                   = array( $this, 'validate_integer' );
95 6
					$rules['validate_callback'] = isset( $rules['validate_callback'] ) ? $this->add_callback( $rules['validate_callback'], $callback ) : $callback;
96
97 6
					$callback                   = array( $this, 'make_integer' );
98 6
					$rules['sanitize_callback'] = isset( $rules['sanitize_callback'] ) ? $this->add_callback( $rules['sanitize_callback'], $callback ) : $callback;
99 6
					break;
100
			}
101 12
		}
102
103 18
		return $rules;
104
	}
105
106
	/**
107
	 * Validate that provided value is an integer.
108
	 *
109
	 * @param mixed $value
110
	 *
111
	 * @return bool
112
	 */
113 9
	public function validate_integer( $value ) {
114 9
		return filter_var( $value, FILTER_VALIDATE_INT ) !== false;
115
	}
116
117
	/**
118
	 * Casts a provided value to an integer.
119
	 *
120
	 * @param mixed $value
121
	 *
122
	 * @return int
123
	 */
124 6
	public function make_integer( $value ) {
125 6
		return (int) $value;
126
	}
127
128
	/**
129
	 * Creates a new callback that connects the previous and next callback.
130
	 *
131
	 * @param callable $previous
132
	 * @param callable $next
133
	 *
134
	 * @return \Closure;
0 ignored issues
show
Documentation introduced by
The doc-type \Closure; could not be parsed: Expected "|" or "end of type", but got ";" at position 8. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
135
	 */
136
	private function add_callback( $previous, $next ) {
137
		return function ( $value ) use ( $previous, $next ) {
138
			if ( call_user_func( $previous, $value ) ) {
139
				return call_user_func( $next, $value );
140
			}
141
142
			return false;
143
		};
144
	}
145
}
146