Skip to content
Viames Marino edited this page Oct 19, 2026 · 2 revisions

Pair framework: Post

Pair\Helpers\Post is a legacy static helper for request values. It does not expose submitted fields as object properties.

When to use

Use it when maintaining existing form and ActiveRecord flows that already depend on its casting rules. For new explicit web controllers, prefer Input; for JSON API modules, use Request.

Main behavior

  • get() reads $_POST when REQUEST_METHOD is exactly POST; otherwise it reads $_REQUEST.
  • A trailing [] is removed from field names, and spaces become underscores.
  • Typed helpers cast values; they do not validate input shape, authorization, or domain rules.
  • Casting obtains the Application singleton, so use these helpers within an initialized Pair runtime.

Common methods

Method Behavior
get(string $name, string $type = 'string', mixed $default = null): mixed Reads a value and applies the selected cast.
string(string $name, ?string $default = null): string Casts the value to a string.
trim(string $name, ?string $default = null): string Reads a string and trims whitespace.
int($name, $default = null): int Casts to an integer.
float(string $name, ?float $default = null): float Casts to a float.
bool(string $name, ?bool $default = null): bool Recognizes the strings true and false, otherwise uses a PHP boolean cast.
array(string $name, ?array $default = null): array Casts to an array.
date(string $name, ?DateTime $default = null): ?DateTime Parses a date using the current datepicker format and user timezone.
datetime(string $name, ?DateTime $default = null): ?DateTime Parses a date-time using the current datepicker format and user timezone.

Defaults are used for missing keys and are cast as well. An explicitly submitted empty string is not replaced by the default. With no explicit default, missing strings become '', integers become 0, booleans become false, and arrays become [].

Implementation examples

Basic usage

use Pair\Helpers\Post;

$email = Post::trim('email');
$name = Post::trim('name');
$quantity = Post::int('quantity', 1);

Validation flow

use Pair\Helpers\Post;

if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') {
	throw new RuntimeException('POST required');
}

$payload = [
	'email' => Post::trim('email'),
	'name' => Post::trim('name'),
];

if (!filter_var($payload['email'], FILTER_VALIDATE_EMAIL)) {
    throw new RuntimeException('Invalid email');
}

This validates the email shape only. Cookie-authenticated writes still need CSRF validation and application authorization before persistence; see Form.

Presence and submission helpers

  • sent(string $name): bool checks key presence in $_REQUEST, including a key whose value is null. It does not use the same source-selection logic as get().
  • submitted(): bool returns whether $_POST is non-empty. It is not an HTTP method check: an empty POST or a JSON request can return false.

Common pitfalls

  • Do not assume POST keys always exist.
  • Always normalize/cast values before persistence.
  • new Post() followed by $post->email does not read request data; call Post::trim('email') or another static accessor.
  • Casting is permissive: for example Post::bool() treats a non-empty unrecognized string such as off as true. Use Input::bool() when its explicit true/false vocabulary fits the endpoint.
  • $_REQUEST is populated by PHP; changing $_POST later does not automatically synchronize it. Tests and custom adapters must set the intended source explicitly.

See also: Input, Request, Form, ActiveRecord, Controller.

Clone this wiki locally