Serialize::getDecodeLocale()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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