<?php
/**
* Created by PhpStorm.
* User: yanickwitschi
* Date: 15/12/14
* Time: 11:52
*/
namespace Bekanntmacher\BoxmakerBundle\Calculation;
class Surface
{
/**
* Formula width
* @var string
*/
protected $strFormulaWidth;
/**
* Formula length
* @var string
*/
protected $strFormulaLength;
/**
* Formula tokens
* @var array
*/
protected $arrData;
/**
* @param $strFormulaLength
* @param $strFormulaLength
* @param $strFormulaWidth
*/
function __construct($strFormulaLength, $strFormulaWidth, array $arrData)
{
$this->strFormulaLength = $strFormulaLength;
$this->strFormulaWidth = $strFormulaWidth;
$this->arrData = $arrData;
}
/**
* Get the "m2 per piece" (based on length and width)
*
* @return Number
* @throws \RuntimeException
*/
public function getM2PerPiece()
{
$width = $this->getFormulaResult(
$this->strFormulaWidth,
$this->arrData
);
$length = $this->getFormulaResult(
$this->strFormulaLength,
$this->arrData
);
return (float) ( $width * $length );
}
/**
* Get the "Nutzen" (based on length and width)
*
* @return Number
* @throws \RuntimeException
*/
public function getNutzen()
{
$width = $this->getFormulaResult(
$this->strFormulaWidth,
$this->arrData
);
$length = $this->getFormulaResult(
$this->strFormulaLength,
$this->arrData
);
return floor($width) * floor($length);
}
/**
* Execute formula code with tokens and return the result
*
* @param string $code
* @param array $tokens
* @return float
*/
private function getFormulaResult($code, $arrData)
{
$code = 'return ' . \StringUtil::parseSimpleTokens($code, $arrData) . ';';
return (float) eval($code);
}
}