jeyroik/extas-terms

There is no license information available for the latest version (0.2.0) of this package.

Provides terms models

0.2.0 2020-09-21 13:13 UTC

This package is auto-updated.

Last update: 2024-10-21 21:28:33 UTC


README

tests codecov.io PHPStan Enabled

Latest Stable Version Total Downloads Dependents

Описание

Пакет предоставляет модель для работы с термами (слагаемыми).

Терм - именованный вычисляемый параметр.

Использование

Получаем список термов:

use \extas\interfaces\terms\ITerm;
$terms = $this->terms()->all([ITerm::FIELD__TAGS => 'some.tag']);

Получаем список калькуляторов:

use extas\interfaces\terms\ITermCalculatorDescription;
$calculators = $this->termsCalculators()->all();

Вычисляем термы:

use extas\interfaces\terms\ITerm;
use extas\interfaces\terms\ITermCalculatorDescription;
use extas\interfaces\terms\ITermCalculationResult;
use extas\interfaces\terms\ITermCalculator;

/**
 * @var ITerm[] $terms
 * @var ITermCalculatorDescription[] $calculators
 */

$calculated = [];

foreach ($calculators as $calculatorDescription) {
    /**
     * @var ITermCalculationResult $result
     * @var ITermCalculator $calculator
     */
    $calculator = $calculatorDescription->buildClassWithParameters([]);
    $result = $calculator->calculateTerms($terms, ['some' => 'args']);

    /**
     * Or you can just 
     * $result = $calculatorDescription->runWithParameters([], 'calculateTerms', $terms);
     */

    /**
     * You should iterate terms if you need to pass different arguments to each of them:
     * foreach($terms as $terms) {
     *      if ($calculator->canCalculate($term, ['some1' => 'arg1'])) {
     *          $calculated[] = $calculator->calculateTerm($term, ['some1' => 'arg1']);
     *      } 
     * }
     */

    $calculated = array_merge($calculated, $result->getCalculatedTerms());
    $terms = array_column($result->getSkippedTerms(), ITermCalculationResult::SKIPPED__TERM);

    if (empty($terms)) {
        break;
    }
}