Example #1
0
    describe("->has()", function () {
        it("returns `true` if a element has been setted", function () {
            $map = new Map();
            $instance = new stdClass();
            expect($map->set($instance, 'Hello'))->toBe($map);
            expect($map->has($instance))->toBe(true);
        });
        it("returns false if a element doesn't exist", function () {
            $map = new Map();
            expect($map->has(new stdClass()))->toBe(false);
        });
    });
    describe("->remove()", function () {
        it("removes items", function () {
            $map = new Map();
            $instance = new stdClass();
            expect($map->set($instance, 'Hello'))->toBe($map);
            expect($map->remove($instance))->toBe($map);
            expect($map->has($instance))->toBe(false);
        });
    });
    describe("->count()", function () {
        it("removes items", function () {
            $map = new Map();
            $instance = new stdClass();
            expect($map->count())->toBe(0);
            expect($map->set($instance, 'Hello'))->toBe($map);
            expect($map->count())->toBe(1);
        });
    });
});
Example #2
0
 /**
  * Indexes a collection.
  *
  * @param  mixed  $collection An collection to extract index from.
  * @param  string $name       The field name to build index for.
  * @return Map                An array of indexes where keys are `$name` values and
  *                            values the corresponding index in the collection.
  */
 protected function _index($collection, $name)
 {
     $indexes = new Map();
     foreach ($collection as $key => $entity) {
         if (is_object($entity)) {
             if ($entity->{$name}) {
                 $indexes->set($entity->{$name}, $key);
             }
         } else {
             if (isset($entity[$name])) {
                 $indexes->set($entity[$name], $key);
             }
         }
     }
     return $indexes;
 }