Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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\StreamReader;
  13  use setasign\Fpdi\PdfParser\Tokenizer;
  14  
  15  /**
  16   * Class representing a PDF name object
  17   *
  18   * @package setasign\Fpdi\PdfParser\Type
  19   */
  20  class PdfName extends PdfType
  21  {
  22      /**
  23       * Parses a name object from the passed tokenizer and stream-reader.
  24       *
  25       * @param Tokenizer $tokenizer
  26       * @param StreamReader $streamReader
  27       * @return self
  28       */
  29      public static function parse(Tokenizer $tokenizer, StreamReader $streamReader)
  30      {
  31          $v = new self;
  32          if (\strspn($streamReader->getByte(), "\x00\x09\x0A\x0C\x0D\x20()<>[]{}/%") === 0) {
  33              $v->value = (string) $tokenizer->getNextToken();
  34              return $v;
  35          }
  36  
  37          $v->value = '';
  38          return $v;
  39      }
  40  
  41      /**
  42       * Unescapes a name string.
  43       *
  44       * @param string $value
  45       * @return string
  46       */
  47      static public function unescape($value)
  48      {
  49          if (strpos($value, '#') === false)
  50              return $value;
  51  
  52          return preg_replace_callback('/#[a-fA-F\d]{2}/', function($matches) {
  53              return chr(hexdec($matches[0]));
  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  }