Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ElementFactory.php
Go to the documentation of this file.
1 <?php
24 #require_once 'Zend/Pdf/ElementFactory/Interface.php';
25 
35 {
44  private $_modifiedObjects = array();
45 
53  private $_removedObjects;
54 
63  private $_registeredObjects = array();
64 
71  private $_objectCount;
72 
73 
80  private $_attachedFactories = array();
81 
82 
88  private $_factoryId;
89 
95  private static $_identity = 0;
96 
97 
103  private $_shiftCalculationCache = array();
104 
105 
111  public function __construct($objCount)
112  {
113  $this->_objectCount = (int)$objCount;
114  $this->_factoryId = self::$_identity++;
115  $this->_removedObjects = new SplObjectStorage();
116  }
117 
118 
124  public function getFactory()
125  {
126  return $this;
127  }
128 
135  static public function createFactory($objCount)
136  {
137  #require_once 'Zend/Pdf/ElementFactory/Proxy.php';
138  return new Zend_Pdf_ElementFactory_Proxy(new Zend_Pdf_ElementFactory($objCount));
139  }
140 
146  public function close()
147  {
148  $this->_modifiedObjects = null;
149  $this->_removedObjects = null;
150  $this->_attachedFactories = null;
151 
152  foreach ($this->_registeredObjects as $obj) {
153  $obj->cleanUp();
154  }
155  $this->_registeredObjects = null;
156  }
157 
163  public function resolve()
164  {
165  return $this;
166  }
167 
173  public function getId()
174  {
175  return $this->_factoryId;
176  }
177 
183  public function setObjectCount($objCount)
184  {
185  $this->_objectCount = (int)$objCount;
186  }
187 
193  public function getObjectCount()
194  {
195  $count = $this->_objectCount;
196 
197  foreach ($this->_attachedFactories as $attached) {
198  $count += $attached->getObjectCount() - 1; // -1 as "0" object is a special case and shared between factories
199  }
200 
201  return $count;
202  }
203 
204 
211  {
212  if ( $factory === $this || isset($this->_attachedFactories[$factory->getId()])) {
219  return;
220  }
221 
222  $this->_attachedFactories[$factory->getId()] = $factory;
223  }
224 
225 
233  {
234  if ($factory === $this) {
235  return 0;
236  }
237 
238  if (isset($this->_shiftCalculationCache[$factory->_factoryId])) {
239  return $this->_shiftCalculationCache[$factory->_factoryId];
240  }
241 
242  $shift = $this->_objectCount - 1;
243 
244  foreach ($this->_attachedFactories as $subFactory) {
245  $subFactoryShift = $subFactory->calculateShift($factory);
246 
247  if ($subFactoryShift != -1) {
248  // context found
249  $this->_shiftCalculationCache[$factory->_factoryId] = $shift + $subFactoryShift;
250  return $shift + $subFactoryShift;
251  } else {
252  $shift += $subFactory->getObjectCount()-1;
253  }
254  }
255 
256  $this->_shiftCalculationCache[$factory->_factoryId] = -1;
257  return -1;
258  }
259 
264  public function cleanEnumerationShiftCache()
265  {
266  $this->_shiftCalculationCache = array();
267 
268  foreach ($this->_attachedFactories as $attached) {
269  $attached->cleanEnumerationShiftCache();
270  }
271  }
272 
281  {
282  if (($shift = $this->calculateShift($factory)) == -1) {
283  #require_once 'Zend/Pdf/Exception.php';
284  throw new Zend_Pdf_Exception('Wrong object context');
285  }
286 
287  return $shift;
288  }
289 
297  {
298  if ($obj->getFactory() !== $this) {
299  #require_once 'Zend/Pdf/Exception.php';
300  throw new Zend_Pdf_Exception('Object is not generated by this factory');
301  }
302 
303  $this->_modifiedObjects[$obj->getObjNum()] = $obj;
304  }
305 
306 
313  public function remove(Zend_Pdf_Element_Object $obj)
314  {
315  if (!$obj->compareFactory($this)) {
316  #require_once 'Zend/Pdf/Exception.php';
317  throw new Zend_Pdf_Exception('Object is not generated by this factory');
318  }
319 
320  $this->_modifiedObjects[$obj->getObjNum()] = $obj;
321  $this->_removedObjects->attach($obj);
322  }
323 
324 
333  public function newObject(Zend_Pdf_Element $objectValue)
334  {
335  #require_once 'Zend/Pdf/Element/Object.php';
336  $obj = new Zend_Pdf_Element_Object($objectValue, $this->_objectCount++, 0, $this);
337  $this->_modifiedObjects[$obj->getObjNum()] = $obj;
338  return $obj;
339  }
340 
349  public function newStreamObject($streamValue)
350  {
351  #require_once 'Zend/Pdf/Element/Object/Stream.php';
352  $obj = new Zend_Pdf_Element_Object_Stream($streamValue, $this->_objectCount++, 0, $this);
353  $this->_modifiedObjects[$obj->getObjNum()] = $obj;
354  return $obj;
355  }
356 
357 
365  public function listModifiedObjects($rootFactory = null)
366  {
367  if ($rootFactory == null) {
368  $rootFactory = $this;
369  $shift = 0;
370  } else {
371  $shift = $rootFactory->getEnumerationShift($this);
372  }
373 
374  ksort($this->_modifiedObjects);
375 
376  $result = array();
377  #require_once 'Zend/Pdf/UpdateInfoContainer.php';
378  foreach ($this->_modifiedObjects as $objNum => $obj) {
379  if ($this->_removedObjects->contains($obj)) {
380  $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
381  $obj->getGenNum()+1,
382  true);
383  } else {
384  $result[$objNum+$shift] = new Zend_Pdf_UpdateInfoContainer($objNum + $shift,
385  $obj->getGenNum(),
386  false,
387  $obj->dump($rootFactory));
388  }
389  }
390 
391  foreach ($this->_attachedFactories as $factory) {
392  $result += $factory->listModifiedObjects($rootFactory);
393  }
394 
395  return $result;
396  }
397 
406  public function registerObject(Zend_Pdf_Element_Object $obj, $refString)
407  {
408  $this->_registeredObjects[$refString] = $obj;
409  }
410 
417  public function fetchObject($refString)
418  {
419  if (!isset($this->_registeredObjects[$refString])) {
420  return null;
421  }
422  return $this->_registeredObjects[$refString];
423  }
424 
425 
431  public function isModified()
432  {
433  if (count($this->_modifiedObjects) != 0) {
434  return true;
435  }
436 
437  foreach ($this->_attachedFactories as $subFactory) {
438  if ($subFactory->isModified()) {
439  return true;
440  }
441  }
442 
443  return false;
444  }
445 }
446 
calculateShift(Zend_Pdf_ElementFactory_Interface $factory)
$count
Definition: recent.phtml:13
newObject(Zend_Pdf_Element $objectValue)
listModifiedObjects($rootFactory=null)
attach(Zend_Pdf_ElementFactory_Interface $factory)
newStreamObject($streamValue)
markAsModified(Zend_Pdf_Element_Object $obj)
getEnumerationShift(Zend_Pdf_ElementFactory_Interface $factory)
registerObject(Zend_Pdf_Element_Object $obj, $refString)
static createFactory($objCount)