PHP code example of jonathanr / php-optional-result
1. Go to this page and download the library: Download jonathanr/php-optional-result 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/ */
jonathanr / php-optional-result example snippets
/**
* @return string
*/
function askDomain(string $name): ?String
{
try {
$json = Http::get('myResolverApi?name='.$name)->json();
if (isset($json['domain']) && ($json['domain'] !== null || $json['domain'] !== '')) {
return $json['domain'];
}
return null;
} catch (HttpException) {
throw new Exception('API No respond');
}
}
function isDomainExist(string $name): string
{
try {
$result = askDomain($name);
if (!empty($result)) {
return 'domain is' . $result;
}
} catch (Exception $e) {
return '';
}
}
echo isDomainExist('goooooooooooogle');
/**
* @return Ok<Some<string>|None>|Err<None>
*/
function askDomain(string $name): Ok|Err
{
try {
$json = Http::get('myResolverApi?name='.$name)->json();
if (isset($json['domain'])) {
return Result::ok(Option::some($json['domain']));
}
return Result::ok(Option::none());
} catch (HttpException) {
return Result::Err(Option::none());
}
}
function isDomainExist(string $name): string
{
$result = askDomain($name);
if ($result->isErr()) {
return 'Not sure. API not respond';
}
if ($result->isOk() && $result->get()->isSome()) {
return 'domain is'.$result->get()->isSome()->get();
}
return 'domain does not exist';
}
echo isDomainExist('goooooooooooogle');