Safe accessors wraps unsafe or uncertain associative data structures and provides methods of safe type casting. Mainly for Symfony.
- casts value to
bool(true, false, 0, 1) if possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
- casts value to
bool(true, false, 0, 1) - or returns default
- casts value to
stringif possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
- casts value to
stringif possible - or returns default when value not set or is
null - or throws
InvalidArgumentExceptionwhen value notnullbut cannot be casted
- casts value to
stringif possible - or returns
null
- casts value to
stringif possible - or returns default
- casts value to
intif possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
- casts value to
intif possible - or returns default when value not set or is
null - or throws
InvalidArgumentExceptionwhen value notnullbut cannot be casted
- casts value to
intif possible - or returns
null
- casts value to
intif possible - or returns default
- casts value to
floatif possible - or throws
InvalidArgumentExceptionwhen value set but cannot be casted - or returns default when value not set
- casts value to
floatif possible - or returns default when value not set or is
null - or throws
InvalidArgumentExceptionwhen value notnullbut cannot be casted
- casts value to
floatif possible - or returns
null
- casts value to
floatif possible - or returns default
- casts value to array of strings if possible
- or throws
InvalidArgumentExceptionwhen some item cannot be casted
- casts value to array of strings skipping items that cannot be casted
- casts value to array of strings replacing with default items that cannot be casted
- casts value to array of ints if possible
- or throws
InvalidArgumentExceptionwhen some item cannot be casted
- casts value to array of ints skipping items that cannot be casted
- casts value to array of ints replacing with default items that cannot be casted
- casts value to array of floats if possible
- or throws
InvalidArgumentExceptionwhen some item cannot be casted
- casts value to array of floats skipping items that cannot be casted
- casts value to array of floats replacing with default items that cannot be casted
- casts value to associative array and wraps with
SafeAssocArray - or throws
InvalidArgumentExceptionwhen value cannot be casted
- casts value to list of associative arrays and wraps with
SafeAssocList - or throws
InvalidArgumentExceptionwhen value cannot be casted
$user = [
'name' => 'John',
'age' => 18,
'sports' => ['football', 'handball'],
];
$safe = SafeAssocArray::from($user);
$safe->string('name'); // 'John'
$safe->int('age'); // 18
$safe->string('nickname', '--'); // '-'
$safe->stringNullable('nickname'); // NULL
$safe->string('nickname'); // InvalidArgumentException
$safe->strings('sports'); // ['football', 'handball']
$safe->ints('sports'); // InvalidArgumentExceptionfinal class ExampleCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$arguments = SafeConsoleInput::arguments($input);
// require string from argument
$file = $arguments->string('name');
$options = SafeConsoleInput::options($input);
// integer with default value
$limit = $options->int('limit', 20);
// optional integer value
$pageOrNull = $options->intNullable('page');
// bool
$isDryRun = $options->bool('dry-run', false);
// string[]
$tags = $options->strings('tag');
// int[]
$tags = $options->ints('status');
}
}final class ExampleAction extends Command
{
// ...
public function __invoke(Request $request): Response
{
$safeRequest = SafeRequest::from($request);
$query = $safeRequest->query();
$post = $safeReques->request();
$attributes = $safeReques->attributes();
$ip = $safeReques->ip();
$postId = $attributes->string('postId');
$tags = $post->strings('tags');
// ...
}
}