1. Go to this page and download the library: Download ailixter/gears-value 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/ */
ailixter / gears-value example snippets
use Ailixter\Gears\IntValue;
use Ailixter\Gears\ValueInterface;
class Positive extends IntValue
{
protected function validate()
{
if ($this->getValue() < 1) {
$this->addError('value', 'is not positive');
}
}
}
function calculate(int $int): Positive
{
$result = new Positive($int * $int);
if ($result->getValue() > 81) {
$result->addError('value', 'too big');
}
return $result;
}
function readyForUnexpected(IntValue $int)
{
if ($int->getErrors()) {
$result = 81;
} else {
$result = $int->getValue();
}
echo $result - 1;
}
function wantNoUnexpected(int $int)
{
echo $int - 1;
}
readyForUnexpected(calculate(8)); // 63
readyForUnexpected(calculate(10)); // 80
wantNoUnexpected(calculate(8)->get()); // 63
try {
wantNoUnexpected(calculate(10)->get()); // exception - too big
} catch (Ailixter\Gears\Exceptions\ValueException $ve) {
echo PHP_EOL, $ve->getMessage(), ': ';
print_r($ve->getValue()->getErrors());
}
try {
calculate(0); // exception - not positive
} catch (Ailixter\Gears\Exceptions\ValueException $ve) {
echo PHP_EOL, $ve->getMessage(), ': ';
print_r($ve->getValue()->getErrors());
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.