Passed
Push — main ( 72c6fa...9344f0 )
by Sílvio
02:59
created

CacheFileHelper::validateCacheItem()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 3
eloc 2
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Helpers;
4
5
use Silviooosilva\CacheerPhp\Exceptions\CacheFileException;
6
7
/**
8
 * Class CacheFileHelper
9
 * @author Sílvio Silva <https://github.com/silviooosilva>
10
 * @package Silviooosilva\CacheerPhp
11
 */
12
class CacheFileHelper
13
{
14
15
    /**
16
    * @param string $expiration
17
    * @return int
18
    */
19
    public static function convertExpirationToSeconds(string $expiration)
20
    {
21
        $units = [
22
            'second' => 1,
23
            'minute' => 60,
24
            'hour'   => 3600,
25
            'day'    => 86400,
26
            'week'   => 604800,
27
            'month'  => 2592000,
28
            'year'   => 31536000,
29
        ];
30
        foreach ($units as $unit => $value) {
31
            if (strpos($expiration, $unit) !== false) {
32
                return (int)$expiration * $value;
33
            }
34
        }
35
        throw CacheFileException::create("Invalid expiration format");
36
    }
37
38
    /**
39
     * @param array $options
40
     * @return array
41
     */
42
    public static function mergeCacheData($cacheData)
43
    {
44
        if (is_array($cacheData) && is_array(reset($cacheData))) {
45
            $merged = [];
46
            foreach ($cacheData as $data) {
47
                $merged[] = $data;
48
            }
49
            return $merged;
50
        }
51
        return (array)$cacheData;
52
    }
53
54
    /**
55
     * @param array $item
56
     * @return void
57
     */
58
    public static function validateCacheItem(array $item)
59
    {
60
        if (!isset($item['cacheKey']) || !isset($item['cacheData'])) {
61
            throw CacheFileException::create("Each item must contain 'cacheKey' and 'cacheData'");
62
        }
63
    }
64
65
    /**
66
    * @param string|int $ttl
67
    * @param int $defaultTTL
68
    * @return mixed
69
    */
70
    public static function ttl(string|int $ttl = null, int $defaultTTL = null) {
71
        if ($ttl) {
72
            $ttl = is_string($ttl) ? CacheFileHelper::convertExpirationToSeconds($ttl) : $ttl;
73
        } else {
74
            $ttl = $defaultTTL;
75
        }
76
        return $ttl;
77
    }
78
79
  /**
80
  * @param mixed $currentCacheData
81
  * @param mixed $cacheData
82
  * @return array
83
  */
84
  public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData)
85
  {
86
    if (is_array($currentCacheData) && is_array($cacheData)) {
87
      $mergedCacheData = array_merge($currentCacheData, $cacheData);
88
    } else {
89
      $mergedCacheData = array_merge((array)$currentCacheData, (array)$cacheData);
90
    }
91
92
    return $mergedCacheData;
93
  }
94
}
95