Skip to content

Commit

Permalink
Add missing methods
Browse files Browse the repository at this point in the history
  • Loading branch information
MrEssex committed Feb 9, 2022
1 parent 59ae523 commit e4d51dc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* Class AbstractCache
*
* @package MrEssex\FileCache
*/
abstract class AbstractCache implements CacheInterface
Expand Down Expand Up @@ -64,11 +65,11 @@ protected function _validateKey($key): bool
}

/**
* @param int|null|string $ttl
* @param int|null|DateTime|DateInterval|string $ttl
*
* @return int
*/
protected function _expirationToTimestamp(?int $ttl): int
protected function _expirationToTimestamp($ttl): int
{
if($ttl instanceof DateInterval)
{
Expand Down Expand Up @@ -105,7 +106,19 @@ protected function _expirationToTimestamp(?int $ttl): int
*/
public function getMultiple($keys, $default = null)
{
// TODO: Implement getMultiple() method.
if(!is_array($keys) && !$keys instanceof \Traversable)
{
throw InvalidArgumentException::invalidTraversableArgument();
}

$values = [];

foreach($keys as $key)
{
$values[$key] = $this->get($key, $default);
}

return $values;
}

/**
Expand All @@ -124,7 +137,19 @@ public function getMultiple($keys, $default = null)
*/
public function setMultiple($values, $ttl = null)
{
// TODO: Implement setMultiple() method.
if(!is_array($values) && !$values instanceof \Traversable)
{
throw InvalidArgumentException::invalidTraversableArgument();
}

$success = true;

foreach($values as $key => $value)
{
$success = $this->set($key, $value) && $success;
}

return $success;
}

/**
Expand All @@ -140,6 +165,18 @@ public function setMultiple($values, $ttl = null)
*/
public function deleteMultiple($keys)
{
// TODO: Implement deleteMultiple() method.
if(!is_array($keys) && !$keys instanceof \Traversable)
{
throw InvalidArgumentException::invalidTraversableArgument();
}

$success = true;

foreach($keys as $key)
{
$success = $this->delete($key) && $success;
}

return $success;
}
}
10 changes: 10 additions & 0 deletions src/Exceptions/InvalidArgumentException.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,14 @@ public static function failedToCache(): InvalidArgumentException
);
}

/**
* @return InvalidArgumentException
*/
public static function invalidTraversableArgument(): InvalidArgumentException
{
return new self(
"Argument 1 must be an array or a Traversable", 500
);
}

}
1 change: 1 addition & 0 deletions src/FileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

/**
* Class FileCache
*
* @package MrEssex\FileCache
*/
class FileCache extends AbstractCache
Expand Down

0 comments on commit e4d51dc

Please sign in to comment.