PHP code example of byjesper / laravel-decision-support
1. Go to this page and download the library: Download byjesper/laravel-decision-support 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/ */
byjesper / laravel-decision-support example snippets
use ByJesper\DecisionSupport\Contracts\FactProvider;
use ByJesper\DecisionSupport\Enums\FactType;
use ByJesper\DecisionSupport\Facts\{FactDefinition, FactValue, FactVocabulary, PendingInteraction};
use ByJesper\DecisionSupport\Runtime\GuideContext;
final class EmploymentFactProvider implements FactProvider
{
public function vocabulary(): FactVocabulary
{
return new FactVocabulary([
new FactDefinition('tenure_years', FactType::Number),
]);
}
public function resolve(string $fact, GuideContext $context): FactValue|PendingInteraction
{
return new FactValue($this->lookupTenure($context));
// ...or `new PendingInteraction($interaction)` to suspend for host input.
}
}
use ByJesper\DecisionSupport\DecisionSupportManager;
app(DecisionSupportManager::class)
->registerProvider('employment-eligibility', EmploymentFactProvider::class);
use ByJesper\DecisionSupport\Conditions\Condition;
use ByJesper\DecisionSupport\Enums\Operator;
use ByJesper\DecisionSupport\Testing\GuideBuilder;
$definition = GuideBuilder::make('employment-eligibility')
->profile('phased')
->question('q_employed', 'Are you employed?', 'employed', 'boolean')
->fact('f_tenure', 'tenure_years')
->decision('d_tenure')
->outcome('senior', 'Eligible (senior)', 'You qualify under the senior track.')
->outcome('junior', 'Eligible (junior)')
->outcome('no', 'Not eligible')
->edge('q_employed', 'f_tenure', 'true')
->edge('q_employed', 'no', 'false')
->edge('f_tenure', 'd_tenure')
->edge('d_tenure', 'senior', 'out', Condition::structured('tenure_years', Operator::GreaterThanOrEqual, 5))
->edge('d_tenure', 'junior', 'out', Condition::always())
->build();
$state = $runner->start($definition, [], 'da'); // da → base
$state = $runner->start($definition, [], 'de', 'da'); // de → da → base
use ByJesper\DecisionSupport\Conditions\Condition;
use ByJesper\DecisionSupport\Enums\Operator;
Condition::structured('tenure_years', Operator::GreaterThanOrEqual, 5);
Condition::expression('tenure_years >= 5 and contract_type == "permanent"');
Condition::always(); // default / else branch
Condition::unknown('tenure_years'); // matches only when the fact is unresolved
use ByJesper\DecisionSupport\Publishing\GuidePublisher;
$result = app(GuidePublisher::class)->publish($version);
if ($result->fails()) {
// Nothing was published. Each error has a code, message, and optional nodeKey.
foreach ($result->errors as $error) {
logger()->warning($error->code, ['message' => $error->message, 'node' => $error->nodeKey]);
}
}
$definition = $version->fresh()->toDefinition(); // the published snapshot