Prado\Collections\TPriorityList::insertAtIndexInPriority PHP Метод

insertAtIndexInPriority() публичный Метод

Inserts an item at the specified index within a priority. Override and call this method to insert your own functionality.
public insertAtIndexInPriority ( $item, $index = false, $priority = null, $preserveCache = false )
    public function insertAtIndexInPriority($item, $index = false, $priority = null, $preserveCache = false)
    {
        if ($this->getReadOnly()) {
            throw new TInvalidOperationException('list_readonly', get_class($this));
        }
        if ($priority === null) {
            $priority = $this->getDefaultPriority();
        }
        $priority = (string) round(TPropertyValue::ensureFloat($priority), $this->_p);
        if ($preserveCache) {
            $this->sortPriorities();
            $cc = 0;
            foreach ($this->_d as $prioritykey => $items) {
                if ($prioritykey >= $priority) {
                    break;
                } else {
                    $cc += count($items);
                }
            }
            if ($index === false && isset($this->_d[$priority])) {
                $c = count($this->_d[$priority]);
                $c += $cc;
                $this->_d[$priority][] = $item;
            } else {
                if (isset($this->_d[$priority])) {
                    $c = $index + $cc;
                    array_splice($this->_d[$priority], $index, 0, array($item));
                } else {
                    $c = $cc;
                    $this->_o = false;
                    $this->_d[$priority] = array($item);
                }
            }
            if ($this->_fd && is_array($this->_fd)) {
                // if there is a flattened array cache
                array_splice($this->_fd, $c, 0, array($item));
            }
        } else {
            $c = null;
            if ($index === false && isset($this->_d[$priority])) {
                $cc = count($this->_d[$priority]);
                $this->_d[$priority][] = $item;
            } else {
                if (isset($this->_d[$priority])) {
                    $cc = $index;
                    array_splice($this->_d[$priority], $index, 0, array($item));
                } else {
                    $cc = 0;
                    $this->_o = false;
                    $this->_d[$priority] = array($item);
                }
            }
            if ($this->_fd && is_array($this->_fd) && count($this->_d) == 1) {
                array_splice($this->_fd, $cc, 0, array($item));
            } else {
                $this->_fd = null;
            }
        }
        $this->_c++;
        return $c;
    }

Usage Example

Пример #1
0
 public function testCanNotInsertAtIndexInPriorityWhenReadOnlyTList()
 {
     $list = new TPriorityList(array(), true);
     try {
         $list->insertAtIndexInPriority(1);
         self::fail('An expected TInvalidOperationException was not raised');
     } catch (TInvalidOperationException $e) {
     }
 }