1. Go to this page and download the library: Download martyrer/throwless-php 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/ */
martyrer / throwless-php example snippets
use Martyrer\Throwless\Ok;
use Martyrer\Throwless\Err;
use Martyrer\Throwless\Result;
// Creating Results
$success = new Ok(42); // Ok<int, mixed>
$failure = new Err("something went wrong"); // Err<mixed, string>
// Checking Result type
$success->isOk(); // true
$success->isErr(); // false
$failure->isOk(); // false
$failure->isErr(); // true
// Unwrapping values
$success->unwrap(); // 42
$failure->unwrapErr(); // "something went wrong"
// Using default values
$success->unwrapOr(0); // 42
$failure->unwrapOr(0); // 0
$result = new Ok(42);
$result->isOk(); // true
$error = new Err("failed");
$error->isOk(); // false
$result = new Ok(42);
$result->isErr(); // false
$error = new Err("failed");
$error->isErr(); // true
class DefaultProvider implements DefaultValueProvider {
public function getDefault(): int {
return 0;
}
}
$result = new Err("error");
$value = $result->unwrapOrDefault(new DefaultProvider()); // 0
$result = new Ok(42);
$value = $result->expect("This should never fail"); // 42
$error = new Err("oops");
$value = $error->expect("Critical error"); // throws UnwrapException with message "Critical error"
$error = new Err("not found");
$value = $error->expectErr("Expected error not found"); // "not found"
$success = new Ok(42);
$value = $success->expectErr("Expected an error"); // throws UnwrapException with message "Expected an error"
// WARNING: Only use when you are absolutely certain the Result is Ok
// Otherwise, it will cause undefined behavior
$result = new Ok(42);
$value = $result->unwrapUnchecked(); // 42
// WARNING: Only use when you are absolutely certain the Result is Err
// Otherwise, it will cause undefined behavior
$error = new Err("error");
$value = $error->unwrapErrUnchecked(); // "error"
class UserAuthenticator {
public function authenticate(string $username, string $password): Result {
$user = $this->findUser($username);
if ($user === null) {
return new Err("User not found");
}
return password_verify($password, $user->password)
? new Ok($user)
: new Err("Invalid password");
}
}
// Usage
$auth = new UserAuthenticator();
$result = $auth->authenticate("john", "password123")
->map(fn($user) => $user->toArray())
->mapErr(fn($error) => ["error" => $error]);
class ApiClient {
public function fetchData(string $url): Result {
try {
$response = file_get_contents($url);
$data = json_decode($response, true);
return $data === null
? new Err("Invalid JSON response")
: new Ok($data);
} catch (Throwable $e) {
return new Err($e->getMessage());
}
}
}
// Usage
$client = new ApiClient();
$result = $client->fetchData("https://api.example.com/data")
->andThen(function($data) {
return isset($data['items'])
? new Ok($data['items'])
: new Err("Missing items in response");
})
->map(fn($items) => array_map(fn($item) => $item['name'], $items));
bash
composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.