CacheDataFormatter   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
dl 0
loc 58
rs 10
c 1
b 0
f 0
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A toString() 0 3 1
A toArray() 0 3 1
A toObject() 0 3 1
A toJson() 0 7 1
1
<?php
2
3
namespace Silviooosilva\CacheerPhp\Utils;
4
5
/**
6
 * Class CacheDataFormatter
7
 * @author Sílvio Silva <https://github.com/silviooosilva>
8
 * @package Silviooosilva\CacheerPhp
9
 */
10
class CacheDataFormatter
11
{
12
    /** @param mixed $data */
13
    private mixed $data;
14
15
    /**
16
    * CacheDataFormatter constructor.
17
    *
18
    * @param mixed $data
19
    */
20
    public function __construct(mixed $data)
21
    {
22
        $this->data = $data;
23
    }
24
25
    /**
26
    * Converts the data to JSON format.
27
    *
28
    * @return string|false
29
    */
30
    public function toJson()
31
    {
32
        return json_encode(
33
            $this->data,
34
            JSON_PRETTY_PRINT |
35
                JSON_UNESCAPED_UNICODE |
36
                JSON_UNESCAPED_SLASHES
37
        );
38
    }
39
40
    /**
41
    * Converts the data to an array.
42
    * 
43
    * @return array
44
    */
45
    public function toArray()
46
    {
47
        return (array)$this->data;
48
    }
49
50
    /**
51
    * Converts the data to a string.
52
    * 
53
    * @return string
54
    */
55
    public function toString()
56
    {
57
        return (string)$this->data;
58
    }
59
60
    /**
61
    * Converts the data to an object.
62
    * 
63
    * @return object
64
    */
65
    public function toObject()
66
    {
67
        return (object)$this->data;
68
    }
69
}
70