Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Stream.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Pdf/Element/Stream.php';
25 #require_once 'Zend/Pdf/Element/Dictionary.php';
26 #require_once 'Zend/Pdf/Element/Numeric.php';
27 
28 
30 #require_once 'Zend/Pdf/Element/Object.php';
31 
41 {
49  private $_dictionary;
50 
56  private $_streamDecoded;
57 
66  private $_initialDictionaryData = null;
67 
78  public function __construct($val, $objNum, $genNum, Zend_Pdf_ElementFactory $factory, $dictionary = null)
79  {
80  parent::__construct(new Zend_Pdf_Element_Stream($val), $objNum, $genNum, $factory);
81 
82  if ($dictionary === null) {
83  $this->_dictionary = new Zend_Pdf_Element_Dictionary();
84  $this->_dictionary->Length = new Zend_Pdf_Element_Numeric(strlen( $val ));
85  $this->_streamDecoded = true;
86  } else {
87  $this->_dictionary = $dictionary;
88  $this->_streamDecoded = false;
89  }
90  }
91 
92 
99  private function _extractDictionaryData()
100  {
101  $dictionaryArray = array();
102 
103  $dictionaryArray['Filter'] = array();
104  $dictionaryArray['DecodeParms'] = array();
105  if ($this->_dictionary->Filter === null) {
106  // Do nothing.
107  } else if ($this->_dictionary->Filter->getType() == Zend_Pdf_Element::TYPE_ARRAY) {
108  foreach ($this->_dictionary->Filter->items as $id => $filter) {
109  $dictionaryArray['Filter'][$id] = $filter->value;
110  $dictionaryArray['DecodeParms'][$id] = array();
111 
112  if ($this->_dictionary->DecodeParms !== null ) {
113  if ($this->_dictionary->DecodeParms->items[$id] !== null &&
114  $this->_dictionary->DecodeParms->items[$id]->value !== null ) {
115  foreach ($this->_dictionary->DecodeParms->items[$id]->getKeys() as $paramKey) {
116  $dictionaryArray['DecodeParms'][$id][$paramKey] =
117  $this->_dictionary->DecodeParms->items[$id]->$paramKey->value;
118  }
119  }
120  }
121  }
122  } else if ($this->_dictionary->Filter->getType() != Zend_Pdf_Element::TYPE_NULL) {
123  $dictionaryArray['Filter'][0] = $this->_dictionary->Filter->value;
124  $dictionaryArray['DecodeParms'][0] = array();
125  if ($this->_dictionary->DecodeParms !== null ) {
126  foreach ($this->_dictionary->DecodeParms->getKeys() as $paramKey) {
127  $dictionaryArray['DecodeParms'][0][$paramKey] =
128  $this->_dictionary->DecodeParms->$paramKey->value;
129  }
130  }
131  }
132 
133  if ($this->_dictionary->F !== null) {
134  $dictionaryArray['F'] = $this->_dictionary->F->value;
135  }
136 
137  $dictionaryArray['FFilter'] = array();
138  $dictionaryArray['FDecodeParms'] = array();
139  if ($this->_dictionary->FFilter === null) {
140  // Do nothing.
141  } else if ($this->_dictionary->FFilter->getType() == Zend_Pdf_Element::TYPE_ARRAY) {
142  foreach ($this->_dictionary->FFilter->items as $id => $filter) {
143  $dictionaryArray['FFilter'][$id] = $filter->value;
144  $dictionaryArray['FDecodeParms'][$id] = array();
145 
146  if ($this->_dictionary->FDecodeParms !== null ) {
147  if ($this->_dictionary->FDecodeParms->items[$id] !== null &&
148  $this->_dictionary->FDecodeParms->items[$id]->value !== null) {
149  foreach ($this->_dictionary->FDecodeParms->items[$id]->getKeys() as $paramKey) {
150  $dictionaryArray['FDecodeParms'][$id][$paramKey] =
151  $this->_dictionary->FDecodeParms->items[$id]->items[$paramKey]->value;
152  }
153  }
154  }
155  }
156  } else {
157  $dictionaryArray['FFilter'][0] = $this->_dictionary->FFilter->value;
158  $dictionaryArray['FDecodeParms'][0] = array();
159  if ($this->_dictionary->FDecodeParms !== null ) {
160  foreach ($this->_dictionary->FDecodeParms->getKeys() as $paramKey) {
161  $dictionaryArray['FDecodeParms'][0][$paramKey] =
162  $this->_dictionary->FDecodeParms->items[$paramKey]->value;
163  }
164  }
165  }
166 
167  return $dictionaryArray;
168  }
169 
175  private function _decodeStream()
176  {
177  if ($this->_initialDictionaryData === null) {
178  $this->_initialDictionaryData = $this->_extractDictionaryData();
179  }
180 
185  if (isset($this->_initialDictionaryData['F'])) {
187  #require_once 'Zend/Pdf/Exception.php';
188  throw new Zend_Pdf_Exception('External filters are not supported now.');
189  }
190 
191  foreach ($this->_initialDictionaryData['Filter'] as $id => $filterName ) {
192  $valueRef = &$this->_value->value->getRef();
193  $this->_value->value->touch();
194  switch ($filterName) {
195  case 'ASCIIHexDecode':
196  #require_once 'Zend/Pdf/Filter/AsciiHex.php';
197  $valueRef = Zend_Pdf_Filter_AsciiHex::decode($valueRef);
198  break;
199 
200  case 'ASCII85Decode':
201  #require_once 'Zend/Pdf/Filter/Ascii85.php';
202  $valueRef = Zend_Pdf_Filter_Ascii85::decode($valueRef);
203  break;
204 
205  case 'FlateDecode':
206  #require_once 'Zend/Pdf/Filter/Compression/Flate.php';
207  $valueRef = Zend_Pdf_Filter_Compression_Flate::decode($valueRef,
208  $this->_initialDictionaryData['DecodeParms'][$id]);
209  break;
210 
211  case 'LZWDecode':
212  #require_once 'Zend/Pdf/Filter/Compression/Lzw.php';
213  $valueRef = Zend_Pdf_Filter_Compression_Lzw::decode($valueRef,
214  $this->_initialDictionaryData['DecodeParms'][$id]);
215  break;
216 
217  case 'RunLengthDecode':
218  #require_once 'Zend/Pdf/Filter/RunLength.php';
219  $valueRef = Zend_Pdf_Filter_RunLength::decode($valueRef);
220  break;
221 
222  default:
223  #require_once 'Zend/Pdf/Exception.php';
224  throw new Zend_Pdf_Exception('Unknown stream filter: \'' . $filterName . '\'.');
225  }
226  }
227 
228  $this->_streamDecoded = true;
229  }
230 
236  private function _encodeStream()
237  {
242  if (isset($this->_initialDictionaryData['F'])) {
244  #require_once 'Zend/Pdf/Exception.php';
245  throw new Zend_Pdf_Exception('External filters are not supported now.');
246  }
247 
248  $filters = array_reverse($this->_initialDictionaryData['Filter'], true);
249 
250  foreach ($filters as $id => $filterName ) {
251  $valueRef = &$this->_value->value->getRef();
252  $this->_value->value->touch();
253  switch ($filterName) {
254  case 'ASCIIHexDecode':
255  #require_once 'Zend/Pdf/Filter/AsciiHex.php';
256  $valueRef = Zend_Pdf_Filter_AsciiHex::encode($valueRef);
257  break;
258 
259  case 'ASCII85Decode':
260  #require_once 'Zend/Pdf/Filter/Ascii85.php';
261  $valueRef = Zend_Pdf_Filter_Ascii85::encode($valueRef);
262  break;
263 
264  case 'FlateDecode':
265  #require_once 'Zend/Pdf/Filter/Compression/Flate.php';
266  $valueRef = Zend_Pdf_Filter_Compression_Flate::encode($valueRef,
267  $this->_initialDictionaryData['DecodeParms'][$id]);
268  break;
269 
270  case 'LZWDecode':
271  #require_once 'Zend/Pdf/Filter/Compression/Lzw.php';
272  $valueRef = Zend_Pdf_Filter_Compression_Lzw::encode($valueRef,
273  $this->_initialDictionaryData['DecodeParms'][$id]);
274  break;
275 
276  case 'RunLengthDecode':
277  #require_once 'Zend/Pdf/Filter/RunLength.php';
278  $valueRef = Zend_Pdf_Filter_RunLength::encode($valueRef);
279  break;
280 
281  default:
282  #require_once 'Zend/Pdf/Exception.php';
283  throw new Zend_Pdf_Exception('Unknown stream filter: \'' . $filterName . '\'.');
284  }
285  }
286 
287  $this->_streamDecoded = false;
288  }
289 
297  public function __get($property)
298  {
299  if ($property == 'dictionary') {
303  if (( !$this->_streamDecoded ) && ($this->_initialDictionaryData === null)) {
304  $this->_initialDictionaryData = $this->_extractDictionaryData();
305  }
306 
307  return $this->_dictionary;
308  }
309 
310  if ($property == 'value') {
311  if (!$this->_streamDecoded) {
312  $this->_decodeStream();
313  }
314 
315  return $this->_value->value->getRef();
316  }
317 
318  #require_once 'Zend/Pdf/Exception.php';
319  throw new Zend_Pdf_Exception('Unknown stream object property requested.');
320  }
321 
322 
329  public function __set($property, $value)
330  {
331  if ($property == 'value') {
332  $valueRef = &$this->_value->value->getRef();
333  $valueRef = $value;
334  $this->_value->value->touch();
335 
336  $this->_streamDecoded = true;
337 
338  return;
339  }
340 
341  #require_once 'Zend/Pdf/Exception.php';
342  throw new Zend_Pdf_Exception('Unknown stream object property: \'' . $property . '\'.');
343  }
344 
345 
349  public function skipFilters()
350  {
351  $this->_streamDecoded = false;
352  }
353 
354 
362  public function __call($method, $args)
363  {
364  if (!$this->_streamDecoded) {
365  $this->_decodeStream();
366  }
367 
368  switch (count($args)) {
369  case 0:
370  return $this->_value->$method();
371  case 1:
372  return $this->_value->$method($args[0]);
373  default:
374  #require_once 'Zend/Pdf/Exception.php';
375  throw new Zend_Pdf_Exception('Unsupported number of arguments');
376  }
377  }
378 
387  public function makeClone(Zend_Pdf_ElementFactory $factory, array &$processed, $mode)
388  {
389  $id = spl_object_hash($this);
390  if (isset($processed[$id])) {
391  // Do nothing if object is already processed
392  // return it
393  return $processed[$id];
394  }
395 
396  $streamValue = $this->_value;
397  $streamDictionary = $this->_dictionary->makeClone($factory, $processed, $mode);
398 
399  // Make new empty instance of stream object and register it in $processed container
400  $processed[$id] = $clonedObject = $factory->newStreamObject('');
401 
402  // Copy current object data and state
403  $clonedObject->_dictionary = $this->_dictionary->makeClone($factory, $processed, $mode);
404  $clonedObject->_value = $this->_value->makeClone($factory, $processed, $mode);
405  $clonedObject->_initialDictionaryData = $this->_initialDictionaryData;
406  $clonedObject->_streamDecoded = $this->_streamDecoded;
407 
408  return $clonedObject;
409  }
410 
419  public function dump(Zend_Pdf_ElementFactory $factory)
420  {
421  $shift = $factory->getEnumerationShift($this->_factory);
422 
423  if ($this->_streamDecoded) {
424  $this->_initialDictionaryData = $this->_extractDictionaryData();
425  $this->_encodeStream();
426  } else if ($this->_initialDictionaryData != null) {
427  $newDictionary = $this->_extractDictionaryData();
428 
429  if ($this->_initialDictionaryData !== $newDictionary) {
430  $this->_decodeStream();
431  $this->_initialDictionaryData = $newDictionary;
432  $this->_encodeStream();
433  }
434  }
435 
436  // Update stream length
437  $this->dictionary->Length->value = $this->_value->length();
438 
439  return $this->_objNum + $shift . " " . $this->_genNum . " obj \n"
440  . $this->dictionary->toString($factory) . "\n"
441  . $this->_value->toString($factory) . "\n"
442  . "endobj\n";
443  }
444 
448  public function cleanUp()
449  {
450  $this->_dictionary = null;
451  $this->_value = null;
452  }
453 }
$block setTitle( 'CMS Block Title') -> setIdentifier('fixture_block') ->setContent('< h1 >Fixture Block Title</h1 >< a href=" store url</a><p> Config value
Definition: block.php:9
const TYPE_ARRAY
Definition: Element.php:36
static decode($data, $params=null)
Definition: Ascii85.php:114
$id
Definition: fieldset.phtml:14
static decode($data, $params=null)
Definition: Lzw.php:84
static decode($data, $params=null)
Definition: Flate.php:76
const TYPE_NULL
Definition: Element.php:39
static decode($data, $params=null)
Definition: AsciiHex.php:56
static decode($data, $params=null)
Definition: RunLength.php:95
__construct($val, $objNum, $genNum, Zend_Pdf_ElementFactory $factory, $dictionary=null)
Definition: Stream.php:78