/** * Inserts an assoc element to container * * @param string $key * @param mixed $value * @param integer $pos * * @return DataContainer * * @throws RuntimeException */ public function insertAssoc($key, $value, $pos = null) { if ($pos === null) { $this->set($key, $value); } else { if ($this->readOnly) { throw new \RuntimeException('Changing values on this Data Container is not allowed.'); } $this->isModified = true; if (abs($pos) > count($this->data)) { $pos = count($this->data) * min(1, max(-1, $pos)); } if ($key === null) { Arr::insert($this->data, $value, $pos); } else { Arr::insertAssoc($this->data, array($key => $value), $pos); } } return $this; }
/** * @covers Fuel\Common\Arr::insertAssoc * @group Common */ public function testInsertAssoc() { $character = array("name" => "Jack", "surname" => "Reacher"); $expected = array("name" => "Jack", "initial" => "P.", "surname" => "Reacher"); $result = Arr::insertAssoc($character, array("initial" => "P."), 1); $this->assertEquals($expected, $character); $this->assertTrue($result); $character = array("name" => "Jack", "surname" => "Reacher"); $expected = array("name" => "Jack", "surname" => "Reacher"); $result = Arr::insertAssoc($character, array("initial" => "P."), 5); $this->assertEquals($expected, $character); $this->assertFalse($result); }