Schema::as_string(), * 'age' => Schema::as_number()->fallback(0), * 'is_active' => Schema::as_boolean()->nullable(), * 'tags' => Schema::as_array(Schema::as_string()) * ] * ); * * $input_data = [ * 'name' => 'John Doe', * 'age' => 30, * 'is_active' => null, * 'tags' => ['tag1', 'tag2'] * ]; * * $parsed_data = $my_schema->parse($input_data); */ class Schema { const PACKAGE_VERSION = '0.2.0'; public static function as_string() { return new Schema_Parser( new Type_String() ); } /** * @param Parser $parser - The parser to apply to each array item when $data is parsed. * * @return Schema_Parser */ public static function as_array( Parser $parser ) { return new Schema_Parser( new Type_Array( $parser ) ); } /** * @param array $assoc_parser_array - An associative array of ["key" => "Parser"] pairs * * @return Schema_Parser */ public static function as_assoc_array( $assoc_parser_array ) { return new Schema_Parser( new Type_Assoc_Array( $assoc_parser_array ) ); } public static function as_boolean() { return new Schema_Parser( new Type_Boolean() ); } public static function as_number() { return new Schema_Parser( new Type_Number() ); } public static function as_float() { return new Schema_Parser( new Type_Float( true ) ); } /** * @param array $allowed_values - An array of values that are allowed for this enum. * * @return Schema_Parser */ public static function enum( $allowed_values ) { return new Schema_Parser( new Type_Enum( $allowed_values ) ); } public static function any_json_data() { return new Schema_Parser( new Type_Any_JSON() ); } /** * Mark a schema as void - it should have no data worth keeping, and * will always parse to null. */ public static function as_void() { return new Schema_Parser( new Type_Void() ); } /** * Use With Caution! This will not parse the data - it will simply return it as-is. * This is useful for delivering read-only data that we don't need to parse server-side. * * @see Type_Any */ public static function as_unsafe_any() { return new Schema_Parser( new Type_Any() ); } /** * @var \Automattic\Jetpack\Schema\Parser $parser - The parser to apply to each array item when $data is parsed. */ public static function either( ...$parsers ) { $or = new Modifier_Fallback(); foreach ( $parsers as $parser ) { $or->add_fallback_parser( $parser ); } return new Schema_Parser( $or ); } }