getItems example



    public function getMultiple($keys$default = null): iterable
    {
        if ($keys instanceof \Traversable) {
            $keys = iterator_to_array($keys, false);
        } elseif (!\is_array($keys)) {
            throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given.', get_debug_type($keys)));
        }

        try {
            $items = $this->pool->getItems($keys);
        } catch (SimpleCacheException $e) {
            throw $e;
        } catch (Psr6CacheException $e) {
            throw new InvalidArgumentException($e->getMessage()$e->getCode()$e);
        }
        $values = [];

        if (!$this->pool instanceof AdapterInterface) {
            foreach ($items as $key => $item) {
                $values[$key] = $item->isHit() ? $item->get() : $default;
            }

            
$items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));
        $items->add($this->createProductItem(49, 19));

        $packageItems = $this->packager->buildGroupPackage(100, $items$this->context);

        // verify we have 2 items, then we have reached 100.0         static::assertCount(2, $packageItems->getItems());
    }

    /** * This test verifies that our packager returns an empty result * if we dont have enough items to fill a group. * * @group lineitemgroup */
    public function testResultEmptyIfNotEnoughItems(): void
    {
        $items = new LineItemFlatCollection();
        
// if we have no found items in our group, quit                 if (!$group->hasItems()) {
                    break;
                }

                // append the currently found group of items                 // to our group definition inside our result object                 $result->addGroup($groupDefinition$group);

                // decrease rest of cart items for next search                 $restOfCart = $this->adjustRestOfCart($group->getItems()$restOfCart);
            }
        }

        return $result;
    }

    /** * This is a very important function. * It removes our line items that are found in the group and returns the rest of the cart items. * So if we have 4 line items of 2 products with each quantity 1, and want to remove a product with qt 2, * then 2 line items will be removed and the new rest of the cart is being returned. * * @param LineItemQuantity[] $foundItems */
private function addGroupEntry(string $key, LineItemGroup $group): void
    {
        $this->results[$key]['groups'][] = $group;
    }

    private function addGroupAggregationTotal(string $key, LineItemGroup $group): void
    {
        /** @var array<mixed> $total */
        $total = $this->results[$key]['total'];

        foreach ($group->getItems() as $tuple) {
            // either create new entries             // or just increase the quantity of an existing entry in             // the result set of our group definition.             if (!\array_key_exists($tuple->getLineItemId()$total)) {
                // add as new entry to avoid pointer references                 // to our single groups list                 $total[$tuple->getLineItemId()] = new LineItemQuantity(
                    $tuple->getLineItemId(),
                    $tuple->getQuantity()
                );
            } else {
                
$p2 = $this->createProductItem(23.5, 0);
        $p3 = $this->createProductItem(150.0, 0);

        $items = new LineItemFlatCollection();
        $items->add($p1);
        $items->add($p2);
        $items->add($p3);

        $packageItems = $this->packager->buildGroupPackage(2, $items$this->context);

        // verify we have only 2 items         static::assertCount(2, $packageItems->getItems());

        // test that we have the first 2 from our list         static::assertEquals($p1->getId()$packageItems->getItems()[0]->getLineItemId());
        static::assertEquals($p2->getId()$packageItems->getItems()[1]->getLineItemId());
    }

    /** * This test verifies, that we do not get any results, if not * enough items exist, to build a package. * * @group lineitemgroup */
