DecodeJson   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 1
dl 0
loc 35
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A from() 0 19 3
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright (c) 2017-present Ganbaro Digital Ltd
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 *
13
 *   * Redistributions of source code must retain the above copyright
14
 *     notice, this list of conditions and the following disclaimer.
15
 *
16
 *   * Redistributions in binary form must reproduce the above copyright
17
 *     notice, this list of conditions and the following disclaimer in
18
 *     the documentation and/or other materials provided with the
19
 *     distribution.
20
 *
21
 *   * Neither the names of the copyright holders nor the names of his
22
 *     contributors may be used to endorse or promote products derived
23
 *     from this software without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 * POSSIBILITY OF SUCH DAMAGE.
37
 *
38
 * @category  Libraries
39
 * @package   MessagingPipeline/Operations
40
 * @author    Stuart Herbert <[email protected]>
41
 * @copyright 2017-present Ganbaro Digital Ltd www.ganbarodigital.com
42
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
43
 * @link      http://ganbarodigital.github.io/php-mv-messaging-pipeline
44
 */
45
46
namespace GanbaroDigital\MessagingPipeline\V1\Operations;
47
48
use GanbaroDigital\MessagingPipeline\V1\Exceptions\CannotJsonDecode;
49
50
/**
51
 * robust wrapper around PHP's json_decode() function
52
 */
53
class DecodeJson
54
{
55
    /**
56
     * json_decode(), with added error detection!
57
     *
58
     * @param  string $item
59
     *         the JSON-encoded data, to be decoded
60
     * @param  string $fieldOrVarName
61
     *         what do you call '$item' in your code?
62
     * @return mixed
63
     *         the JSON-decoded data, as a native PHP value
64
     *
65
     * @throws CannotJsonDecode
66
     *         if PHP's json_decode() rejected $item
67
     */
68
    public static function from(string $item, string $fieldOrVarName='$item')
69
    {
70
        // deal with decoding problems
71
        $errorMessage = null;
72
        set_error_handler(function ($errno, $errstr) use (&$errorMessage) {
73
            $errorMessage = $errstr;
74
        });
75
        $retval = json_decode($item);
76
        restore_error_handler();
77
78
        // did we manage to decode it?
79
        if ($errorMessage || !$retval) {
80
            // something went wrong
81
            throw CannotJsonDecode::newFromInputParameter($item, $fieldOrVarName, ['PHP_error' => $errorMessage]);
82
        }
83
84
        // if we get here, all is well
85
        return $retval;
86
    }
87
}