Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Financial;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\DateTimeExcel;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Exception;
   7  use PhpOffice\PhpSpreadsheet\Calculation\Financial\Constants as FinancialConstants;
   8  use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
   9  
  10  class FinancialValidations
  11  {
  12      /**
  13       * @param mixed $date
  14       */
  15      public static function validateDate($date): float
  16      {
  17          return DateTimeExcel\Helpers::getDateValue($date);
  18      }
  19  
  20      /**
  21       * @param mixed $settlement
  22       */
  23      public static function validateSettlementDate($settlement): float
  24      {
  25          return self::validateDate($settlement);
  26      }
  27  
  28      /**
  29       * @param mixed $maturity
  30       */
  31      public static function validateMaturityDate($maturity): float
  32      {
  33          return self::validateDate($maturity);
  34      }
  35  
  36      /**
  37       * @param mixed $value
  38       */
  39      public static function validateFloat($value): float
  40      {
  41          if (!is_numeric($value)) {
  42              throw new Exception(ExcelError::VALUE());
  43          }
  44  
  45          return (float) $value;
  46      }
  47  
  48      /**
  49       * @param mixed $value
  50       */
  51      public static function validateInt($value): int
  52      {
  53          if (!is_numeric($value)) {
  54              throw new Exception(ExcelError::VALUE());
  55          }
  56  
  57          return (int) floor((float) $value);
  58      }
  59  
  60      /**
  61       * @param mixed $rate
  62       */
  63      public static function validateRate($rate): float
  64      {
  65          $rate = self::validateFloat($rate);
  66          if ($rate < 0.0) {
  67              throw new Exception(ExcelError::NAN());
  68          }
  69  
  70          return $rate;
  71      }
  72  
  73      /**
  74       * @param mixed $frequency
  75       */
  76      public static function validateFrequency($frequency): int
  77      {
  78          $frequency = self::validateInt($frequency);
  79          if (
  80              ($frequency !== FinancialConstants::FREQUENCY_ANNUAL) &&
  81              ($frequency !== FinancialConstants::FREQUENCY_SEMI_ANNUAL) &&
  82              ($frequency !== FinancialConstants::FREQUENCY_QUARTERLY)
  83          ) {
  84              throw new Exception(ExcelError::NAN());
  85          }
  86  
  87          return $frequency;
  88      }
  89  
  90      /**
  91       * @param mixed $basis
  92       */
  93      public static function validateBasis($basis): int
  94      {
  95          if (!is_numeric($basis)) {
  96              throw new Exception(ExcelError::VALUE());
  97          }
  98  
  99          $basis = (int) $basis;
 100          if (($basis < 0) || ($basis > 4)) {
 101              throw new Exception(ExcelError::NAN());
 102          }
 103  
 104          return $basis;
 105      }
 106  
 107      /**
 108       * @param mixed $price
 109       */
 110      public static function validatePrice($price): float
 111      {
 112          $price = self::validateFloat($price);
 113          if ($price < 0.0) {
 114              throw new Exception(ExcelError::NAN());
 115          }
 116  
 117          return $price;
 118      }
 119  
 120      /**
 121       * @param mixed $parValue
 122       */
 123      public static function validateParValue($parValue): float
 124      {
 125          $parValue = self::validateFloat($parValue);
 126          if ($parValue < 0.0) {
 127              throw new Exception(ExcelError::NAN());
 128          }
 129  
 130          return $parValue;
 131      }
 132  
 133      /**
 134       * @param mixed $yield
 135       */
 136      public static function validateYield($yield): float
 137      {
 138          $yield = self::validateFloat($yield);
 139          if ($yield < 0.0) {
 140              throw new Exception(ExcelError::NAN());
 141          }
 142  
 143          return $yield;
 144      }
 145  
 146      /**
 147       * @param mixed $discount
 148       */
 149      public static function validateDiscount($discount): float
 150      {
 151          $discount = self::validateFloat($discount);
 152          if ($discount <= 0.0) {
 153              throw new Exception(ExcelError::NAN());
 154          }
 155  
 156          return $discount;
 157      }
 158  }