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\StreamReader;
  14  use setasign\Fpdi\PdfParser\Tokenizer;
  15  
  16  /**
  17   * Class representing a PDF name object
  18   */
  19  class PdfName extends PdfType
  20  {
  21      /**
  22       * Parses a name object from the passed tokenizer and stream-reader.
  23       *
  24       * @param Tokenizer $tokenizer
  25       * @param StreamReader $streamReader
  26       * @return self
  27       */
  28      public static function parse(Tokenizer $tokenizer, StreamReader $streamReader)
  29      {
  30          $v = new self();
  31          if (\strspn($streamReader->getByte(), "\x00\x09\x0A\x0C\x0D\x20()<>[]{}/%") === 0) {
  32              $v->value = (string) $tokenizer->getNextToken();
  33              return $v;
  34          }
  35  
  36          $v->value = '';
  37          return $v;
  38      }
  39  
  40      /**
  41       * Unescapes a name string.
  42       *
  43       * @param string $value
  44       * @return string
  45       */
  46      public static function unescape($value)
  47      {
  48          if (strpos($value, '#') === false) {
  49              return $value;
  50          }
  51  
  52          return preg_replace_callback('/#([a-fA-F\d]{2})/', function ($matches) {
  53              return chr(hexdec($matches[1]));
  54          }, $value);
  55      }
  56  
  57      /**
  58       * Helper method to create an instance.
  59       *
  60       * @param string $string
  61       * @return self
  62       */
  63      public static function create($string)
  64      {
  65          $v = new self();
  66          $v->value = $string;
  67  
  68          return $v;
  69      }
  70  
  71      /**
  72       * Ensures that the passed value is a PdfName instance.
  73       *
  74       * @param mixed $name
  75       * @return self
  76       * @throws PdfTypeException
  77       */
  78      public static function ensure($name)
  79      {
  80          return PdfType::ensureType(self::class, $name, 'Name value expected.');
  81      }
  82  }