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.

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403]

   1  <?php
   2  /**
   3   * This file is part of FPDI
   4   *
   5   * @package   setasign\Fpdi
   6   * @copyright Copyright (c) 2019 Setasign - Jan Slabon (https://www.setasign.com)
   7   * @license   http://opensource.org/licenses/mit-license The MIT License
   8   */
   9  
  10  namespace setasign\Fpdi\PdfParser\Type;
  11  
  12  use setasign\Fpdi\PdfParser\PdfParser;
  13  use setasign\Fpdi\PdfParser\Tokenizer;
  14  
  15  /**
  16   * Class representing a PDF array object
  17   *
  18   * @package setasign\Fpdi\PdfParser\Type
  19   * @property array $value The value of the PDF type.
  20   */
  21  class PdfArray extends PdfType
  22  {
  23      /**
  24       * Parses an array of the passed tokenizer and parser.
  25       *
  26       * @param Tokenizer $tokenizer
  27       * @param PdfParser $parser
  28       * @return bool|self
  29       * @throws PdfTypeException
  30       */
  31      public static function parse(Tokenizer $tokenizer, PdfParser $parser)
  32      {
  33          $result = [];
  34  
  35          // Recurse into this function until we reach the end of the array.
  36          while (($token = $tokenizer->getNextToken()) !== ']') {
  37              if ($token === false || ($value = $parser->readValue($token)) === false) {
  38                  return false;
  39              }
  40  
  41              $result[] = $value;
  42          }
  43  
  44          $v = new self;
  45          $v->value = $result;
  46  
  47          return $v;
  48      }
  49  
  50      /**
  51       * Helper method to create an instance.
  52       *
  53       * @param PdfType[] $values
  54       * @return self
  55       */
  56      public static function create(array $values = [])
  57      {
  58          $v = new self;
  59          $v->value = $values;
  60  
  61          return $v;
  62      }
  63  
  64      /**
  65       * Ensures that the passed array is a PdfArray instance with a (optional) specific size.
  66       *
  67       * @param mixed $array
  68       * @param null|int $size
  69       * @return self
  70       * @throws PdfTypeException
  71       */
  72      public static function ensure($array, $size = null)
  73      {
  74          $result = PdfType::ensureType(self::class, $array, 'Array value expected.');
  75  
  76          if ($size !== null && \count($array->value) !== $size) {
  77              throw new PdfTypeException(
  78                  \sprintf('Array with %s entries expected.', $size),
  79                  PdfTypeException::INVALID_DATA_SIZE
  80              );
  81          }
  82  
  83          return $result;
  84      }
  85  }