Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Type_Boolean | |
94.12% |
16 / 17 |
|
75.00% |
3 / 4 |
6.01 | |
0.00% |
0 / 1 |
| parse | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| __toString | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| schema | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\Jetpack\Schema\Types; |
| 4 | |
| 5 | use Automattic\Jetpack\Schema\Parser; |
| 6 | use Automattic\Jetpack\Schema\Schema_Error; |
| 7 | |
| 8 | class Type_Boolean implements Parser { |
| 9 | public function parse( $value, $_context ) { |
| 10 | if ( is_bool( $value ) ) { |
| 11 | return $value; |
| 12 | } |
| 13 | |
| 14 | $loose_values = array( |
| 15 | // Numbers used as booleans |
| 16 | '1', |
| 17 | '0', |
| 18 | 1, |
| 19 | 0, |
| 20 | // WordPress can return empty string for false. |
| 21 | '', |
| 22 | ); |
| 23 | if ( ! in_array( $value, $loose_values, true ) ) { |
| 24 | throw new Schema_Error( 'Invalid boolean value', $value ); |
| 25 | } |
| 26 | return filter_var( $value, FILTER_VALIDATE_BOOLEAN ); |
| 27 | } |
| 28 | |
| 29 | public function __toString() { |
| 30 | return 'boolean'; |
| 31 | } |
| 32 | |
| 33 | #[\ReturnTypeWillChange] |
| 34 | public function jsonSerialize() { |
| 35 | return $this->schema(); |
| 36 | } |
| 37 | |
| 38 | public function schema() { |
| 39 | return array( |
| 40 | 'type' => (string) $this, |
| 41 | ); |
| 42 | } |
| 43 | } |