PhpOffice\PhpPresentation\HashTable::addFromSource PHP Method

addFromSource() public method

Add HashTable items from source
public addFromSource ( PhpOffice\PhpPresentation\ComparableInterface[] $pSource = null )
$pSource PhpOffice\PhpPresentation\ComparableInterface[] Source array to create HashTable from
    public function addFromSource($pSource = null)
    {
        // Check if an array was passed
        if ($pSource == null) {
            return;
        } elseif (!is_array($pSource)) {
            throw new \Exception('Invalid array parameter passed.');
        }
        foreach ($pSource as $item) {
            $this->add($item);
        }
    }

Usage Example

Example #1
0
 /**
  * Save PhpPresentation to file
  *
  * @param  string    $pFilename
  * @throws \Exception
  */
 public function save($pFilename)
 {
     if (empty($pFilename)) {
         throw new \Exception("Filename is empty");
     }
     if (empty($this->presentation)) {
         throw new \Exception("PhpPresentation object unassigned");
     }
     // If $pFilename is php://output or php://stdout, make it a temporary file...
     $originalFilename = $pFilename;
     if (strtolower($pFilename) == 'php://output' || strtolower($pFilename) == 'php://stdout') {
         $pFilename = @tempnam('./', 'phppttmp');
         if ($pFilename == '') {
             $pFilename = $originalFilename;
         }
     }
     // Create drawing dictionary
     $this->drawingHashTable->addFromSource($this->allDrawings());
     $oZip = $this->getZipAdapter();
     $oZip->open($pFilename);
     $oDir = new DirectoryIterator(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PowerPoint2007');
     foreach ($oDir as $oFile) {
         if (!$oFile->isFile()) {
             continue;
         }
         $class = __NAMESPACE__ . '\\PowerPoint2007\\' . $oFile->getBasename('.php');
         $o = new \ReflectionClass($class);
         if ($o->isAbstract() || !$o->isSubclassOf('PhpOffice\\PhpPresentation\\Writer\\PowerPoint2007\\AbstractDecoratorWriter')) {
             continue;
         }
         $oService = $o->newInstance();
         $oService->setZip($oZip);
         $oService->setPresentation($this->presentation);
         $oService->setDrawingHashTable($this->drawingHashTable);
         $oZip = $oService->render();
         unset($oService);
     }
     // Close file
     $oZip->close();
     // If a temporary file was used, copy it to the correct file stream
     if ($originalFilename != $pFilename) {
         if (copy($pFilename, $originalFilename) === false) {
             throw new \Exception("Could not copy temporary zip file {$pFilename} to {$originalFilename}.");
         }
         if (@unlink($pFilename) === false) {
             throw new \Exception('The file ' . $pFilename . ' could not be removed.');
         }
     }
 }
All Usage Examples Of PhpOffice\PhpPresentation\HashTable::addFromSource