Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
91.67% covered (success)
91.67%
11 / 12
83.33% covered (success)
83.33%
5 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
AbstractStorage
91.67% covered (success)
91.67%
11 / 12
83.33% covered (success)
83.33%
5 / 6
7.03
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 setFormat
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 isText
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isPhp
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isJson
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFormat
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 save
n/a
0 / 0
n/a
0 / 0
0
 getById
n/a
0 / 0
n/a
0 / 0
0
 getByType
n/a
0 / 0
n/a
0 / 0
0
 has
n/a
0 / 0
n/a
0 / 0
0
 delete
n/a
0 / 0
n/a
0 / 0
0
 clear
n/a
0 / 0
n/a
0 / 0
0
 encodeValue
n/a
0 / 0
n/a
0 / 0
0
 decodeValue
n/a
0 / 0
n/a
0 / 0
0
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Debug\Storage;
15
16/**
17 * Debug storage abstract class
18 *
19 * @category   Pop
20 * @package    Pop\Debug
21 * @author     Nick Sagona, III <dev@nolainteractive.com>
22 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
23 * @license    http://www.popphp.org/license     New BSD License
24 * @version    2.0.0
25 */
26abstract class AbstractStorage implements StorageInterface
27{
28
29    /**
30     * Format constants
31     */
32    const TEXT = 'TEXT';
33    const JSON = 'JSON';
34    const PHP  = 'PHP';
35
36    /**
37     * Storage format (json, php or text)
38     * @var string
39     */
40    protected string $format = 'TEXT';
41
42    /**
43     * Constructor
44     *
45     * Instantiate the storage object
46     *
47     * @param  ?string $format
48     */
49    public function __construct(?string $format = self::TEXT)
50    {
51        if ($format !== null) {
52            $this->setFormat($format);
53        }
54    }
55
56    /**
57     * Set the storage format
58     *
59     * @param  string $format
60     * @return AbstractStorage
61     */
62    public function setFormat(string $format): AbstractStorage
63    {
64        $this->format = match (strtoupper($format)) {
65            self::JSON => self::JSON,
66            self::PHP  => self::PHP,
67            default    => self::TEXT,
68        };
69
70        return $this;
71    }
72
73    /**
74     * Determine if the format is PHP
75     *
76     * @return bool
77     */
78    public function isText(): bool
79    {
80        return ($this->format == self::TEXT);
81    }
82
83    /**
84     * Determine if the format is PHP
85     *
86     * @return bool
87     */
88    public function isPhp(): bool
89    {
90        return ($this->format == self::PHP);
91    }
92
93    /**
94     * Determine if the format is JSON
95     *
96     * @return bool
97     */
98    public function isJson(): bool
99    {
100        return ($this->format == self::JSON);
101    }
102
103    /**
104     * Get the storage format
105     *
106     * @return ?string
107     */
108    public function getFormat(): ?string
109    {
110        return $this->format;
111    }
112
113    /**
114     * Save debug data
115     *
116     * @param  string $id
117     * @param  mixed  $value
118     * @return void
119     */
120    abstract public function save(string $id, mixed $value): void;
121
122    /**
123     * Get debug data by ID
124     *
125     * @param  string $id
126     * @return mixed
127     */
128    abstract public function getById(string $id): mixed;
129
130    /**
131     * Get debug data by type
132     *
133     * @param  string $type
134     * @return mixed
135     */
136    abstract public function getByType(string $type): mixed;
137
138    /**
139     * Determine if debug data exists by
140     *
141     * @param  string $id
142     * @return bool
143     */
144    abstract public function has(string $id): bool;
145
146    /**
147     * Delete debug data by id
148     *
149     * @param  string $id
150     * @return void
151     */
152    abstract public function delete(string $id): void;
153
154    /**
155     * Clear all debug data
156     *
157     * @return void
158     */
159    abstract public function clear(): void;
160
161    /**
162     * Encode the value based on the format
163     *
164     * @param  mixed  $value
165     * @return string
166     */
167    abstract public function encodeValue(mixed $value): string;
168
169    /**
170     * Decode the value based on the format
171     *
172     * @param  mixed  $value
173     * @return mixed
174     */
175    abstract public function decodeValue(mixed $value): mixed;
176}