/** @test */ public function setAssignsNewValue() { $map = new Map(array('foo' => 'bar')); $map->set('foo', 'baz'); $this->assertEquals('baz', $map->get('foo')); $map->set('ping', 'pong'); $this->assertEquals('pong', $map->get('ping')); }
/** * In PHP, float and boolean keys are truncated to integers, thus if you * want to store $a[1] and $a[1.0], you can't, without hashing the key * yourself. Map does not behave this way: these are distinct keys. * * @see http://php.net/manual/en/language.types.array.php */ public function test_key_normalization() { $map = new Map(); $map->set(true, 'foo'); $map->set(1, 'bar'); $map->set(1.0, 'baz'); $this->assertCount(3, $map); }
/** * Loads the list from a sql query. * * @param Recipe_Database_Statement_Abstract $result Result set * @param integer $start Rank/Counter start * * @return Bengine_Game_Alliance_List */ public function load($result, $start = 0) { foreach ($result->fetchAll() as $row) { $row["counter"] = $start; $start++; $row["rank"] = $start; $row = $this->formatRow($row); $key = !is_null($this->key) ? (int) $row[$this->key] : $start; $this->list->set($key, $row); } $result->closeCursor(); return $this; }
/** * Set the value at a given key, provided that the value passes the * defined guard. Optionally, normalize the value before setting. * * {@inheritDoc} * @throws \UnexpectedValueException */ public function set($key, $value) { $result = $this->allowed($value); if ($this->passes($result)) { parent::set($key, $this->normalize($value)); } else { throw new \UnexpectedValueException(sprintf('Value of type "%s" forbidden in this instance of %s', gettype($value), get_class($this))); } }
private static function toCollection($data) { if (is_object($data)) { $map = new Map(); foreach ($data as $k => $v) { if (is_object($v) || is_array($v)) { $map->set($k, self::toCollection($v)); } else { $map->set($k, $v); } } return $map; } else { if (is_array($data)) { $list = new ArrayList(); foreach ($data as $v) { $list->add(is_object($v) || is_array($v) ? self::toCollection($v) : $v); } return $list; } } }
/** * @dataProvider provider__construct */ public function test_set($base, $diff) { $map = new Map($base); foreach ($base as $key => $value) { $this->assertEquals($map->get($key), $value); } foreach ($diff as $key => $value) { $map->set($key, $value); $base[$key] = $value; } foreach ($base as $key => $value) { $this->assertEquals($map->get($key), $value); } }
/** * Put something in the map * * @param int|string $key The key. Strings that are ints are cast to ints. * @param mixed $value The value to cache * * @return LRUMap */ public function set($key, $value) { if (parent::get($key)->isDefined()) { parent::set($key, $value); $this->recordAccess($key); } else { parent::set($key, $value); if ($this->length() > $this->maximumSize) { // remove least recently used element (front of array) reset($this->elements); unset($this->elements[key($this->elements)]); } } return $this; }
/** * Loads the alliance relations. * * @return Bengine_Game_User_Relation */ protected function loadAllianceRelations() { if ($this->alliancesLoaded) { return $this; } $select = array("ar.rel1", "ar.rel2", "ar.mode", "rt.acs", "rt.name", "rt.css"); $joins = "LEFT JOIN " . PREFIX . "ally_relationship_type rt ON (rt.type_id = ar.mode)"; $where = Core::getDB()->quoteInto("(ar.rel1 = ? OR ar.rel2 = ?)", $this->aid); $result = Core::getQuery()->select("ally_relationships ar", $select, $joins, $where); foreach ($result->fetchAll() as $row) { $rel = $row["rel1"] == $this->aid ? $row["rel2"] : $row["rel1"]; $data = new stdClass(); $data->mode = $row["mode"]; $data->css = $row["css"]; $data->name = $row["name"]; $data->acs = $row["acs"]; $this->alliances->set($rel, $data); } $result->closeCursor(); $this->alliancesLoaded = true; Hook::event("AllianceRelationsLoaded", array(&$this->alliances)); return $this; }
/** * @param callable $fn * * @return Map */ public function map($fn) { $map = new Map(); foreach ($this->data as $data) { $map->set($data['offset'], $fn($data['value'], $data['offset'])); } return $map; }
public function testKeysLocked() { $map = new Map(['foo6' => 'bar6'], true); $success = false; try { $map->set('foo7', 'bar7'); } catch (\RuntimeException $e) { $success = true; } $this->assertTrue($success); $this->assertTrue($map->remove('foo6')->has('foo6')); $this->assertEquals('bar6', $map->set('foo6', 'bar6')->get('foo6')); $this->assertEquals(['foo6' => null], $map->clear()->toArray()); }
public static function add($a, $b, $options = null) { $out = new Map($a->lines, $a->columns, $options); $map_a = $a->map(); $map_b = $b->map(); $l_start = 0; $c_start = 0; $transparent = false; if (!$options) { $options = $b->options; } if ($options) { foreach ($options as $key => $value) { ${$key} = $value; } } for ($l = $l_start; $l < $a->lines; $l++) { if (!isset($map_b[$l])) { for ($c = $c_start; $c < $a->columns; $c++) { $out->set($l, $c, $map_a[$l][$c]); } } else { for ($c = $c_start; $c < $a->columns; $c++) { if (isset($map_b[$l][$c])) { $bc = $map_b[$l][$c]; if ($bc != $transparent) { $out->set($l, $c, $bc); } else { $out->set($l, $c, $map_a[$l][$c]); } } else { $out->set($l, $c, $map_a[$l][$c]); } } } } return $out; }
/** * Puts the given key-value pair into the map. * @param mixed $key * @param mixed $value * @throws \InvalidArgumentException if $value is not numeric */ public function set($key, $value) { $this->assertIsNumeric($value); parent::set($key, $value); }