Skip to content

Commit

Permalink
Improving Pipeline code examples in README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewatts committed Mar 7, 2024
1 parent b787abb commit b20f36b
Showing 1 changed file with 24 additions and 15 deletions.
39 changes: 24 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ They are useful for multi-step data processing, http middleware, database queryi
Here's an example of how to use it to validation, filter, transform and save an incoming get request.

```php
// 1. Prepare the request
class PrepareRequest
{
public function handle($request, $next)
Expand All @@ -284,41 +285,49 @@ class PrepareRequest
$query = $uri->getQuery(); // Get the query string (e.g., "param1=value1&param2=value2")
parse_str($query, $queryParams); // Parse the query string into an array

return $next($request);
return $next($queryParams);
}
}

// 1. Create a custom pipe for validation
// 2. Validate the request
class ValidateRequest
{
public function handle($request, $next)
public function handle($data, $next)
{
// Validate parameters (e.g., check if 'name', 'age', and 'email' exist)
// Validate parameters
// (e.g. check if 'email' and 'password' exist, validate 'email' and 'password' etc)

// If invalid then $data['valid'] = false, else $data['valid'] = true;

return $next($request);
return $next($data);
}
}

// 2. Create a pipe for data transformation
// 2. Transform the request
class TransformRequest
{
public function handle($request, $next)
public function handle($data, $next)
{
// Capitalize the 'name' parameter
$request['name'] = ucfirst($request['name']);
$data['password'] = bcrypt($data['password']);

return $next($request);
return $next($data);
}
}

// 3. Create a pipe for Saving the data
// 3. Save the data, or log errors
class SaveRequest
{
public function handle($request, $next)
public function handle($data, $next)
{
// Save to database
if (!$data['valid']) {
// Log errors...

return $next($data);
}

$data['saved'] = true;

return $next($request);
return $next($data);
}
}

Expand All @@ -335,7 +344,7 @@ App::get('/', function ($request) {
->thenReturn();

// 5. Respond with the processed data
return response()->json(['message' => 'Request processed successfully', 'result' => $result])->get();
return response()->json(['result' => $result])->get();
});
```

Expand Down

0 comments on commit b20f36b

Please sign in to comment.