Completed
Push — master ( c5ed18...ed0fce )
by Nathan
07:36
created

QJUtils::recursiveQuote()   C

Complexity

Conditions 7
Paths 7

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 6.7272
c 0
b 0
f 0
cc 7
eloc 17
nc 7
nop 1
1
<?php
2
3
namespace Symbiote\QueuedJobs;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Core\Injector\Injector;
7
use SilverStripe\ORM\DB;
8
9
/**
10
 * A set of utility functions used by the queued jobs module
11
 *
12
 * @license http://silverstripe.org/bsd-license
13
 * @author Marcus Nyeholt <[email protected]>
14
 */
15
class QJUtils
16
{
17
    /**
18
     * Quote up a filter of the form
19
     *
20
     * array ("ParentID =" => 1)
21
     *
22
     * @param array $filter
23
     * @param string $join
24
     * @return string
25
     */
26
    public function dbQuote($filter = array(), $join = " AND ")
27
    {
28
        $quoteChar = defined(DB::class . '::USE_ANSI_SQL') && DB::USE_ANSI_SQL  ? '"' : '';
29
30
        $string = '';
31
        $sep = '';
32
33
        foreach ($filter as $field => $value) {
34
            // first break the field up into its two components
35
            $operator = '';
36
            if (is_string($field)) {
37
                list($field, $operator) = explode(' ', trim($field));
38
            }
39
40
            $value = $this->recursiveQuote($value);
41
42
            if (strpos($field, '.')) {
43
                list($tb, $fl) = explode('.', $field);
44
                $string .= $sep . $quoteChar . $tb . $quoteChar . '.' . $quoteChar . $fl . $quoteChar . " $operator " . $value;
45
            } else {
46
                if (is_numeric($field)) {
47
                    $string .= $sep . $value;
48
                } else {
49
                    $string .= $sep . $quoteChar . $field . $quoteChar . " $operator " . $value;
50
                }
51
            }
52
53
            $sep = $join;
54
        }
55
56
        return $string;
57
    }
58
59
    /**
60
     * @param mixed $val
61
     * @return string
62
     */
63
    protected function recursiveQuote($val)
64
    {
65
        if (is_array($val)) {
66
            $return = array();
67
            foreach ($val as $v) {
68
                $return[] = $this->recursiveQuote($v);
69
            }
70
71
            return '('.implode(',', $return).')';
72
        } elseif (is_null($val)) {
73
            $val = 'NULL';
74
        } elseif (is_int($val)) {
75
            $val = (int) $val;
76
        } elseif (is_double($val)) {
77
            $val = (double) $val;
78
        } elseif (is_float($val)) {
79
            $val = (float) $val;
80
        } else {
81
            $val = "'" . Convert::raw2sql($val) . "'";
82
        }
83
84
        return $val;
85
    }
86
87
    /**
88
     * @param string $message
89
     * @param string $status
90
     * @return string
91
     */
92
    public function ajaxResponse($message, $status)
93
    {
94
        return Convert::raw2json(array(
95
            'message' => $message,
96
            'status' => $status,
97
        ));
98
    }
99
}
100