/** * @param string $content * @param Document $document * @param int $offset * * @return bool|ElementStruct */ public static function parse($content, Document $document = null, &$offset = 0) { if (preg_match('/^\\s*<<(?<struct>.*)/is', $content)) { preg_match_all('/(.*?)(<<|>>)/s', trim($content), $matches); $level = 0; $sub = ''; foreach ($matches[0] as $part) { $sub .= $part; $level += strpos($part, '<<') !== false ? 1 : -1; if ($level <= 0) { break; } } $offset += strpos($content, '<<') + strlen(rtrim($sub)); // Removes '<<' and '>>'. $sub = trim(preg_replace('/^\\s*<<(.*)>>\\s*$/s', '\\1', $sub)); $position = 0; $elements = Element::parse($sub, $document, $position); $header = new Header($elements, $document); return $header; } return false; }
/** * @param string $value * @param Document $document */ public function __construct($value, Document $document = null) { parent::__construct(null, null); }
/** * @param string $value * @param Document $document */ public function __construct($value, Document $document = null) { parent::__construct(floatval($value), null); }
/** * @param string $content * @param Document $document * @param int $offset * * @return bool|ElementArray */ public static function parse($content, Document $document = null, &$offset = 0) { if (preg_match('/^\\s*\\[(?P<array>.*)/is', $content, $match)) { preg_match_all('/(.*?)(\\[|\\])/s', trim($content), $matches); $level = 0; $sub = ''; foreach ($matches[0] as $part) { $sub .= $part; $level += strpos($part, '[') !== false ? 1 : -1; if ($level <= 0) { break; } } // Removes 1 level [ and ]. $sub = substr(trim($sub), 1, -1); $sub_offset = 0; $values = Element::parse($sub, $document, $sub_offset, true); $offset += strpos($content, '[') + 1; // Find next ']' position $offset += strlen($sub) + 1; return new self($values, $document); } return false; }
/** * @param string $value * @param Document $document */ public function __construct($value, Document $document = null) { parent::__construct(strtolower($value) == 'true' || $value === true, null); }
public function testParse() { $document = new \Smalot\PdfParser\Document(array()); // Only_values = false. $content = '/NameType /FlateDecode /Contents[4 0 R 42]/Fonts<</F1 41/F2 43>>/NullType null/StringType(hello)/DateType(D:20130901235555+02\'00\')/XRefType 2 0 R /NumericType 8/HexaType<0020>/BooleanType false'; $offset = 0; $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false); $this->assert->array($elements)->hasKey('NameType'); $this->assert->object($elements['NameType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementName'); $this->assert->string($elements['NameType']->getContent())->isEqualTo('FlateDecode'); $this->assert->boolean(array_key_exists('Contents', $elements))->isEqualTo(true); $this->assert->object($elements['Contents'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementArray'); $this->assert->boolean($elements['Contents']->contains(42))->isEqualTo(true); $this->assert->boolean(array_key_exists('Fonts', $elements))->isEqualTo(true); $this->assert->object($elements['Fonts'])->isInstanceOf('\\Smalot\\PdfParser\\Header'); $this->assert->boolean(array_key_exists('NullType', $elements))->isEqualTo(true); $this->assert->object($elements['NullType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementNull'); $this->assert->castToString($elements['NullType'])->isEqualTo('null'); $this->assert->boolean(array_key_exists('StringType', $elements))->isEqualTo(true); $this->assert->object($elements['StringType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementString'); $this->assert->string($elements['StringType']->getContent())->isEqualTo('hello'); $this->assert->boolean(array_key_exists('DateType', $elements))->isEqualTo(true); $this->assert->object($elements['DateType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementDate'); // $this->assert->castToString($elements['DateType'])->isEqualTo('2013-09-01T23:55:55+02:00'); $this->assert->boolean(array_key_exists('XRefType', $elements))->isEqualTo(true); $this->assert->object($elements['XRefType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementXRef'); $this->assert->string($elements['XRefType']->getId())->isEqualTo('2_0'); $this->assert->boolean(array_key_exists('NumericType', $elements))->isEqualTo(true); $this->assert->object($elements['NumericType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementNumeric'); $this->assert->castToString($elements['NumericType'])->isEqualTo('8'); $this->assert->boolean(array_key_exists('HexaType', $elements))->isEqualTo(true); $this->assert->object($elements['HexaType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementString'); $this->assert->string($elements['HexaType']->getContent())->isEqualTo(' '); $this->assert->boolean(array_key_exists('BooleanType', $elements))->isEqualTo(true); $this->assert->object($elements['BooleanType'])->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementBoolean'); $this->assert->boolean($elements['BooleanType']->getContent())->isEqualTo(false); // Only_values = true. $content = '/NameType /FlateDecode'; $offset = 0; $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, true); $this->assert->array($elements)->hasSize(2); $this->assert->integer($offset)->isEqualTo(22); // Test error. $content = '/NameType /FlateDecode $$$'; $offset = 0; $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false); $this->assert->array($elements)->hasSize(1); $this->assert->integer($offset)->isEqualTo(22); $this->assert->string(key($elements))->isEqualTo('NameType'); $this->assert->object(current($elements))->isInstanceOf('\\Smalot\\PdfParser\\Element\\ElementName'); $content = '/NameType $$$'; $offset = 0; $elements = \Smalot\PdfParser\Element::parse($content, $document, $offset, false); $this->assert->integer($offset)->isEqualTo(0); $this->assert->array($elements)->isEmpty(); /*$this->assert->boolean(array_key_exists('NameType', $elements))->isEqualTo(true); $this->assert->boolean($elements['NameType'])->isInstanceOf('\Smalot\PdfParser\Element\ElementName)->isEqualTo(true); $this->assert->string($elements['NameType']->getContent())->isEqualTo('FlateDecode');*/ }