PhpOffice\PhpPresentation\AbstractShape::setContainer PHP Method

setContainer() public method

Set Container, Slide or Group
public setContainer ( PhpOffice\PhpPresentation\ShapeContainerInterface $pValue = null, boolean $pOverrideOld = false ) : self
$pValue PhpOffice\PhpPresentation\ShapeContainerInterface
$pOverrideOld boolean If a Slide has already been assigned, overwrite it and remove image from old Slide?
return self
    public function setContainer(ShapeContainerInterface $pValue = null, $pOverrideOld = false)
    {
        if (is_null($this->container)) {
            // Add drawing to \PhpOffice\PhpPresentation\ShapeContainerInterface
            $this->container = $pValue;
            if (!is_null($this->container)) {
                $this->container->getShapeCollection()->append($this);
            }
        } else {
            if ($pOverrideOld) {
                // Remove drawing from old \PhpOffice\PhpPresentation\ShapeContainerInterface
                $iterator = $this->container->getShapeCollection()->getIterator();
                while ($iterator->valid()) {
                    if ($iterator->current()->getHashCode() == $this->getHashCode()) {
                        $this->container->getShapeCollection()->offsetUnset($iterator->key());
                        $this->container = null;
                        break;
                    }
                    $iterator->next();
                }
                // Set new \PhpOffice\PhpPresentation\Slide
                $this->setContainer($pValue);
            } else {
                throw new \Exception("A \\PhpOffice\\PhpPresentation\\ShapeContainerInterface has already been assigned. Shapes can only exist on one \\PhpOffice\\PhpPresentation\\ShapeContainerInterface.");
            }
        }
        return $this;
    }

Usage Example

Example #1
0
 /**
  * Add shape to slide
  *
  * @param  \PhpOffice\PhpPresentation\AbstractShape $shape
  * @return \PhpOffice\PhpPresentation\AbstractShape
  */
 public function addShape(AbstractShape $shape)
 {
     $shape->setContainer($this);
     return $shape;
 }