true, '1' => true, 'yes' => true, 'on' => true, 'false' => false, '0' => false, 'no' => false, 'off' => false, ); /** * Split "key=value" on the first "=". * * @param string $pair Argument as typed. * @return array|\WP_Error Key and value, or why not. */ public static function split( $pair ) { $pair = (string) $pair; $at = strpos( $pair, '=' ); if ( false === $at || 0 === $at ) { return new \WP_Error( 'adminkeep_cli_pair', sprintf( '"%s" is not a key=value pair.', $pair ) ); } return array( substr( $pair, 0, $at ), (string) substr( $pair, $at + 1 ) ); } /** * Parse one value against its schema entry. * * @param string $raw Value as typed. * @param array $spec Schema entry. * @return mixed|\WP_Error Typed value, or why not. */ public static function parse( $raw, array $spec ) { $raw = (string) $raw; $type = isset( $spec['type'] ) ? $spec['type'] : 'string'; switch ( $type ) { case 'bool': $word = strtolower( trim( $raw ) ); if ( ! array_key_exists( $word, self::BOOLS ) ) { return new \WP_Error( 'adminkeep_cli_value', sprintf( '"%s" is not a yes/no value. Use true or false.', $raw ) ); } return self::BOOLS[ $word ]; case 'int': if ( ! preg_match( '/^-?\d+$/', trim( $raw ) ) ) { return new \WP_Error( 'adminkeep_cli_value', sprintf( '"%s" is not a whole number.', $raw ) ); } return (int) trim( $raw ); case 'array': return array_values( array_filter( array_map( 'trim', explode( ',', $raw ) ), 'strlen' ) ); case 'string': default: if ( isset( $spec['choices'] ) && is_array( $spec['choices'] ) && ! in_array( $raw, $spec['choices'], true ) ) { return new \WP_Error( 'adminkeep_cli_value', sprintf( '"%s" is not allowed. Choose one of: %s.', $raw, implode( ', ', $spec['choices'] ) ) ); } return $raw; } } }