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 310 and 401] [Versions 39 and 401]

   1  <?php
   2  
   3  namespace Box\Spout\Reader\Wrapper;
   4  
   5  use Box\Spout\Reader\Exception\XMLProcessingException;
   6  
   7  /**
   8   * Trait XMLInternalErrorsHelper
   9   */
  10  trait XMLInternalErrorsHelper
  11  {
  12      /** @var bool Stores whether XML errors were initially stored internally - used to reset */
  13      protected $initialUseInternalErrorsValue;
  14  
  15      /**
  16       * To avoid displaying lots of warning/error messages on screen,
  17       * stores errors internally instead.
  18       *
  19       * @return void
  20       */
  21      protected function useXMLInternalErrors()
  22      {
  23          \libxml_clear_errors();
  24          $this->initialUseInternalErrorsValue = \libxml_use_internal_errors(true);
  25      }
  26  
  27      /**
  28       * Throws an XMLProcessingException if an error occured.
  29       * It also always resets the "libxml_use_internal_errors" setting back to its initial value.
  30       *
  31       * @throws \Box\Spout\Reader\Exception\XMLProcessingException
  32       * @return void
  33       */
  34      protected function resetXMLInternalErrorsSettingAndThrowIfXMLErrorOccured()
  35      {
  36          if ($this->hasXMLErrorOccured()) {
  37              $this->resetXMLInternalErrorsSetting();
  38              throw new XMLProcessingException($this->getLastXMLErrorMessage());
  39          }
  40  
  41          $this->resetXMLInternalErrorsSetting();
  42      }
  43  
  44      /**
  45       * Returns whether the a XML error has occured since the last time errors were cleared.
  46       *
  47       * @return bool TRUE if an error occured, FALSE otherwise
  48       */
  49      private function hasXMLErrorOccured()
  50      {
  51          return (\libxml_get_last_error() !== false);
  52      }
  53  
  54      /**
  55       * Returns the error message for the last XML error that occured.
  56       * @see libxml_get_last_error
  57       *
  58       * @return string|null Last XML error message or null if no error
  59       */
  60      private function getLastXMLErrorMessage()
  61      {
  62          $errorMessage = null;
  63          $error = \libxml_get_last_error();
  64  
  65          if ($error !== false) {
  66              $errorMessage = \trim($error->message);
  67          }
  68  
  69          return $errorMessage;
  70      }
  71  
  72      /**
  73       * @return void
  74       */
  75      protected function resetXMLInternalErrorsSetting()
  76      {
  77          \libxml_use_internal_errors($this->initialUseInternalErrorsValue);
  78      }
  79  }