Overview

Namespaces

  • BN
    • Collections
    • Compiler
      • Grammar
      • Parser
        • Operator
        • Token
      • Postfix
        • Operands
        • Operator
        • Token
      • Scanner
        • Converter
      • Token
  • Demo
  • None

Classes

  • AggregateFunctions
  • Number
  • NumberFactory
  • OperatorsFactory

Interfaces

  • INumber
  • Overview
  • Namespace
  • Class
  • Tree
 1: <?php
 2: /**
 3:  * BN-PHP (https://bitbucket.org/zdenekdrahos/bn-php)
 4:  * @license New BSD License
 5:  * @author Zdenek Drahos
 6:  */
 7: 
 8: namespace BN;
 9: 
10: /**
11:  * Factory for creating @see Number from string, integer, float or object.
12:  * Useful for creating numbers from different types because Number
13:  * throws \InvalidArgumentException if argument in constructor is not string.
14:  * It can also create numbers for mathematical constants pi and e.
15:  */
16: class NumberFactory
17: {
18:     /** @var array */
19:     private $constants;
20: 
21:     public function __construct()
22:     {
23:         $this->loadConstants();
24:     }
25: 
26:     /**
27:      * Try to create new @see Number from an argument.
28:      * @param  string|int|float|object   $number
29:      * @return \BN\INumber
30:      * @throws \InvalidArgumentException if $number is not numeric string, int, float
31:      *  or object with __toString() method
32:      */
33:     public function createNumber($number)
34:     {
35:         if ($this->isValidNumber($number)) {
36:             return new Number((string) $number);
37:         } else {
38:             throw new \InvalidArgumentException('Argument must be numeric string, int, float, object with __toString');
39:         }
40:     }
41: 
42:     /**
43:      * Returns a new number which represents a mathematical constant π (PI).
44:      * Precision of the constant is 200 digits after decimal point.
45:      * @return \BN\Number
46:      */
47:     public function createPI()
48:     {
49:         return clone $this->constants['pi'];
50:     }
51: 
52:     /**
53:      * Returns a new number which represents a mathematical constant e (Euler's
54:      * number). Precision of the constant is 200 digits after decimal point.
55:      * @return \BN\Number
56:      */
57:     public function createEulerNumber()
58:     {
59:         return clone $this->constants['e'];
60:     }
61: 
62:     private function isValidNumber($number)
63:     {
64:         return is_string($number) || is_int($number) || is_float($number)
65:                 || $this->canBeObjectConvertedToString($number);
66:     }
67: 
68:     private function canBeObjectConvertedToString($object)
69:     {
70:         return is_object($object) && method_exists($object, '__toString');
71:     }
72: 
73:     private function loadConstants()
74:     {
75:         $this->constants = array(
76:             'pi' => new Number($this->getPI()),
77:             'e' => new Number($this->getE()),
78:         );
79:     }
80: 
81:     private function getPI()
82:     {
83:         $pi = '3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117067';
84:         $pi .= '98214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196';
85:         return $pi;
86:     }
87: 
88:     private function getE()
89:     {
90:         $e = '2.7182818284590452353602874713526624977572470936999595749669676277240766303535475945713821785251664274';
91:         $e .= '2746639193200305992181741359662904357290033429526059563073813232862794349076323382988075319525101901';
92:         return $e;
93:     }
94: }
95: 
BN-PHP - Big Number data type for PHP API documentation generated by ApiGen 2.8.0