Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.
   1  <?php
   2  /**
   3   * Copyright 2010-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * @license  http://www.horde.org/licenses/bsd BSD
   6   * @category Horde
   7   * @package  Support
   8   */
   9  
  10  /**
  11   * @license  http://www.horde.org/licenses/bsd BSD
  12   * @category Horde
  13   * @package  Support
  14   */
  15  class Horde_Support_Numerizer_Locale_Pt extends Horde_Support_Numerizer_Locale_Base
  16  {
  17      public $DIRECT_NUMS = array(
  18          'treze' => '13',
  19          'catorze' => '14',
  20          'quatorze' => '14',
  21          'quinze' => '15',
  22          'dezasseis' => '16',
  23          'dezassete' => '17',
  24          'dezoito' => '18',
  25          'dezanove' => '19',
  26          'um(\W|$)' => '1$1',
  27          'uma(\W|$)' => '1$1',
  28          'dois' => '2',
  29          'duas' => '2',
  30          'tres' => '3',
  31          'quatro' => '4',
  32          'cinco' => '5',
  33          'seis' => '6',
  34          'sete' => '7',
  35          'oito' => '8',
  36          'nove' => '9',
  37          'dez' => '10',
  38          'onze' => '11',
  39          'doze' => '12',
  40      );
  41  
  42      public $TEN_PREFIXES = array(
  43          'vinte' => '20',
  44          'trinta' => '30',
  45          'quarenta' => '40',
  46          'cinquenta' => '50',
  47          'sessenta' => '60',
  48          'setenta' => '70',
  49          'oitenta' => '80',
  50          'noventa' => '90',
  51      );
  52  
  53      public $BIG_PREFIXES = array(
  54          'cem' => '100',
  55          'mil' => '1000',
  56          'milhao *' => '1000000',
  57          'milhar de *' => '1000000000',
  58          'biliao *' => '1000000000000',
  59      );
  60  
  61      public function numerize($string)
  62      {
  63          // preprocess
  64          $string = $this->_splitHyphenateWords($string);
  65          $string = $this->_replaceTenPrefixes($string);
  66          $string = $this->_directReplacements($string);
  67          $string = $this->_replaceBigPrefixes($string);
  68  //        $string = $this->_fractionalAddition($string);
  69  
  70          return $string;
  71      }
  72  
  73  
  74      /**
  75       * will mutilate hyphenated-words but shouldn't matter for date extraction
  76       */
  77      protected function _splitHyphenateWords($string)
  78      {
  79          return preg_replace('/ +|([^\d]) e? ([^d])/', '$1 $2', $string);
  80      }
  81  
  82      /**
  83       * easy/direct replacements
  84       */
  85      protected function _directReplacements($string)
  86      {
  87          foreach ($this->DIRECT_NUMS as $dn => $dn_replacement) {
  88              $string = preg_replace("/$dn/i", $dn_replacement, $string);
  89          }
  90          return $string;
  91      }
  92  
  93      /**
  94       * ten, twenty, etc.
  95       */
  96      protected function _replaceTenPrefixes($string)
  97      {
  98          foreach ($this->TEN_PREFIXES as $tp => $tp_replacement) {
  99              $string = preg_replace_callback(
 100                  "/(?:$tp)( *\d(?=[^\d]|\$))*/i",
 101                  function ($m) use ($tp_replacement) {
 102                      return $tp_replacement + (isset($m[1]) ? (int)$m[1] : 0);
 103                  },
 104                  $string
 105              );
 106          }
 107          return $string;
 108      }
 109  
 110      /**
 111       * hundreds, thousands, millions, etc.
 112       */
 113      protected function _replaceBigPrefixes($string)
 114      {
 115          foreach ($this->BIG_PREFIXES as $bp => $bp_replacement) {
 116              $string = preg_replace_callback(
 117                  '/(\d*) *' . $bp . '(\d?)/i',
 118                  function ($m) use ($bp_replacement) {
 119                      $factor = (int)$m[1];
 120                      if (!$factor) {
 121                          $factor = 1;
 122                      }
 123                      return ($bp_replacement * $factor)
 124                          . ($bp_replacement == 100 ? ($m[2] ? 'e' : '') : 'e')
 125                          . $m[2];
 126                  },
 127                  $string);
 128              $string = $this->_andition($string);
 129          }
 130          return $string;
 131      }
 132  
 133      protected function _andition($string)
 134      {
 135          while (preg_match('/(\d+)((?: *e *)+)(\d*)(?=\w|$)/i', $string, $sc, PREG_OFFSET_CAPTURE)) {
 136              $string = substr($string, 0, $sc[1][1]) . ((int)$sc[1][0] + (int)$sc[3][0]) . substr($string, $sc[3][1] + strlen($sc[3][0]));
 137          }
 138          return $string;
 139      }
 140  
 141      protected function _fractionalAddition($string)
 142      {
 143          return preg_replace_callback(
 144              '/(\d+)(?: | e |-)*/i',
 145              function ($m) {
 146                  return (string)((float)$m[1] + 0.5);
 147              },
 148              $string
 149          );
 150      }
 151  
 152  }