Prado\Collections\TPriorityMap::remove PHP Method

remove() public method

Removes an item from the map by its key. If no priority, or false, is specified then priority is irrelevant. If null is used as a parameter for priority, then the priority will be the default priority. If a priority is specified, or the default priority is specified, only key-value pairs in that priority will be affected.
public remove ( $key, $priority = false ) : mixed
return mixed the removed value, null if no such key exists.
    public function remove($key, $priority = false)
    {
        if (!$this->_r) {
            if ($priority === null) {
                $priority = $this->getDefaultPriority();
            }
            if ($priority === false) {
                $this->sortPriorities();
                foreach ($this->_d as $priority => $items) {
                    if (array_key_exists($key, $items)) {
                        $value = $this->_d[$priority][$key];
                        unset($this->_d[$priority][$key]);
                        $this->_c--;
                        if (count($this->_d[$priority]) === 0) {
                            unset($this->_d[$priority]);
                            $this->_o = false;
                        }
                        $this->_fd = null;
                        return $value;
                    }
                }
                return null;
            } else {
                $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p);
                if (isset($this->_d[$priority]) && (isset($this->_d[$priority][$key]) || array_key_exists($key, $this->_d[$priority]))) {
                    $value = $this->_d[$priority][$key];
                    unset($this->_d[$priority][$key]);
                    $this->_c--;
                    if (count($this->_d[$priority]) === 0) {
                        unset($this->_d[$priority]);
                        $this->_o = false;
                    }
                    $this->_fd = null;
                    return $value;
                } else {
                    return null;
                }
            }
        } else {
            throw new TInvalidOperationException('map_readonly', get_class($this));
        }
    }

Usage Example

Beispiel #1
0
 public function testCanNotRemoveWhenReadOnly()
 {
     $map = new TPriorityMap(array('key' => 'value'), true);
     try {
         $map->remove('key');
     } catch (TInvalidOperationException $e) {
         return;
     }
     self::fail('An expected TInvalidOperationException was not raised');
 }