1<?php 2/** 3 * This file is part of FPDI 4 * 5 * @package setasign\Fpdi 6 * @copyright Copyright (c) 2020 Setasign GmbH & Co. KG (https://www.setasign.com) 7 * @license http://opensource.org/licenses/mit-license The MIT License 8 */ 9 10namespace setasign\Fpdi\PdfParser\Type; 11 12use setasign\Fpdi\PdfParser\PdfParser; 13use 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 */ 21class 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} 86