Example #1
0
<?php

/*
This is a test for formula framework prototype.
*/
$formulaPath = './';
$operandsPath = $formulaPath . 'Operands/';
$operatorsPath = $formulaPath . 'Operators/';
require $formulaPath . 'Atom.php';
require $formulaPath . 'Operand.php';
require $formulaPath . 'Operator.php';
require $formulaPath . 'FormulaUtils.php';
$directory = new DirectoryIterator($operandsPath);
foreach ($directory as $fileInfo) {
    if ($fileInfo->isFile()) {
        require $operandsPath . $fileInfo->getFilename();
    }
}
$directory = new DirectoryIterator($operatorsPath);
foreach ($directory as $fileInfo) {
    if ($fileInfo->isFile()) {
        require $operatorsPath . $fileInfo->getFilename();
    }
}
$object = FormulaUtils::parse('not (1 = 2)');
// There is the formula to parse.
print_r($object);
echo $object->getValue();
Example #2
0
 /**
  * Converts an array of instances to a formula object.
  *
  * @param array $aFormula
  * @return Atom
  * @throws EyeUnexpectedValueException Wrong formula sintax.
  */
 public static function sortByPriority($aFormula)
 {
     $aFormula = FormulaUtils::sortByPharentesis($aFormula);
     $aFormulaSize = count($aFormula);
     foreach (FormulaUtils::$Operators as $operatorClass) {
         $i = 0;
         while ($i < $aFormulaSize) {
             if (get_class($aFormula[$i]) == $operatorClass) {
                 if ($aFormula[$i]->isUnitary()) {
                     // If is unitary,
                     if ($aFormulaSize <= $i + 1) {
                         // there should be another operand at right of the current operator.
                         throw new EyeUnexpectedValueException($operatorClass . ' has not any argument.');
                     }
                     $aFormula[$i]->addOperand($aFormula[$i + 1]);
                     array_splice($aFormula, $i + 1, 1);
                     // Deleting the right operand
                     --$aFormulaSize;
                     ++$i;
                 } else {
                     // Else, if can have multiple operands,
                     if ($i == 0 || $aFormulaSize <= $i + 1) {
                         // there should be two operands. One at left and one at right of the current operator.
                         throw new EyeUnexpectedValueException($operatorClass . ' has not any argument.');
                     }
                     $aFormula[$i]->addOperand($aFormula[$i - 1]);
                     $aFormula[$i]->addOperand($aFormula[$i + 1]);
                     array_splice($aFormula, $i - 1, 1);
                     // Deleting the left operand
                     array_splice($aFormula, $i, 1);
                     // Deleting the right operand
                     $aFormulaSize -= 2;
                 }
             } else {
                 ++$i;
             }
         }
     }
     return $aFormula[0];
 }