VOOZH about

URL: https://deepwiki.com/hypervel/http/2.2-input-and-data-access

⇱ Input & Data Access | hypervel/http | DeepWiki


Loading...
Menu

Input & Data Access

This page documents the methods for accessing and manipulating input data from HTTP requests. The Request class provides a comprehensive API for retrieving query parameters, POST data, JSON payloads, performing type conversions, and validating input existence. For information about file uploads, see File Uploads. For header access and content negotiation, see Headers & Content Negotiation.

Sources: src/Request.php1-985 src/Contracts/RequestContract.php1-442


Input Data Sources

The Request object aggregates data from multiple sources in the HTTP request. Understanding how these sources are combined is essential for correct data access.


Sources: src/Request.php407-422

The all() method at src/Request.php407-422 merges input data from query strings, POST data, and JSON bodies with uploaded files using array_replace_recursive(). This creates a unified input array that all other input methods operate on.


Basic Input Retrieval

Retrieving All Input

MethodDescriptionReturn Type
all()Get all input and filesarray
all($keys)Get specific keys from all inputarray
keys()Get keys of all input and filesarray

The all() method retrieves the complete merged input array:


Sources: src/Request.php407-422 src/Request.php322-328

Query Parameters and POST Data

The Request class inherits from Hyperf\HttpServer\Request, which provides access to specific data sources:

Inherited MethodSourceExample
query()URL query string?name=value
post()POST form dataForm submissions
input($key, $default)Unified input (query + POST + JSON)Any input source

Sources: src/Request.php31 (inheritance from HyperfRequest)

JSON Request Detection

The isJson() method at src/Request.php298-301 determines if the request contains JSON data by checking the Content-Type header:


Sources: src/Request.php298-301


Selective Input Access

Retrieving Specific Keys

Retrieving All Input Data Flow


Sources: src/Request.php387-402 src/Request.php140-148 src/Request.php85-94

only() Method

The only() method at src/Request.php387-402 retrieves a subset of input containing only the specified keys. It uses dot notation for nested data:


Sources: src/Request.php387-402

except() Method

The except() method at src/Request.php140-148 returns all input except the specified keys:


Sources: src/Request.php140-148

collect() Method

The collect() method at src/Request.php85-94 wraps input data in a Collection instance for fluent manipulation:


Sources: src/Request.php85-94


Type Conversion Methods

The Request class provides methods to retrieve input with automatic type conversion. These methods handle type coercion and provide sensible defaults.

Type Conversion Method Taxonomy


Sources: src/Request.php74-80 src/Request.php278-281 src/Request.php177-180 src/Request.php185-196 src/Request.php101-112 src/Request.php122-133

Boolean Conversion

The boolean() method at src/Request.php74-80 uses PHP's FILTER_VALIDATE_BOOLEAN to convert input:

Input ValueResult
"1", "true", "on", "yes"true
All other valuesfalse
Missing keyDefault value

Sources: src/Request.php74-80 src/Contracts/RequestContract.php38-42

Integer and Float Conversion

The integer() and float() methods perform simple type casting:


Sources: src/Request.php278-281 src/Request.php177-180

String Conversion

The string() and str() methods at src/Request.php185-196 return a Stringable instance from the Hyperf\Stringable\Str facade:


Sources: src/Request.php185-196 src/Contracts/RequestContract.php89-96

Date Conversion

The date() method at src/Request.php101-112 converts input to a Carbon instance:


The method throws Carbon\Exceptions\InvalidFormatException if the date cannot be parsed.

Sources: src/Request.php101-112 src/Contracts/RequestContract.php50-54

Enum Conversion

The enum() method at src/Request.php122-133 converts input to PHP 8.1+ enum instances using the enum's tryFrom() method:


Sources: src/Request.php122-133 src/Contracts/RequestContract.php57-64


Input Existence Validation

The Request class provides multiple methods to check whether input keys exist and contain meaningful values.

Input Validation Methods Decision Tree


Sources: src/Request.php153-156 src/Request.php161-172 src/Request.php286-293 src/Request.php306-317 src/Request.php377-380 src/Request.php56-67 src/Request.php201-207

Existence Checks

MethodChecksReturns
exists($key)Key exists in inputbool
has($key)Key exists in input (inherited)bool
missing($key)Key does not existbool
hasAny($keys)Any of the keys existbool

Sources: src/Request.php153-156 src/Request.php377-380 src/Request.php201-207

Value Presence Checks

MethodChecksEmpty Strings
filled($key)Key exists with non-empty valueReturns false
isNotFilled($key)Key is empty or missingReturns true
anyFilled($keys)Any key has non-empty valueChecks all

