/** * @inheritdoc */ public function toDictionary($keySelector, $valueSelector = null) { $result = new Dictionary(); $keyFunc = $this->resolveFunction($keySelector); $valueFunc = $this->resolveFunction($valueSelector); foreach ($this as $item) { $key = $keyFunc($item); if ($result->containsKey($key)) { throw new LogicException(sprintf('Key selection produces duplicated elements "%s".', $key)); } $result->add($keyFunc($item), $valueFunc($item)); } return $result; }
public function testAdd() { $dictionary = new Dictionary(array('a' => 1, 'c' => 3)); $dictionary->add('b', 2); $this->assertEquals(array('a', 'c', 'b'), $dictionary->keys()); $this->assertEquals(array(1, 3, 2), $dictionary->values()); }