Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 310 and 311] [Versions 39 and 311]

   1  <?php
   2  
   3  /**
   4   * This file is part of FPDI
   5   *
   6   * @package   setasign\Fpdi
   7   * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com)
   8   * @license   http://opensource.org/licenses/mit-license The MIT License
   9   */
  10  
  11  namespace setasign\Fpdi\PdfParser\Type;
  12  
  13  use setasign\Fpdi\PdfParser\PdfParser;
  14  use setasign\Fpdi\PdfParser\StreamReader;
  15  use setasign\Fpdi\PdfParser\Tokenizer;
  16  
  17  /**
  18   * Class representing a PDF dictionary object
  19   */
  20  class PdfDictionary extends PdfType
  21  {
  22      /**
  23       * Parses a dictionary of the passed tokenizer, stream-reader and parser.
  24       *
  25       * @param Tokenizer $tokenizer
  26       * @param StreamReader $streamReader
  27       * @param PdfParser $parser
  28       * @return bool|self
  29       * @throws PdfTypeException
  30       */
  31      public static function parse(Tokenizer $tokenizer, StreamReader $streamReader, PdfParser $parser)
  32      {
  33          $entries = [];
  34  
  35          while (true) {
  36              $token = $tokenizer->getNextToken();
  37              if ($token === '>' && $streamReader->getByte() === '>') {
  38                  $streamReader->addOffset(1);
  39                  break;
  40              }
  41  
  42              $key = $parser->readValue($token);
  43              if ($key === false) {
  44                  return false;
  45              }
  46  
  47              // ensure the first value to be a Name object
  48              if (!($key instanceof PdfName)) {
  49                  $lastToken = null;
  50                  // ignore all other entries and search for the closing brackets
  51                  while (($token = $tokenizer->getNextToken()) !== '>' && $token !== false && $lastToken !== '>') {
  52                      $lastToken = $token;
  53                  }
  54  
  55                  if ($token === false) {
  56                      return false;
  57                  }
  58  
  59                  break;
  60              }
  61  
  62  
  63              $value = $parser->readValue();
  64              if ($value === false) {
  65                  return false;
  66              }
  67  
  68              if ($value instanceof PdfNull) {
  69                  continue;
  70              }
  71  
  72              // catch missing value
  73              if ($value instanceof PdfToken && $value->value === '>' && $streamReader->getByte() === '>') {
  74                  $streamReader->addOffset(1);
  75                  break;
  76              }
  77  
  78              $entries[$key->value] = $value;
  79          }
  80  
  81          $v = new self();
  82          $v->value = $entries;
  83  
  84          return $v;
  85      }
  86  
  87      /**
  88       * Helper method to create an instance.
  89       *
  90       * @param PdfType[] $entries The keys are the name entries of the dictionary.
  91       * @return self
  92       */
  93      public static function create(array $entries = [])
  94      {
  95          $v = new self();
  96          $v->value = $entries;
  97  
  98          return $v;
  99      }
 100  
 101      /**
 102       * Get a value by its key from a dictionary or a default value.
 103       *
 104       * @param mixed $dictionary
 105       * @param string $key
 106       * @param PdfType|null $default
 107       * @return PdfNull|PdfType
 108       * @throws PdfTypeException
 109       */
 110      public static function get($dictionary, $key, PdfType $default = null)
 111      {
 112          $dictionary = self::ensure($dictionary);
 113  
 114          if (isset($dictionary->value[$key])) {
 115              return $dictionary->value[$key];
 116          }
 117  
 118          return $default === null
 119              ? new PdfNull()
 120              : $default;
 121      }
 122  
 123      /**
 124       * Ensures that the passed value is a PdfDictionary instance.
 125       *
 126       * @param mixed $dictionary
 127       * @return self
 128       * @throws PdfTypeException
 129       */
 130      public static function ensure($dictionary)
 131      {
 132          return PdfType::ensureType(self::class, $dictionary, 'Dictionary value expected.');
 133      }
 134  }