The filled() method at src/Request.php161-172 considers a key filled if:

  • The key exists AND
  • The value is not an empty string after trimming AND
  • The value is not a boolean or array

Sources: src/Request.php161-172 src/Request.php306-317 src/Request.php56-67

Empty String Detection

The isEmptyString() method at src/Request.php286-293 determines if a value should be considered empty for the filled() check:


Sources: src/Request.php286-293


Input Manipulation

The Request class provides methods to modify the input data during request processing. These modifications use Hyperf's Context system to maintain request-scoped changes.

Input Manipulation Flow


Sources: src/Request.php335-343 src/Request.php350-358 src/Request.php365-372

merge() Method

The merge() method at src/Request.php335-343 adds or updates input keys by merging new data with existing input:


The merge is performed by Context::override() which applies array_merge() to the parsed data stored in the context at key $this->contextkeys['parsedData'].

Sources: src/Request.php335-343 src/Contracts/RequestContract.php163-167

replace() Method

The replace() method at src/Request.php350-358 replaces existing input values using array_replace():


Unlike merge(), replace() uses array_replace() instead of array_merge(), which has different behavior for numeric keys.

Sources: src/Request.php350-358 src/Contracts/RequestContract.php170-174

mergeIfMissing() Method

The mergeIfMissing() method at src/Request.php365-372 only adds keys that don't already exist in the input:


This method filters the input array using the missing() check before calling merge().

Sources: src/Request.php365-372 src/Contracts/RequestContract.php177-181


Conditional Execution

The Request class provides methods to execute callbacks conditionally based on input presence or value.

whenHas() Method

The whenHas() method at src/Request.php572-583 executes a callback if a key exists:


Sources: src/Request.php572-583 src/Contracts/RequestContract.php251-255

whenFilled() Method

The whenFilled() method at src/Request.php554-565 executes a callback if a key has a non-empty value:


Sources: src/Request.php554-565 src/Contracts/RequestContract.php244-248


Implementation Details

Context-Based Storage

The Request class stores input data in Hyperf's Context system, which provides request-scoped storage in the Swoole environment. The contextkeys['parsedData'] key holds the parsed input data that is retrieved by getInputData().

When methods like merge(), replace(), and mergeIfMissing() modify input, they use Context::override() to update the context value:


This ensures that:

  1. Modifications are scoped to the current request
  2. Subsequent input access methods see the updated data
  3. No pollution occurs between concurrent requests in Swoole

Sources: src/Request.php335-343 src/Request.php350-358

Inheritance Hierarchy


Sources: src/Request.php31 src/Contracts/RequestContract.php18

The Request class at src/Request.php31 extends Hyperf\HttpServer\Request which provides the base input access methods like input(), query(), and post(). The Hypervel Request adds enhanced type conversion, validation checks, and input manipulation capabilities.

Dot Notation Support

Methods like only(), except(), and data access use Hyperf\Collection\Arr helpers which support dot notation for nested array access:


Sources: src/Request.php387-402 src/Request.php140-148


Summary Table: All Input Access Methods

MethodPurposeReturn Type
all($keys = null)Get all input, optionally filtered by keysarray
only($keys)Get subset with specified keysarray
except($keys)Get all except specified keysarray
keys()Get all input keysarray
collect($key = null)Get input as CollectionCollection
boolean($key, $default = false)Get as booleanbool
integer($key, $default = 0)Get as integerint
float($key, $default = 0.0)Get as floatfloat
string($key, $default = null)Get as StringableStringable
str($key, $default = null)Alias for string()Stringable
date($key, $format = null, $tz = null)Get as Carbon date?Carbon
enum($key, $enumClass)Get as enum instance?TEnum
exists($key)Check if key existsbool
has($key)Check if key exists (inherited)bool
missing($key)Check if key is missingbool
hasAny($keys)Check if any key existsbool
filled($key)Check if non-empty valuebool
anyFilled($keys)Check if any key is filledbool
isNotFilled($key)Check if empty or missingbool
isEmptyString($key)Check if value is empty stringbool
isJson()Check if Content-Type is JSONbool
merge($input)Merge new inputstatic
replace($input)Replace input valuesstatic
mergeIfMissing($input)Merge only missing keysstatic
whenHas($key, $callback, $default = null)Execute callback if key existsmixed
whenFilled($key, $callback, $default = null)Execute callback if filledmixed

Sources: src/Request.php48-985 src/Contracts/RequestContract.php20-441