CacheDataFormatter::__construct()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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(): bool|string
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(): array
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(): string
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(): object
66
    {
67
        return (object)$this->data;
68
    }
69
}
70