PhpOffice\PhpPresentation\HashTable::add PHP Method

add() public method

Add HashTable item
public add ( ComparableInterface $pSource )
$pSource ComparableInterface Item to add
    public function add(ComparableInterface $pSource)
    {
        // Determine hashcode
        $hashIndex = $pSource->getHashIndex();
        if (is_null($hashIndex)) {
            $hashCode = $pSource->getHashCode();
        } elseif (isset($this->keyMap[$hashIndex])) {
            $hashCode = $this->keyMap[$hashIndex];
        } else {
            $hashCode = $pSource->getHashCode();
        }
        // Add value
        if (!isset($this->items[$hashCode])) {
            $this->items[$hashCode] = $pSource;
            $index = count($this->items) - 1;
            $this->keyMap[$index] = $hashCode;
            $pSource->setHashIndex($index);
        } else {
            $pSource->setHashIndex($this->items[$hashCode]->getHashIndex());
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  */
 public function testRemove()
 {
     $object = new HashTable();
     $oSlide1 = new Slide();
     $oSlide2 = new Slide();
     $oSlide3 = new Slide();
     // Add Object
     $this->assertNull($object->add($oSlide1));
     $this->assertNull($object->add($oSlide2));
     $this->assertNull($object->add($oSlide3));
     // Remove
     $this->assertNull($object->remove($oSlide2));
     $this->assertCount(2, $object->toArray());
     $this->assertNull($object->remove($oSlide3));
     $this->assertCount(1, $object->toArray());
 }