private function emitMap(array $input, $composite) { if (count($input) % 2 == 1) { throw new TransitException('Input is not valid transit.'); } $result = []; $i = 0; foreach ($input as $value) { $result[] = $this->handle($value, $i++ % 2 == 0); } $map = new Map($result); return $this->useAssocArrayInsteadOfMap && !$composite ? $map->toAssocArray() : $map; }
use transit\handlers\Handler; use transit\Keyword; use transit\Symbol; use transit\Map; use transit\Set; use transit\Bytes; use transit\URI; use transit\UUID; use transit\Char; //------------------------- // structs // map Assert::exception(function () { new Map(['keyOnly']); }, 'transit\\TransitException'); $m = new Map([true, 'a']); Assert::equal('a', $m[true]); Assert::true(isset($m[true])); Assert::equal([true, 'a'], $m->toArray()); $m[true] = 'b'; Assert::equal('b', $m[true]); Assert::equal([true, 'b'], $m->toArray()); unset($m[true]); Assert::false(isset($m[true])); Assert::equal([], $m->toArray()); Assert::false(isset($m['whatever'])); $m[1] = 'x'; $m[1.0] = 'y'; $m[new Keyword('abc')] = 'z'; Assert::equal([1, 'x', 1.0, 'y', new Keyword('abc'), 'z'], $m->toArray()); Assert::equal('x', $m[1]);