Example #1
0
 /**
  * @param $checkedVariable
  * @return bool
  */
 public static function checkIfNotNegativeNumberOrThrowAnException($checkedVariable)
 {
     if (Helpers::checkIfNotNegativeNumber($checkedVariable)) {
         return true;
     }
     throw new InvalidArgumentException(ErrorMessages::getMustNotBeNegativeNumberMessage($checkedVariable));
 }
Example #2
0
 /**
  * @param array $propResultArray
  * @return array
  */
 public final function getResultAsArray(array $propResultArray = null)
 {
     if ($propResultArray === null) {
         if ($this->propResultArray !== null && is_array($this->propResultArray)) {
             $propResultArray = $this->propResultArray;
         } else {
             error_log(ErrorMessages::getPropresultarrayNotSuppliedMessage());
             return [];
         }
     }
     $processArray = function ($inputArray) use(&$processArray) {
         $processedArray = array();
         foreach ($inputArray as $key => $prop) {
             if (is_string($prop)) {
                 $propGetter = "get" . ucfirst($prop);
                 if (method_exists($this, $propGetter)) {
                     $processedArray[is_string($key) ? $key : $prop] = call_user_func(array($this, $propGetter));
                 } else {
                     error_log(ErrorMessages::getMethodDoesNotExistMessage($propGetter, get_class($this)));
                 }
             }
             if (is_array($prop)) {
                 $processedArray[$key] = $processArray($prop);
             }
         }
         return $processedArray;
     };
     return $processArray($propResultArray);
 }
Example #3
0
 /**
  * Tries to set the value  of this enum
  *
  * @param int $value
  * @throws Exception If value is not part of this enum
  */
 public function setValue($value)
 {
     if ($this->isValidEnumValue($value)) {
         $this->value = $value;
     } else {
         throw new Exception(ErrorMessages::getInvalidTypeMessage());
     }
 }
 /**
  * @throws Exception
  */
 public final function __construct()
 {
     if (!is_string(static::MANUFACTURED_CLASS_NAME)) {
         throw new Exception(ErrorMessages::getMustDefineManufacturedClassNameMessage());
     }
     if (!class_exists(static::MANUFACTURED_CLASS_NAME)) {
         throw new Exception(ErrorMessages::getClassNotDefinedMessage(static::MANUFACTURED_CLASS_NAME));
     }
 }
Example #5
0
 /**
  * @param string $key
  * @return mixed
  * @throws Exception
  */
 public static function getConfigField($key)
 {
     if (empty(static::$configArray)) {
         Config::init();
     }
     $configField = static::$configArray[$key];
     if ($configField === null) {
         throw new Exception(ErrorMessages::getConfigFieldNotFoundMessage($key));
     }
     return $configField;
 }
Example #6
0
 public function testDayCountConventionInvalid()
 {
     $this->setExpectedException('Exception', ErrorMessages::getDayCountConventionNotDefinedMessage());
     $availableDayCountConventions = Config::getConfigField('available_day_count_conventions');
     $dayCountConvention = Config::getConfigField('day_count_convention');
     Config::setConfigField('available_day_count_conventions', ['faulty']);
     Config::setConfigField('day_count_convention', 'faulty');
     TimeUtils::getCurrentDayCountConvention();
     Config::setConfigField('available_day_count_conventions', $availableDayCountConventions);
     Config::setConfigField('day_count_convention', $dayCountConvention);
 }
Example #7
0
 public function testCheckIfNotNegativeNumberException()
 {
     $this->setExpectedException('InvalidArgumentException', ErrorMessages::getMustNotBeNegativeNumberMessage("-6"));
     Helpers::checkIfNotNegativeNumberOrThrowAnException(-6);
 }
Example #8
0
 public function testGetNonExistentConfigValue()
 {
     $this->setExpectedException("Exception", ErrorMessages::getConfigFieldNotFoundMessage("NonExistent"));
     Config::setConfigField("NonExistent", null);
     Config::getConfigField("NonExistent");
 }
Example #9
0
 /**
  * @return mixed
  * @throws Exception
  */
 public static function getCurrentDayCountConvention()
 {
     $dayCountConventionIdentifier = Config::getConfigField('day_count_convention');
     $availableDayCountConventions = Config::getConfigField('available_day_count_conventions');
     if (is_array($availableDayCountConventions) && array_key_exists($dayCountConventionIdentifier, $availableDayCountConventions)) {
         $dayCountConvention = $availableDayCountConventions[$dayCountConventionIdentifier];
         if (self::isDayCountConventionValid($dayCountConvention)) {
             $dayCountConvention['days_in_a_month'] = MathFuncs::div($dayCountConvention['days_in_a_year'], 12);
             return $dayCountConvention;
         }
     }
     throw new Exception(ErrorMessages::getDayCountConventionNotDefinedMessage());
 }
Example #10
0
 /**
  * @param DateTime $startDate
  * @param DateTime $endDate
  */
 private function checkStartEndDateAndSetInterval(DateTime $startDate, DateTime $endDate)
 {
     if ($startDate < $endDate) {
         $this->newDateIntervalDifference($startDate, $endDate);
     } else {
         throw new InvalidArgumentException(ErrorMessages::getStartDateMustBeBeforeEndDateMessage());
     }
 }
Example #11
0
 /**
  *
  * PRIVATE functions
  *
  */
 private function populateFactoryClassesArray()
 {
     $factoryFiles = glob(FinanCalc::getPath() . Config::getConfigField('factories_relative_path') . '/*.php');
     $factoriesNamespace = Config::getConfigField('factories_namespace');
     foreach ($factoryFiles as $factoryFile) {
         $factoryFileContents = file_get_contents($factoryFile);
         $fileTokens = token_get_all($factoryFileContents);
         $numTokens = count($fileTokens);
         for ($i = 2; $i < $numTokens; $i++) {
             if ($fileTokens[$i - 2][0] == T_CLASS) {
                 $factoryClassName = $fileTokens[$i][1];
                 try {
                     /** @noinspection PhpIncludeInspection */
                     require_once $factoryFile;
                     $factoryClassReflector = new ReflectionClass($factoriesNamespace . '\\' . $factoryClassName);
                 } catch (ReflectionException $e) {
                     error_log(ErrorMessages::getFactoryClassExpectedInNamespaceMessage($factoryClassName, $factoriesNamespace));
                     continue;
                 }
                 $factoryAbstractClass = 'FinanCalc\\Interfaces\\Calculator\\CalculatorFactoryAbstract';
                 if ($factoryClassReflector->isSubclassOf($factoryAbstractClass)) {
                     $this->factoryClasses[$factoryClassName] = $factoryClassReflector->newInstance();
                     break;
                 } else {
                     error_log(ErrorMessages::getFactoryClassExpectedAncestorMessage($factoryClassName, $factoryAbstractClass));
                 }
             }
         }
     }
 }