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

   1  <?php
   2  /**
   3   * Copyright 2015-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2015-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Mime
  12   */
  13  
  14  /**
  15   * Recursive iterator for Horde_Mime_Part objects. This iterator is
  16   * self-contained and independent of all other iterators.
  17   *
  18   * @author    Michael Slusarz <slusarz@horde.org>
  19   * @category  Horde
  20   * @copyright 2015-2017 Horde LLC
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Mime
  23   * @since     2.9.0
  24   */
  25  class Horde_Mime_Part_Iterator
  26  implements Countable, Iterator
  27  {
  28      /**
  29       * Include the base when iterating?
  30       *
  31       * @var boolean
  32       */
  33      protected $_includeBase;
  34  
  35      /**
  36       * Base part.
  37       *
  38       * @var Horde_Mime_Part
  39       */
  40      protected $_part;
  41  
  42      /**
  43       * State data.
  44       *
  45       * @var object
  46       */
  47      protected $_state;
  48  
  49      /**
  50       * Constructor.
  51       */
  52      public function __construct(Horde_Mime_Part $part, $base = false)
  53      {
  54          $this->_includeBase = (bool)$base;
  55          $this->_part = $part;
  56      }
  57  
  58      /* Countable methods. */
  59  
  60      /**
  61       * Returns the number of message parts.
  62       *
  63       * @return integer  Number of message parts.
  64       */
  65      #[ReturnTypeWillChange]
  66      public function count()
  67      {
  68          return count(iterator_to_array($this));
  69      }
  70  
  71      /* RecursiveIterator methods. */
  72  
  73      /**
  74       */
  75      #[ReturnTypeWillChange]
  76      public function current()
  77      {
  78          return $this->valid()
  79              ? $this->_state->current
  80              : null;
  81      }
  82  
  83      /**
  84       */
  85      #[ReturnTypeWillChange]
  86      public function key()
  87      {
  88          return ($curr = $this->current())
  89              ? $curr->getMimeId()
  90              : null;
  91      }
  92  
  93      /**
  94       */
  95      #[ReturnTypeWillChange]
  96      public function next()
  97      {
  98          if (!isset($this->_state)) {
  99              return;
 100          }
 101  
 102          $out = $this->_state->current->getPartByIndex($this->_state->index++);
 103  
 104          if ($out) {
 105              $this->_state->recurse[] = array(
 106                  $this->_state->current,
 107                  $this->_state->index
 108              );
 109              $this->_state->current = $out;
 110              $this->_state->index = 0;
 111          } elseif ($tmp = array_pop($this->_state->recurse)) {
 112              $this->_state->current = $tmp[0];
 113              $this->_state->index = $tmp[1];
 114              $this->next();
 115          } else {
 116              unset($this->_state);
 117          }
 118      }
 119  
 120      /**
 121       */
 122      #[ReturnTypeWillChange]
 123      public function rewind()
 124      {
 125          $this->_state = new stdClass;
 126          $this->_state->current = $this->_part;
 127          $this->_state->index = 0;
 128          $this->_state->recurse = array();
 129  
 130          if (!$this->_includeBase) {
 131              $this->next();
 132          }
 133      }
 134  
 135      /**
 136       */
 137      #[ReturnTypeWillChange]
 138      public function valid()
 139      {
 140          return !empty($this->_state);
 141      }
 142  
 143  }