Completed
Pull Request — master (#109)
by Robbie
07:12
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 SilverStripe\QueuedJobs;
4
5
use SilverStripe\Core\Convert;
6
use SilverStripe\Core\Injector\Injector;
7
8
/**
9
 * A set of utility functions used by the queued jobs module
10
 *
11
 * @license http://silverstripe.org/bsd-license
12
 * @author Marcus Nyeholt <[email protected]>
13
 */
14
class QJUtils
15
{
16
    /**
17
     * Quote up a filter of the form
18
     *
19
     * array ("ParentID =" => 1)
20
     *
21
     * @param array $filter
22
     * @param string $join
23
     * @return string
24
     */
25
    public function dbQuote($filter = array(), $join = " AND ")
26
    {
27
        $quoteChar = defined('DB::USE_ANSI_SQL') ? '"' : '';
28
29
        $string = '';
30
        $sep = '';
31
32
        foreach ($filter as $field => $value) {
33
            // first break the field up into its two components
34
            $operator = '';
35
            if (is_string($field)) {
36
                list($field, $operator) = explode(' ', trim($field));
37
            }
38
39
            $value = $this->recursiveQuote($value);
40
41
            if (strpos($field, '.')) {
42
                list($tb, $fl) = explode('.', $field);
43
                $string .= $sep . $quoteChar . $tb . $quoteChar . '.' . $quoteChar . $fl . $quoteChar . " $operator " . $value;
44
            } else {
45
                if (is_numeric($field)) {
46
                    $string .= $sep . $value;
47
                } else {
48
                    $string .= $sep . $quoteChar . $field . $quoteChar . " $operator " . $value;
49
                }
50
            }
51
52
            $sep = $join;
53
        }
54
55
        return $string;
56
    }
57
58
    /**
59
     * @param mixed $val
60
     * @return string
61
     */
62
    protected function recursiveQuote($val)
63
    {
64
        if (is_array($val)) {
65
            $return = array();
66
            foreach ($val as $v) {
67
                $return[] = $this->recursiveQuote($v);
68
            }
69
70
            return '('.implode(',', $return).')';
71
        } elseif (is_null($val)) {
72
            $val = 'NULL';
73
        } elseif (is_int($val)) {
74
            $val = (int) $val;
75
        } elseif (is_double($val)) {
76
            $val = (double) $val;
77
        } elseif (is_float($val)) {
78
            $val = (float) $val;
79
        } else {
80
            $val = "'" . Convert::raw2sql($val) . "'";
81
        }
82
83
        return $val;
84
    }
85
86
    /**
87
     * @deprecated 3.0 Use Injector::inst()->get('Logger') instead
88
     *
89
     * @param string $message
90
     * @param int $level
91
     */
92
    public function log($message, $level = null)
0 ignored issues
show
Unused Code introduced by
The parameter $level is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
93
    {
94
        Injector::inst()
95
            ->get('Logger')
96
            ->debug(
97
                print_r(
98
                    array(
99
                        'errno' => '',
100
                        'errstr' => $message,
101
                        'errfile' => dirname(__FILE__),
102
                        'errline' => '',
103
                        'errcontext' => array()
104
                    ),
105
                    true
106
                )
107
            );
108
    }
109
110
    /**
111
     * @param string $message
112
     * @param string $status
113
     * @return string
114
     */
115
    public function ajaxResponse($message, $status)
116
    {
117
        return Convert::raw2json(array(
118
            'message' => $message,
119
            'status' => $status,
120
        ));
121
    }
122
}
123