Serialize   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 46
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A decode() 0 3 1
A getDecodeLocale() 0 3 1
A encode() 0 3 1
A getDecoded() 0 7 3
1
<?php
2
3
namespace Ffcms\Core\Helper;
4
5
use Ffcms\Core\App;
6
use Ffcms\Core\Helper\Type\Any;
7
use Ffcms\Core\Helper\Type\Obj;
8
9
/**
10
 * Serialization and unsertialization data for database/any storage
11
 * Class Serialize
12
 * @package Ffcms\Core\Helper
13
 */
14
class Serialize
15
{
16
17
    /**
18
     * Serialize any data to special string (save to db/etc)
19
     * @param $data
20
     * @return string
21
     */
22
    public static function encode($data)
23
    {
24
        return serialize($data);
25
    }
26
27
    /**
28
     * Unserialize encoded data from string to object/array/etc. Can return false if $data is not serialized
29
     * @param string $data
30
     * @return array|false
31
     */
32
    public static function decode($data)
33
    {
34
        return @unserialize($data);
35
    }
36
37
    /**
38
     * Decode string $data and get value by key
39
     * @param $data
40
     * @param string $key
41
     * @return string|array|null
42
     */
43
    public static function getDecoded($data, $key)
44
    {
45
        if (!Any::isArray($data)) {
46
            $data = self::decode($data);
47
        }
48
49
        return $data === false ? null : $data[$key];
50
    }
51
52
    /**
53
     * Decode serialized data based on current language as key
54
     * @param $data
55
     * @return array|null|string
56
     */
57
    public static function getDecodeLocale($data)
58
    {
59
        return self::getDecoded($data, App::$Request->getLanguage());
60
    }
61
}
62