diff --git a/CaptchaAction.php b/CaptchaAction.php index ab2c8f2..4bbd6f7 100644 --- a/CaptchaAction.php +++ b/CaptchaAction.php @@ -2,32 +2,57 @@ namespace mdm\captcha; -use \Yii; -use yii\helpers\Url; +use Yii; use yii\web\Response; +use yii\helpers\Url; /** - * Description of CaptchaAction 2520 + * Description of CaptchaAction * - * @author MDMunir + * @author Misbahul D Munir + * @since 1.0 */ class CaptchaAction extends \yii\captcha\CaptchaAction { + const JPEG_FORMAT = 'jpeg'; + const PNG_FORMAT = 'png'; + + /** + * Avaliable value are 'jpeg' or 'png' + * @var string + */ + public $imageFormat = self::JPEG_FORMAT; + + /** + * Dificully level + * @var int + */ public $level = 1; + + /** + * Font size. + * @var int + */ public $size = 14; /** - * - * @var Formula + * Allow decimal + * @var boolean */ - private $_formula; + public $allowDecimal = false; + + /** + * Registered equation class + * @var array + */ + public static $classes; /** * @inheritdoc */ public function init() { - $this->_formula = new Formula($this->level); + } /** @@ -38,77 +63,141 @@ public function run() if (Yii::$app->request->getQueryParam(self::REFRESH_GET_VAR) !== null) { // AJAX request for regenerating code $code = $this->getVerifyCode(true); - - return json_encode([ + Yii::$app->response->format = Response::FORMAT_JSON; + return [ 'hash1' => $this->generateValidationHash($code), 'hash2' => $this->generateValidationHash(strtolower($code)), // we add a random 'v' parameter so that FireFox can refresh the image // when src attribute of image tag is changed 'url' => Url::to([$this->id, 'v' => uniqid()]), - ]); + ]; } else { $this->setHttpHeaders(); Yii::$app->response->format = Response::FORMAT_RAW; - return $this->renderImage($this->_formula->getExpresion($this->getFormulaCode())); + return $this->renderImage($this->getVerifyCode(false, true)); } } /** - * Gets the verification code. - * @param boolean $regenerate whether the verification code should be regenerated. - * @return string the verification code. + * @inheritdoc */ - public function getVerifyCode($regenerate = false) + public function getVerifyCode($regenerate = false, $code = false) { $session = Yii::$app->getSession(); $session->open(); $name = $this->getSessionKey(); if ($session[$name] === null || $regenerate) { - $code = $session[$name . 'code'] = $this->generateVerifyCode(); - $session[$name] = $this->_formula->getValue($code); + $session[$name . 'code'] = $this->generateVerifyCode(); + $session[$name] = $this->getValue($session[$name . 'code']); $session[$name . 'count'] = 1; } - return $session[$name]; + + return $code ? $session[$name . 'code'] : $session[$name]; } - protected function getFormulaCode() + /** + * @inheritdoc + */ + public function validate($input) { + $code = $this->getVerifyCode(false, true); + $value = $this->getValue($code); + if ($this->allowDecimal) { + $valid = abs(round($input, 2) - round($value, 2)) <= 0.02; + } else { + $valid = $input == $value; + } + $session = Yii::$app->getSession(); $session->open(); - $name = $this->getSessionKey(); - if ($session[$name] === null) { - $code = $session[$name . 'code'] = $this->generateVerifyCode(); - $session[$name] = $this->_formula->getValue($code); - $session[$name . 'count'] = 1; + $name = $this->getSessionKey() . 'count'; + $session[$name] = $session[$name] + 1; + if ($valid || $session[$name] > $this->testLimit && $this->testLimit > 0) { + $this->getVerifyCode(true); } - return $session[$name . 'code']; + + return $valid; } + /** + * @inheritdoc + */ protected function generateVerifyCode() { - if ($this->fixedVerifyCode !== null) { - return $this->fixedVerifyCode; - } mt_srand(time()); - $code = []; - for ($i = 0; $i <= 5; $i++) { + $code = [mt_rand(0, 100)]; + for ($i = 1; $i <= 5; $i++) { $code[$i] = mt_rand(0, 10); } - $code[6]= mt_rand(0, 100); + return $code; } + /** + * @inheritdoc + */ protected function renderImage($code) { require __DIR__ . '/mathpublisher.php'; - $formula = new \expression_math(tableau_expression(trim($code))); + $formula = new \expression_math(tableau_expression(trim($this->getExpresion($code)))); $formula->dessine($this->size); ob_start(); - imagepng($formula->image); + switch ($this->imageFormat) { + case self::JPEG_FORMAT: + imagejpeg($formula->image); + break; + case self::PNG_FORMAT: + imagepng($formula->image); + break; + } imagedestroy($formula->image); return ob_get_clean(); } -} \ No newline at end of file + + /** + * Sets the HTTP headers needed by image response. + */ + protected function setHttpHeaders() + { + Yii::$app->getResponse()->getHeaders() + ->set('Pragma', 'public') + ->set('Expires', '0') + ->set('Cache-Control', 'must-revalidate, post-check=0, pre-check=0') + ->set('Content-Transfer-Encoding', 'binary') + ->set('Content-type', "image/{$this->imageFormat}"); + } + + /** + * Get expresion formula . + * @param array $code + * @return string + */ + protected function getExpresion($code) + { + if ($this->fixedVerifyCode !== null) { + return $this->fixedVerifyCode; + } + $class = static::$classes[$this->level][$code[0] % count(static::$classes[$this->level])]; + return $class::getExpresion($code, $this->allowDecimal); + } + + /** + * Get value of formula + * @param array $code + * @return int|float + */ + protected function getValue($code) + { + if ($this->fixedVerifyCode !== null) { + return $this->fixedVerifyCode; + } + $class = static::$classes[$this->level][$code[0] % count(static::$classes[$this->level])]; + return $class::getValue($code, $this->allowDecimal); + } +} + +// +CaptchaAction::$classes = require(__DIR__ . '/equations/classes.php'); diff --git a/Formula.php b/Formula.php deleted file mode 100644 index 74c1378..0000000 --- a/Formula.php +++ /dev/null @@ -1,37 +0,0 @@ - - * @since 1.0 - */ -class Formula extends \yii\base\Object -{ - public static $classes; - public $level; - - public function __construct($level, $config = array()) - { - $this->level = $level; - parent::__construct($config); - } - - public function getExpresion($code) - { - $idx = $code[6] % count(static::$classes[$this->level]); - $class = static::$classes[$this->level][$idx]; - return $class::getExpresion($code); - } - - public function getValue($code) - { - $idx = $code[6] % count(static::$classes[$this->level]); - $class = static::$classes[$this->level][$idx]; - return round($class::getValue($code)); - } -} -// avaliable classes -Formula::$classes = require(__DIR__ . DIRECTORY_SEPARATOR . 'equations' . DIRECTORY_SEPARATOR . 'classes.php'); diff --git a/README.md b/README.md index e521b33..bf739a3 100644 --- a/README.md +++ b/README.md @@ -9,19 +9,13 @@ The preferred way to install this extension is through [composer](http://getcomp Either run ``` -php composer.phar require --prefer-dist mdmsoft/yii2-captcha "1.0.0" +php composer.phar require --prefer-dist mdmsoft/yii2-captcha "~1.0" ``` or add ``` -"mdmsoft/yii2-captcha": "1.0.0" -``` - -for dev, using - -``` -"mdmsoft/yii2-captcha": "1.0.*@dev" +"mdmsoft/yii2-captcha": "~1.0" ``` to the require section of your `composer.json` file. @@ -43,5 +37,15 @@ public function actions() ], ]; } +``` + +In view +```php +field($model, 'verifyCode')->widget(Captcha::className(), [ + 'template' => '
{image}
{input}
', +]) +?> + ``` ![screenshot](https://lh3.googleusercontent.com/-ACmPR-FSnfE/U4Rz2f3tqqI/AAAAAAAAAgw/D6xuLeobLU4/w804-h496-no/Screenshot+from+2014-05-27+16%253A47%253A07.png) diff --git a/composer.json b/composer.json index e2b1ceb..8ac6887 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ }, "extra": { "branch-alias": { - "dev-master": "1.0.x-dev" + "dev-master": "1.x-dev" } } } diff --git a/equations/AddSub.php b/equations/AddSub.php index 728c8ce..db47dc6 100644 --- a/equations/AddSub.php +++ b/equations/AddSub.php @@ -9,42 +9,15 @@ * @author Misbahul D Munir * @since 1.0 */ -class AddSub implements EquationInterface +class AddSub { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[1] + $code[2]; - $b = $code[3] + 11; - $c = $code[4]; - break; + $a = $code[1] + $code[2] + $code[3] * $code[5]; + $b = $code[3] + $code[4] + 12; + $c = $code[2] + $code[4] + $code[5] + 1; - case 1: - $a = $code[1] * $code[2]; - $b = $code[3] + 11; - $c = $code[4]; - break; - - case 2: - $a = $code[1] + $code[2]; - $b = $code[3] + 11; - $c = $code[3] + $code[4]; - break; - - case 3: - $a = $code[1] * $code[2]; - $b = $code[3] + 11; - $c = $code[3] + $code[4]; - break; - - default: - $a = 2 * ($code[1] + $code[2]); - $b = $code[3] + 11; - $c = $code[3] + $code[4]; - break; - } return [$a, $b, $c]; } @@ -59,4 +32,4 @@ public static function getValue($code) list($a, $b, $c) = static::format($code); return $a + $b - $c; } -} \ No newline at end of file +} diff --git a/equations/Division.php b/equations/Division.php index 148ef74..ba59b55 100644 --- a/equations/Division.php +++ b/equations/Division.php @@ -8,45 +8,28 @@ * @author Misbahul D Munir * @since 1.0 */ -class Division implements EquationInterface +class Division { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3] + 11; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + 11; - break; - default : - $a = $code[0] - $code[1] + $code[4] + 11; - $b = $code[2] - $code[3] + 11; - break; - } - return [$a * $b, $b]; + $a = $code[1] + $code[2] + $code[3] * $code[5]; + $b = $code[3] + $code[4] + 12; + $c = $code[2] + $code[4] + $code[5] + 1; + + return [$a + $b - $c, $c]; } - public static function getExpresion($code) + public static function getExpresion($code, $decimal = false) { list($a, $b) = static::format($code); + $a = $decimal ? $a : $a * $b; return "{$a} / {$b}"; } - public static function getValue($code) + public static function getValue($code, $decimal = false) { list($a, $b) = static::format($code); - return $a / $b; + return $decimal ? 1.0 * $a / $b : $a; } -} \ No newline at end of file +} diff --git a/equations/EquationInterface.php b/equations/EquationInterface.php deleted file mode 100644 index f894ba0..0000000 --- a/equations/EquationInterface.php +++ /dev/null @@ -1,31 +0,0 @@ - - * @since 1.0 - */ -interface EquationInterface -{ - /** - * Return mathematic expresion to be rendered - * @param string $code - * @return string - */ - public static function getExpresion($code); - - /** - * Value of expresion - * @param string $code - * @return string - */ - public static function getValue($code); -} \ No newline at end of file diff --git a/equations/Fraction.php b/equations/Fraction.php index 6b6e624..8ffd418 100644 --- a/equations/Fraction.php +++ b/equations/Fraction.php @@ -4,58 +4,50 @@ /** * Polynomial Orde 2 - * - * Limit [a^2 + b*c + d]/[e + f] + * + * (a - b)(a + 2 + c) / (a + 2 + c) + * [a^2 + (c-b+2)*a - b*(c+2)] / [a + 2 + c] + * a -b * * @author Misbahul D Munir * @since 1.0 */ -class Fraction implements EquationInterface +class Fraction { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3]; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3]; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] * $code[4] - $code[3]; - break; - } - return [$a + 11, $b]; + $a = $code[1] + $code[2] + 1; + $b = $code[3] + $code[4] + 1; + $c = $code[4] + $code[5] + 1; + + return [$a, $b, $c]; } - public static function getExpresion($code) + public static function getExpresion($code, $decimal = false) { - list($a, $b) = static::format($code); - if ($b == 0) { - $b++; + list($a, $b, $c) = static::format($code); + $a += $b; + + $c2 = $c + 2; + $midle = $c2 > $b ? '+ ' . ($c2 - $b) . "*{$a}" : ($c2 < $b ? '- ' . ($b - $c2) . "*{$a}" : ''); + $bc = $b * $c2; + + if ($decimal) { + $c += 3; } - $sign = $b > 0 ? '+' : '-'; - $ab = 2 * abs($a * $b); - $bb = $b * $b; - $b = abs($b); - return "{{$a}^2 {$sign} {$ab} + {$bb}} / {{$a} {$sign} {$b}}"; + $a2 = $a + 2; + return "{{$a}^2 {$midle} - {$bc}} / {{$a2} + {$c}}"; } - public static function getValue($code) + public static function getValue($code, $decimal = false) { - list($a, $b) = static::format($code); - return $a + $b; + list($a, $b, $c) = static::format($code); + if ($decimal) { + $a += $b; + return 1.0 * ($a - $b) * ($a + 2 + $c) / ($a + $c + 5); + } else { + return $a; + } } -} \ No newline at end of file +} diff --git a/equations/Integrate1.php b/equations/Integrate1.php index 20014ae..3dfa4bb 100644 --- a/equations/Integrate1.php +++ b/equations/Integrate1.php @@ -3,51 +3,41 @@ namespace mdm\captcha\equations; /** - * Multiply + * Integral 1 + * + * Integrate[x = 0 to a, x^2 + 2bx + c] + * + * = x^3/3 + bx^2 + cx , x = 0 to a + * + * = [a^3]/3 + b*[a^2] + c*a * * @author Misbahul D Munir * @since 1.0 */ -class Integrate1 implements EquationInterface +class Integrate1 { public static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3] + 11; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + 11; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + $code[4] + 11; - break; - } - return [3 * $a + $b, $b, $code[0] - $code[2]]; + $a = $code[1] + 1; + $b = $code[2] + 1; + $c = $code[4] + 1; + + return [$a, $b, $c]; } - public static function getExpresion($code) + public static function getExpresion($code, $decimal = false) { list($a, $b, $c) = static::format($code); - $c = $c == 0 ? '' : ($c < 0 ? ' - ' . abs($c) : ' + ' . $c); - return "int{{$b}}{{$a}}{(x^2 - 2x{$c}) dx}"; + $b *= 2; + $f = $decimal ? '' : '3'; + return "int{0}{{$a}}{({$f}x^2 + {$b}x + {$c}) dx}"; } - public static function getValue($code) + public static function getValue($code, $decimal = false) { list($a, $b, $c) = static::format($code); - return ($a - $b) * ($a * $a + $a * $b + $b * $b) / 3 - ($a * $a - $b * $b) + $c * ($a - $b); + $f = $decimal ? 1.0 / 3 : 1; + return ($a * $a * $a) * $f + $b * $a * $a + $c * $a; } -} \ No newline at end of file +} diff --git a/equations/LimitFnt.php b/equations/LimitFnt.php index 0b62e17..a97b673 100644 --- a/equations/LimitFnt.php +++ b/equations/LimitFnt.php @@ -3,53 +3,36 @@ namespace mdm\captcha\equations; /** - * Multiply + * Limit + * + * (a - b)(a + c) / (a - b) + * [a^2 + (c-b)*a - b*c] / [a - b] + * a + b + * + * Limit[x -> a, (x^2 + (c-b)x - b*c)/(x - b)] * * @author Misbahul D Munir * @since 1.0 */ -class LimitFnt implements EquationInterface +class LimitFnt { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3] + 11; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + 11; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + $code[4] + 11; - break; - } - return [$a, $b, $code[0] == '0' ? 1 : $code[0]]; + $a = $code[1] + $code[2] + 1; + $b = $code[3] + $code[4] + 1; + $c = $code[4] + $code[5] + 1; + + return [$a, $b, $c]; } public static function getExpresion($code) { list($a, $b, $c) = static::format($code); - $sign = $c > $b ? ' + ' : ($c < $b ? ' - ' : ''); - if ($sign != '') { - $bc1 = abs($c - $b); - $bc1 = $bc1 == 1 ? 'x' : $bc1 . 'x'; - } else { - $bc1 = ''; - } - $bc2 = $b * $c; - return "lim{x right {$a}}{{x^2{$sign}{$bc1} - {$bc2}}/{x - {$b}}}"; + $midle = $c > $b ? '+ ' . ($c - $b) . 'x' : ($c < $b ? '- ' . ($b - $c) . 'x' : ''); + $bc = $b * $c; + + return "lim{x right {$a}}{{x^2 {$midle} - {$bc}}/{x - {$b}}}"; } public static function getValue($code) @@ -57,4 +40,4 @@ public static function getValue($code) list($a,, $c) = static::format($code); return $a + $c; } -} \ No newline at end of file +} diff --git a/equations/LimitIfnt1.php b/equations/LimitIfnt1.php index ef72934..47e8238 100644 --- a/equations/LimitIfnt1.php +++ b/equations/LimitIfnt1.php @@ -3,52 +3,44 @@ namespace mdm\captcha\equations; /** - * Multiply + * Limit Infinity 1 + * + * Limit[x -> ~,V(x^2 + ax + c) - V(x^2 + bx + d)] + * + * (a - b)/2 * * @author Misbahul D Munir * @since 1.0 */ -class LimitIfnt1 implements EquationInterface +class LimitIfnt1 { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3] + 11; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + 11; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + $code[4] + 11; - break; - } - return [2 * $a + $b, $b, $code[0] - $code[2]+11, $code[1] - $code[3]+11]; + $a = $code[1] + $code[2] + 10; + $b = $code[3] + $code[4] - 10; + $c = $code[4] + $code[5] + 1; + $d = $code[1] + $code[5] + 1; + + return [$a, $b, $c, $d]; } - public static function getExpresion($code) + public static function getExpresion($code, $decimal = false) { list($a, $b, $c, $d) = static::format($code); - $sg1 = $c > 10? '+' : '-'; - $sg2 = $d > 10? '+' : '-'; - return "lim{x right infty}{sqrt{x^2 + {$a}x {$sg1} {$c}}-sqrt{x^2 + {$b}x {$sg2} {$d}}}"; + if (!$decimal) { + $a *= 2; + } + $a += $b; + $sb = $b > 0 ? "+ {$b}x" : ($b < 0 ? '- ' . abs($b) . 'x' : ''); + $sg1 = $c > 10 ? '+' : '-'; + $sg2 = $d > 10 ? '+' : '-'; + return "lim{x right infty}{sqrt{x^2 + {$a}x {$sg1} {$c}}-sqrt{x^2 {$sb} {$sg2} {$d}}}"; } - public static function getValue($code) + public static function getValue($code, $decimal = false) { - list($a, $b,, ) = static::format($code); - return ($a - $b) / 2; + list($a,,, ) = static::format($code); + return $decimal ? $a / 2 : $a; } -} \ No newline at end of file +} diff --git a/equations/LimitIfnt2.php b/equations/LimitIfnt2.php index 45cbd0c..10e7fda 100644 --- a/equations/LimitIfnt2.php +++ b/equations/LimitIfnt2.php @@ -3,56 +3,43 @@ namespace mdm\captcha\equations; /** - * Multiply + * Limit Infinity 2 + * + * Limit[x -> ~,V(ax^2 + cx + c) / V(x^2 + dx + d)] + * + * Va * * @author Misbahul D Munir * @since 1.0 */ -class LimitIfnt2 implements EquationInterface +class LimitIfnt2 { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3] + 11; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + 11; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + $code[4] + 11; - break; - } - return [$a, $b, $code[0] - $code[2] + 11, $code[1] - $code[3] + 11]; + $a = $code[1] + $code[2] + 1; + $b = $code[2] + $code[3] + $code[4] + 1; + $c = $code[1] + $code[4] + $code[5] + 1; + $d = $code[1] + $code[5] + 5; + + return [$a, $b, $c, $d]; } - public static function getExpresion($code) + public static function getExpresion($code, $decimal = false) { list($a, $b, $c, $d) = static::format($code); - $sg1 = $c > 10 ? '+' : '-'; - $sg2 = $d > 10 ? '+' : '-'; - $a1 = $b * $b; - if ($a1 == 0) { - $a1 = 4; + if (!$decimal) { + $a = $a * $a; } - return "lim{x right infty}{sqrt{{$a1}x^2 {$sg2} {$a}x {$sg1} {$c}}}/{sqrt{x^2 {$sg1} {$b}x {$sg2} {$d}}}"; + $midle1 = (($b > 15) ? '+' : '-') . " {$b}x + " . ($d - 4); + $midle2 = (($c > 15) ? '+' : '-') . " {$c}x - " . ($d + 4); + + return "lim{x right infty}{sqrt{{$a}x^2 {$midle1}}}/{sqrt{x^2 {$midle2}}}"; } - public static function getValue($code) + public static function getValue($code, $decimal = false) { - list(, $b,, ) = static::format($code); - return $b == 0 ? 2 : $b; + list($a,,, ) = static::format($code); + return $decimal ? sqrt($a) : $a; } -} \ No newline at end of file +} diff --git a/equations/Multiply.php b/equations/Multiply.php index 23678b5..090436c 100644 --- a/equations/Multiply.php +++ b/equations/Multiply.php @@ -5,37 +5,21 @@ /** * Multiply * + * $a * $b + * * @author Misbahul D Munir * @since 1.0 */ -class Multiply implements EquationInterface +class Multiply { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3] + 11; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + 11; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3] + $code[4] + 11; - break; - } - return [$a, $b]; + $a = $code[1] + $code[2] + $code[3] * $code[5]; + $b = $code[3] + $code[4] + 12; + $c = $code[2] + $code[4] + $code[5] + 1; + + return [$a + $b - $c, $c]; } public static function getExpresion($code) diff --git a/equations/Polynom2.php b/equations/Polynom2.php index b31f1ee..5fad345 100644 --- a/equations/Polynom2.php +++ b/equations/Polynom2.php @@ -5,51 +5,33 @@ /** * Polynomial Orde 2 * - * Limit a^2 + b*c - d + * Limit a^2 + b*c - (b-2) * * @author Misbahul D Munir * @since 1.0 */ -class Polynom2 implements EquationInterface +class Polynom2 { protected static function format($code) { - switch ($code[5] % 5) { - case 0: - $a = $code[0] + $code[1]; - $b = $code[2] + $code[3]; - break; - case 1: - $a = $code[0] - $code[1] + 11; - $b = $code[2] + $code[3]; - break; - case 2: - $a = $code[0] + $code[1]; - $b = $code[2] - $code[3]; - break; - case 3: - $a = $code[0] - $code[1] + 11; - $b = $code[2] - $code[3]; - break; - default : - $a = $code[0] - $code[1] + 11; - $b = $code[2] * $code[4] - $code[3]; - break; - } - return [$a, $b + 11, $code[4] % 3 + 1]; + $a = $code[1] + $code[2] + 1; + $b = $code[3] + $code[4] + 3; + $c = $code[3] + $code[4] + $code[5] + 1; + + return [$a, $b, $c]; } public static function getExpresion($code) { list($a, $b, $c) = static::format($code); - $sb = $c * $b; - return "{$a}^2 + {$sb} - {$b}"; + $b2 = $b - 2; + return "{$a}^2 + {$b}*{$c} - {$b2}"; } public static function getValue($code) { list($a, $b, $c) = static::format($code); - return $a * $a + $c * $b - $b; + return $a * $a + $b * $c - ($b - 2); } -} \ No newline at end of file +}