Example #1
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 #2
0
 public function testConfigArrayEmpty()
 {
     $resetConfig = function () {
         $reflectionClass = new ReflectionClass("FinanCalc\\Utils\\Config");
         $reflectedProperty = $reflectionClass->getProperty('configArray');
         $reflectedProperty->setAccessible(true);
         $reflectedProperty->setValue(array());
     };
     $resetConfig();
     Config::setConfigField("TestField", "TestValue");
     $this->assertEquals("TestValue", Config::getConfigField("TestField"));
     $resetConfig();
     $this->assertEquals("/Calculators/Factories", Config::getConfigField("factories_relative_path"));
 }
Example #3
0
 /**
  * @param string $identifier
  * @param string|null $locale
  * @return string
  * @throws \Exception
  */
 public static function getString($identifier, $locale = null)
 {
     if (is_null($locale)) {
         $locale = Config::getConfigField("locale");
     }
     $localePath = FinanCalc::getPath() . "/locale/{$locale}.php";
     if (!file_exists($localePath)) {
         return null;
     }
     /** @noinspection PhpIncludeInspection */
     $strings = (include $localePath);
     if (!is_array($strings) || !array_key_exists($identifier, $strings)) {
         return null;
     }
     return $strings[$identifier];
 }
Example #4
0
 /**
  * @param array $inputArray
  * @return mixed
  *
  * inspired by http://stackoverflow.com/questions/9152176/convert-an-array-to-xml-or-json
  */
 public static function serializeArray(array $inputArray)
 {
     $domDocument = new \DOMDocument('1.0', 'UTF-8');
     $domDocument->formatOutput = true;
     $rootElem = $domDocument->createElement(Config::getConfigField('serializers_root_elem_name'));
     $domDocument->appendChild($rootElem);
     $funcArrayToXML = function (\DOMElement $parentNode, $inputArray) use($domDocument, &$funcArrayToXML) {
         foreach ($inputArray as $key => $value) {
             $key = preg_replace('/(^[0-9])/', '_\\1', str_replace(' ', '_', $key));
             $isValueArray = is_array($value);
             $elem = $domDocument->createElement($key, !$isValueArray ? $value : null);
             $parentNode->appendChild($elem);
             if ($isValueArray) {
                 $funcArrayToXML($elem, $value);
             }
         }
     };
     $funcArrayToXML($rootElem, $inputArray);
     return $domDocument->saveXML();
 }
Example #5
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 #6
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));
                 }
             }
         }
     }
 }