Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
sanitizer
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 sanitize
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
2
 decode
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 *
4 * This file is part of the phpBB Forum Software package.
5 *
6 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7 * @license GNU General Public License, version 2 (GPL-2.0)
8 *
9 * For full copyright and license information, please see
10 * the docs/CREDITS.txt file.
11 *
12 */
13
14declare(strict_types=1);
15
16namespace phpbb\json;
17
18use phpbb\request\type_cast_helper;
19
20/**
21 * JSON sanitizer class
22 */
23class sanitizer
24{
25    /**
26     * Sanitize json data
27     *
28     * @param array $data Data to sanitize
29     *
30     * @return array Sanitized data
31     */
32    public static function sanitize(array $data) : array
33    {
34        if (!empty($data))
35        {
36            $json_sanitizer = function (&$value)
37            {
38                $type_cast_helper = new type_cast_helper();
39                $type_cast_helper->set_var($value, $value, gettype($value), true);
40            };
41            array_walk_recursive($data, $json_sanitizer);
42        }
43
44        return $data;
45    }
46
47    /**
48     * Decode and sanitize json data
49     *
50     * @param string $json JSON data string
51     *
52     * @return array Data array
53     */
54    public static function decode(string $json) : array
55    {
56        $data = json_decode($json, true);
57        return !empty($data) ? self::sanitize($data) : [];
58    }
59}