Completed
Push — master ( 301d75...7b46e5 )
by Russell
02:43
created

JSONBackend::matchOnExpr()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 3
nop 0
1
<?php
2
3
/**
4
 * DB backend for use with the {@link JSONText} DB field. Allows us to use DB-specific JSON query syntax within
5
 * the module.
6
 * 
7
 * @package silverstripe-jsontext
8
 * @subpackage models
9
 * @author Russell Michell <[email protected]>
10
 * @see https://github.com/Peekmo/JsonPath/blob/master/tests/JsonStoreTest.php
11
 * @see http://goessner.net/articles/JsonPath/
12
 */
13
14
namespace JSONText\Backends;
15
16
use JSONText\Exceptions\JSONTextException;
17
use JSONText\Fields\JSONText;
18
19
abstract class JSONBackend
20
{
21
    
22
    /**
23
     * @var string
24
     */
25
    protected $operand;
26
27
    /**
28
     * @var JSONText
29
     */
30
    protected $jsonText;
31
32
    /**
33
     * JSONBackend constructor.
34
     *
35
     * @param string $operand
36
     * @param JSONText $jsonText
37
     */
38
    public function __construct($operand, $jsonText)
39
    {
40
        $this->operand = $operand;
41
        $this->jsonText = $jsonText;
42
    }
43
    
44
    /**
45
     * Match on keys by INT. If >1 matches are found, an indexed array of all matches is returned.
46
     *
47
     * @return array
48
     * @throws JSONTextException
49
     */
50
    abstract public function matchOnInt();
51
52
    /**
53
     * Match on keys by STRING. If >1 matches are found, an indexed array of all matches is returned.
54
     *
55
     * @return array
56
     * @throws JSONTextException
57
     */
58
    abstract function matchOnStr();
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
60
    /**
61
     * Match on RDBMS-specific path operator. If >1 matches are found, an indexed array of all matches is returned.
62
     *
63
     * @return array
64
     * @throws \JSONText\Exceptions\JSONTextException
65
     */
66
    abstract public function matchOnPath();
67
68
    /**
69
     * Match on JSONPath expression. If >1 matches are found, an indexed array of all matches is returned.
70
     *
71
     * @return array
72
     * @throws JSONTextException
73
     */
74
    public function matchOnExpr()
75
    {
76
        if (!is_string($this->operand)) {
77
            $msg = 'Non-string passed to: ' . __FUNCTION__;
78
            throw new JSONTextException($msg);
79
        }
80
81
        // Re-use existing field passed via constructor
82
        $expr = $this->operand;
83
        $fetch = $this->jsonText->getJSONStore()->get($expr);
84
        if (!$fetch) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $fetch of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
            return [];
86
        }
87
88
        return $fetch;
89
    }
90
    
91
}
92