Skip to content

Commit e971c95

Browse files
committed
feat: add request validation
1 parent bc80630 commit e971c95

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

composer.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "leafs/http",
3-
"description": "Leaf PHP HTTP module.",
3+
"description": "Http abstraction for Leaf PHP",
44
"keywords": [
55
"http",
66
"request",
@@ -32,6 +32,7 @@
3232
"minimum-stability": "dev",
3333
"prefer-stable": true,
3434
"require": {
35-
"leafs/anchor": "*"
35+
"leafs/anchor": "*",
36+
"leafs/form": "v2.x-dev"
3637
}
3738
}

src/Request.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,25 @@ public static function headers($key = null, bool $safeData = true)
306306
Headers::get($key, $safeData);
307307
}
308308

309+
/**
310+
* Validate the request data
311+
* @param array $rules The rules to validate against
312+
* @return bool
313+
*/
314+
public static function validate(array $rules)
315+
{
316+
return \Leaf\Form::validate(static::body(false), $rules);
317+
}
318+
319+
/**
320+
* Handle errors from validation
321+
* @return array
322+
*/
323+
public static function errors()
324+
{
325+
return \Leaf\Form::errors();
326+
}
327+
309328
/**
310329
* Get Content Type
311330
* @return string|null
@@ -360,6 +379,7 @@ public static function getMediaTypeParams(): array
360379
public static function getContentCharset(): ?string
361380
{
362381
$mediaTypeParams = static::getMediaTypeParams();
382+
363383
if (isset($mediaTypeParams['charset'])) {
364384
return $mediaTypeParams['charset'];
365385
}
@@ -412,7 +432,7 @@ public static function getHostWithPort(): string
412432
*/
413433
public static function getPort(): int
414434
{
415-
return (int)$_SERVER['SERVER_PORT'] ?? 80;
435+
return (int) $_SERVER['SERVER_PORT'] ?? 80;
416436
}
417437

418438
/**

0 commit comments

Comments
 (0)