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

CacheRedisHelper::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\CacheRedisException;
6
7
/**
8
 * Class CacheRedisHelper
9
 * @author Sílvio Silva <https://github.com/silviooosilva>
10
 * @package Silviooosilva\CacheerPhp
11
 */
12
class CacheRedisHelper
13
{
14
15
  /**
16
  * @param mixed $data
17
  * @param bool  $serialize
18
  * @return string
19
  */
20
  public static function serialize(mixed $data, bool $serialize = true)
21
  {
22
    if($serialize) {
23
      return serialize($data);
24
    }
25
26
    return unserialize($data);
27
28
  }
29
30
    /**
31
     * @param array $item
32
     * @return void
33
     */
34
    public static function validateCacheItem(array $item)
35
    {
36
        if (!isset($item['cacheKey']) || !isset($item['cacheData'])) {
37
            throw CacheRedisException::create("Each item must contain 'cacheKey' and 'cacheData'");
38
        }
39
    }
40
41
    /**
42
     * @param array $options
43
     * @return array
44
     */
45
    public static function mergeCacheData($cacheData)
46
    {
47
        if (is_array($cacheData) && is_array(reset($cacheData))) {
48
            $merged = [];
49
            foreach ($cacheData as $data) {
50
                $merged[] = $data;
51
            }
52
            return $merged;
53
        }
54
        return (array)$cacheData;
55
    }
56
57
  /**
58
  * @param mixed $currentCacheData
59
  * @param mixed $cacheData
60
  * @return array
61
  */
62
  public static function arrayIdentifier(mixed $currentCacheData, mixed $cacheData)
63
  {
64
    if (is_array($currentCacheData) && is_array($cacheData)) {
65
      $mergedCacheData = array_merge($currentCacheData, $cacheData);
66
    } else {
67
      $mergedCacheData = array_merge((array)$currentCacheData, (array)$cacheData);
68
    }
69
70
    return $mergedCacheData;
71
  }
72
73
}
74
75