Example #1
0
 /** {@inheritdoc} */
 public function offsetSet($offset, $value)
 {
     if (!is_array($value)) {
         throw new \InvalidArgumentException(self::ERROR_LOOKUP_VALUE_NOT_ARRAY);
     }
     parent::offsetSet($offset, $value);
 }
Example #2
0
 function testMixedOffsets_iteration()
 {
     $o = array(new O(), new O(), new O());
     $d = new D();
     $d[$o[0]] = $o[1];
     $d['a'] = 'b';
     $d[$o[2]] = 1;
     $d[2] = 3;
     $a = array();
     $d->rewind();
     while ($d->valid()) {
         $a[] = a($d->key(), $d->current());
         $d->next();
     }
     $this->assertSame(array(a($o[0], $o[1]), a('a', 'b'), a($o[2], 1), a(2, 3)), $a);
 }
Example #3
0
 /**
  * <p><b>Syntax</b>: toDictionary ([keySelector {{(v, k) ==> key} [, valueSelector {{(v, k) ==> value}]])
  * <p>Creates a {@link Dictionary} from a sequence according to specified key selector and value selector functions.
  * <p>The toDictionary method returns a Dictionary, a one-to-one dictionary that maps keys to values. If the source sequence contains multiple values with the same key, the result dictionary will only contain the latter value.
  * @param callable|null $keySelector {(v, k) ==> key} A function to extract a key from each element. Default: key.
  * @param callable|null $valueSelector {(v, k) ==> value} A transform function to produce a result value from each element. Default: value.
  * @return collections\Dictionary A Dictionary that contains values selected from the input sequence.
  */
 public function toDictionary($keySelector = null, $valueSelector = null)
 {
     $keySelector = Utils::createLambda($keySelector, 'v,k', Functions::$key);
     $valueSelector = Utils::createLambda($valueSelector, 'v,k', Functions::$value);
     $dic = new c\Dictionary();
     foreach ($this as $k => $v) {
         $dic->offsetSet(call_user_func($keySelector, $v, $k), call_user_func($valueSelector, $v, $k));
     }
     return $dic;
 }