<?php
namespace App\Libraries\formula;


use App\Libraries\formula\Double;
use App\Libraries\formula\Integer;

use App\Libraries\formula\Percent;
use App\Libraries\formula\Variable;

class ExpressionFactory
{
    public static function factory(string $expression, $variables = [])
    {
        if ( strpos( $expression, '.' ) !== false || strpos( $expression, ',' ) !== false ) {
            return new Double( $expression );
        } else if ( strpos( $expression, '%' ) !== false ) {
            return new Percent( $expression );
        } else if ( ( int ) $expression > 0 ) {
            return new Integer( $expression );
        } else {
            $variable = new Variable( $expression );

            if ( array_key_exists( $expression, $variables ) ) {
                $variable->setValue( $variables[ $expression ] );
            }

            return $variable;
        }
    }
}