PHP code example of gowork / safe
1. Go to this page and download the library: Download gowork/safe library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gowork / safe example snippets
$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'); // InvalidArgumentException
final class ExampleCommand extends Command
{
// ...
protected function execute(InputInterface $input, OutputInterface $output): int
{
$arguments = SafeConsoleInput::arguments($input);
// 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');
// ...
}
}