Completed
Push — develop ( b53258...702238 )
by Tony
15:27
created

Util::isJson()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
rs 9.4285
c 1
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * Util.php
4
 *
5
 * Common Utility functions
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 * @package    LibreNMS
21
 * @link       http://librenms.org
22
 * @copyright  2016 Tony Murray
23
 * @author     Tony Murray <[email protected]>
24
 */
25
26
namespace App;
27
28
29
class Util
30
{
31
    /**
32
     * Merge arrays, concatenate the values of any common keys
33
     *
34
     * @param array ...
35
     * @return array
36
     */
37
    public static function arrayMergeConcat()
38
    {
39
        $out = [];
40
        // loop through the arguments
41
        foreach (func_get_args() as $arr) {
42
43
            // loop through each array
44
            foreach ($arr as $key => $value) {
45
                // If the same key exists in the $out array
46
                if (array_key_exists($key, $out)) {
47
                    // concat the values
48
                    $out[$key] = $out[$key].$arr[$key];
49
                }
50
                else {
51
                    $out[$key] = $arr[$key];
52
                }
53
            }
54
        }
55
        return $out;
56
    }
57
58
    /**
59
     * Checks if the given variable is a json encoded string.
60
     *
61
     * @param string $string The string to check
62
     * @return bool true if this is json, otherwise false
63
     */
64
    public static function isJson($string)
65
    {
66
        if (!is_string($string)) {
67
            return false;
68
        }
69
        json_decode($string, true);
70
        return (json_last_error() == JSON_ERROR_NONE);
71
    }
72
}
73