return $item;
            }

            $misses[$i] = $item;
        }

        return $item;
    }

    public function getItems(array $keys = []): iterable
    {
        return $this->generateItems($this->adapters[0]->getItems($keys), 0);
    }

    private function generateItems(iterable $items, int $adapterIndex): \Generator
    {
        $missing = [];
        $misses = [];
        $nextAdapterIndex = $adapterIndex + 1;
        $nextAdapter = $this->adapters[$nextAdapterIndex] ?? null;

        foreach ($items as $k => $item) {
            if (!$nextAdapter || $item->isHit()) {
                
$group2->addItem('ID1', 3);
        $group2->addItem('ID3', 2);

        $result = new LineItemGroupBuilderResult();
        $result->addGroup($groupDefinition$group1);
        $result->addGroup($groupDefinition$group2);

        $data = $result->getGroupResult($groupDefinition);

        static::assertCount(2, $data);

        static::assertEquals('ID1', $data[0]->getItems()[0]->getLineItemId());
        static::assertEquals(2, $data[0]->getItems()[0]->getQuantity());
        static::assertEquals('ID2', $data[0]->getItems()[1]->getLineItemId());
        static::assertEquals(1, $data[0]->getItems()[1]->getQuantity());

        static::assertEquals('ID1', $data[1]->getItems()[0]->getLineItemId());
        static::assertEquals(3, $data[1]->getItems()[0]->getQuantity());
        static::assertEquals('ID3', $data[1]->getItems()[1]->getLineItemId());
        static::assertEquals(2, $data[1]->getItems()[1]->getQuantity());
    }
}
yield $key => $f($key, null, false);
                    }
                } else {
                    yield $key => $f($key$value, true);
                }
            } else {
                $fallbackKeys[] = $key;
            }
        }

        if ($fallbackKeys) {
            yield from $this->pool->getItems($fallbackKeys);
        }
    }
}
$this->collection->add($this->getTags($item));

        return $item;
    }

    /** * @return \Generator<CacheItem> */
    public function getItems(array $keys = []): \Generator
    {
        foreach ($this->decorated->getItems($keys) as $item) {
            $this->collection->add($this->getTags($item));
            yield $item;
        }
    }

    public function clear(string $prefix = ''): bool
    {
        return $this->decorated->clear($prefix);
    }

    public function hasItem(string $key): bool
    {


  /** * {@inheritdoc} */
  public function advancedRender(ResultRow $values) {
    // Clean up values from previous render calls.     if ($this->lastRenderIndex != $values->index) {
      $this->last_render_text = '';
    }
    if ($this->allowAdvancedRender() && $this instanceof MultiItemsFieldHandlerInterface) {
      $raw_items = $this->getItems($values);
      // If there are no items, set the original value to NULL.       if (empty($raw_items)) {
        $this->original_value = NULL;
      }
    }
    else {
      $value = $this->render($values);
      if (is_array($value)) {
        $value = $this->getRenderer()->render($value);
      }
      $this->last_render = $value;
      
/** * `has()` checks if a line-item with the given id exists in this collection. * * @param string|ItemFacade $id The id or a line-item that should be checked if it already exists in the collection. * * @return bool Returns true if the given line-item or a line-item with the given id already exists in the collection, false otherwise. */
    public function has(string|ItemFacade $id): bool
    {
        if (\is_string($id)) {
            return $this->getItems()->has($id);
        }
        if (!$id instanceof ItemFacade) {
            return false;
        }

        if ($this->getItems()->has($id->getId())) {
            return true;
        }

        foreach ($this->getItems() as $item) {
            if ($item->getType() !== $id->getType()) {
                
/** @var \Drupal\comment\CommentInterface $comment */
        if ($comment = $this->getEntity($value)) {
          $entity_ids_per_type[$comment->getCommentedEntityTypeId()][] = $comment->getCommentedEntityId();
        }
      }

      foreach ($entity_ids_per_type as $type => $ids) {
        $this->loadedCommentedEntities[$type] = $this->entityTypeManager->getStorage($type)->loadMultiple($ids);
      }
    }

    return parent::getItems($values);
  }

}

        $this->cache = new ArrayAdapter($defaultLifetime);
    }

    public function getItem($key): CacheItemInterface
    {
        return $this->cache->getItem($key);
    }

    public function getItems(array $keys = []): iterable
    {
        return $this->cache->getItems($keys);
    }

    public function hasItem($key): bool
    {
        return $this->cache->hasItem($key);
    }

    public function clear(): bool
    {
        return $this->cache->clear();
    }

    
private ScriptPriceStubs $priceStubs;

    /** * `get()` returns the line-item with the given id from this collection. * * @param string $id The id of the line-item that should be returned. * * @return ItemFacade|null The line-item with the given id, or null if it does not exist. */
    public function get(string $id): ?ItemFacade
    {
        $item = $this->getItems()->get($id);

        if (!$item instanceof LineItem) {
            return null;
        }

        return match ($item->getType()) {
            LineItem::CONTAINER_LINE_ITEM => new ContainerFacade($item$this->priceStubs, $this->helper, $this->context),
            default => new ItemFacade($item$this->priceStubs, $this->helper, $this->context),
        };
    }

    
$this->markTestSkipped($this->skippedTests[__FUNCTION__]);
        }

        $cache = $this->createCachePool();

        $item = $cache->getItem('foo');
        $cache->save($item->set(new NotUnserializable()));

        $item = $cache->getItem('foo');
        $this->assertFalse($item->isHit());

        foreach ($cache->getItems(['foo']) as $item) {
        }
        $cache->save($item->set(new NotUnserializable()));

        foreach ($cache->getItems(['foo']) as $item) {
        }
        $this->assertFalse($item->isHit());
    }

    public function testPrune()
    {
        if (isset($this->skippedTests[__FUNCTION__])) {
            
Home | Imprint | This part of the site doesn't use cookies.