Source for file Numeral.php
Documentation is available at Numeral.php
require_once 'Text/CAPTCHA/Numeral/interfaces/NumeralInterface.php';
// {{{ Class Text_CAPTCHA_Numeral
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1998-2007 David Coallier |
// | All rights reserved. |
// +----------------------------------------------------------------------+
// | Redistribution and use in source and binary forms, with or without |
// | modification, are permitted provided that the following conditions |
// | Redistributions of source code must retain the above copyright |
// | notice, this list of conditions and the following disclaimer. |
// | Redistributions in binary form must reproduce the above copyright |
// | notice, this list of conditions and the following disclaimer in the |
// | documentation and/or other materials provided with the distribution. |
// | Neither the name of David Coallier nor the names of his contributors |
// | may be used to endorse |
// | or promote products derived from this software without specific prior|
// | written permission. |
// | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
// | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
// | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
// | FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
// | REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
// | INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
// | BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS|
// | OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
// | AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
// | LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY|
// | WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
// | POSSIBILITY OF SUCH DAMAGE. |
// +----------------------------------------------------------------------+
// | Author: David Coallier <davidc@agoraproduction.com> |
// +----------------------------------------------------------------------+
* Class used for numeral captchas
* This class is intended to be used to generate
* numeral captchas as such as:
* Give me the answer to "54 + 2" to prove that you are human.
* @author David Coallier <davidc@agoraproduction.com>
* @package Text_CAPTCHA_Numeral
* This variable holds the minimum range value
* @var integer $minValue The minimum range value
* This variable holds the maximum range value
* @var integer $maxValue The maximum value of the number range
private $maxValue = '50';
* The valid operators to use
* in the numeral captcha. We could
* use / and * but not yet.
* @var array $operators The operations for the captcha
private $operators = array ();
* This variable is basically the operation
* that we're going to be using in the
* numeral captcha we are about to generate.
* @var string $operator The operation's operator
* This is the mathematical operation
* that we are displaying to the user.
* @var string $operation The math operation
* First number of the operation
* This variable holds the first number
* of the numeral operation we are about
* @var integer $firstNumber The first number of the operation
private $firstNumber = '';
* Second Number of the operation
* This variable holds the value of the
* second variable of the operation we are
* about to generate for the captcha.
* @var integer $secondNumber The second number of the operation
private $secondNumber = '';
* The answer to the numeral operation
* @var integer $answer The mathematical operation answer value.
* A constant that indicates the complexity of mathematical operations
const TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY = 1;
* A constant that indicates the complexity of mathematical operations
const TEXT_CAPTCHA_NUMERAL_COMPLEXITY_HIGH_SCHOOL = 2;
* A constant that indicates the complexity of mathematical operations
const TEXT_CAPTCHA_NUMERAL_COMPLEXITY_UNIVERSITY = 4;
* Constructor with different levels of mathematical operations sets
* @param constant $complexityType
public function __construct($complexityType = self ::TEXT_CAPTCHA_NUMERAL_COMPLEXITY_ELEMENTARY )
switch ($complexityType) {
$this->operators = array ('+', '-', '*');
$this->operators = array ('+', '-', '*', '%', '/');
$this->operators = array ('-', '+');
$this->generateFirstNumber ();
$this->generateSecondNumber ();
$this->generateOperator ();
$this->generateOperation ();
// {{{ private function setRangeMinimum
* Set Range Minimum value
* This function give the developer the ability
* to set the range minimum value so the operations
* can be bigger, smaller, etc.
* @param integer $minValue The minimum value
private function setRangeMinimum ($minValue = '1')
$this->minValue = (int) $minValue;
// {{{ private function generateFirstNumber
* This function sets the first number
* of the operation by calling the generateNumber
* function that generates a random number.
* @see $this->firstNumber, $this->generateNumber
private function generateFirstNumber ()
$this->setFirstNumber ($this->generateNumber ());
// {{{ private function generateSecondNumber
* This function sets the second number of the
* operation by calling generateNumber()
* @see $this->secondNumber, $this->generateNumber()
private function generateSecondNumber ()
$this->setSecondNumber ($this->generateNumber ());
// {{{ private function generateOperator
* Sets the operation operator
* This function sets the operation operator by
* getting the array value of an array_rand() of
* the $this->operators() array.
* @see $this->operators, $this->operator
private function generateOperator ()
$this->operator = $this->operators[array_rand($this->operators)];
// {{{ private function setAnswer
* This function will accept the parameters which is
* basically the result of the function we have done
* and it will set $this->answer with it.
* @param integer $answerValue The answer value
private function setAnswer ($answerValue)
$this->answer = $answerValue;
// {{{ private function setFirstNumber
* This function sets the first number
* to the value passed to the function
* @param integer $value The first number value.
* @return object $this The self object
private function setFirstNumber ($value)
$this->firstNumber = (int) $value;
// {{{ private function setSecondNumber
* This function sets the second number
* with the value passed to it.
* @param integer $value The second number new value.
* @return object $this The self object
private function setSecondNumber ($value)
$this->secondNumber = (int) $value;
// {{{ private function setOperation
* This variable sets the operation variable
* by taking the firstNumber, secondNumber and operator
private function setOperation ()
// {{{ private function generateNumber
* This function takes the parameters that are in
* the $this->maxValue and $this->minValue and get
* the random number from them using mt_rand()
* @return integer Random value between minValue and maxValue
private function generateNumber ()
return mt_rand($this->minValue, $this->maxValue);
// {{{ private function doAdd
* This function will add the firstNumber and the
* secondNumber value and then call setAnswer to
* @see $this->firstNumber, $this->secondNumber, $this->setAnswer()
$this->setAnswer ($answer);
// {{{ private function doMultiplication
* This method will multiply two numbers
* @see $this->firstNumber, $this->secondNumber, $this->setAnswer
private function doMultiplication ()
// {{{ private function doDivision
* This function executes a division based on the two
* @param integer $firstNumber The first number of the operation.
* This is by default set to null.
* @param integer $secondNumber The second number of the operation
* This is by default set to null.
private function doDivision ($firstNumber = null , $secondNumber = null )
if ($secondNumber == 0 ) {
$this->doDivision ($firstNumber, $secondNumber);
if ($firstNumber % $secondNumber != 0 ) {
$this->doDivision ($firstNumber, $secondNumber);
$this->setFirstNumber ($firstNumber)
->setSecondNumber ($secondNumber)
// {{{ private function doModulus
* This method will do a modulus operation between two numbers
* @see $this->firstNumber, $this->secondNumber, $this->setAnswer()
private function doModulus ()
// {{{ private function doSubstract
* Does a substract on the values
* This function executes a substraction on the firstNumber
* and the secondNumber to then call $this->setAnswer to set
* If the firstnumber value is smaller than the secondnumber value
* then we regenerate the first number and regenerate the operation.
* @see $this->firstNumber, $this->secondNumber, $this->setAnswer()
private function doSubstract ()
* Check if firstNumber is smaller than secondNumber
$this->setFirstNumber ($second)
->setSecondNumber ($first)
$this->setAnswer ($answer);
// {{{ private function generateOperation
* This function will call the setOperation() function
* to set the operation string that will be called
* to display the operation, and call the function necessary
* depending on which operation is set by this->operator.
* @see $this->setOperation(), $this->operator
private function generateOperation ()
switch ($this->operator) {
$this->doMultiplication ();
// {{{ public function getOperation
* This function will get the operation
* string from $this->operation
* @return string The operation String
// {{{ public function getAnswer
* This function will retrieve the answer
* value from this->answer and return it so
* we can then display it to the user.
* @return string The operation answer value.
// {{{ public function getFirstNumber
* This function will get the first number
* value from $this->firstNumber
* @return integer $this->firstNumber The firstNumber
return $this->firstNumber;
// {{{ public function getSecondNumber
* Get the second number value
* This function will return the second number value
* @return integer $this->secondNumber The second number
return $this->secondNumber;
Documentation generated on Mon, 11 Mar 2019 15:00:31 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|