Skip to content

Commit

Permalink
Make sure we pass all integration tests (#133)
Browse files Browse the repository at this point in the history
* Make sure we pass all integration tests

* cs
  • Loading branch information
Nyholm authored Feb 9, 2017
1 parent 9b70241 commit 19c817a
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions SimpleCacheBridge.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,17 @@ public function getMultiple($keys, $default = null)
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}

return $this->generateValues($default, $items);
}

/**
* @param $default
* @param $items
*
* @return \Generator
*/
private function generateValues($default, $items)
{
foreach ($items as $key => $item) {
/** @type $item CacheItemInterface */
if (!$item->isHit()) {
Expand All @@ -132,14 +143,29 @@ public function setMultiple($values, $ttl = null)
if (!$values instanceof \Traversable) {
throw new InvalidArgumentException('$values is neither an array nor Traversable');
}
}

// Since we need to throw an exception if *any* key is invalid, it doesn't
// make sense to wrap iterators or something like that.
$values = iterator_to_array($values, false);
$keys = [];
$arrayValues = [];
foreach ($values as $key => $value) {
if (is_int($key)) {
$key = (string) $key;
}

if (!is_string($key)) {
throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', gettype($key)));
}

if (preg_match('|[\{\}\(\)/\\\@\:]|', $key)) {
throw new InvalidArgumentException(sprintf('Invalid key: "%s". The key contains one or more characters reserved for future extension: {}()/\@:', $key));
}

$keys[] = $key;
$arrayValues[$key] = $value;
}

try {
$items = $this->cacheItemPool->getItems(array_keys($values));
$items = $this->cacheItemPool->getItems($keys);
} catch (CacheInvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
}
Expand All @@ -148,7 +174,7 @@ public function setMultiple($values, $ttl = null)

foreach ($items as $key => $item) {
/* @var $item CacheItemInterface */
$item->set($values[$key]);
$item->set($arrayValues[$key]);

try {
$item->expiresAfter($ttl);
Expand Down

0 comments on commit 19c817a

Please sign in to comment.