Completed
Push — work-fleets ( eaef65...a4e57a )
by SuperNova.WS
05:17
created

DbSqlAware::stringEscape()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 6
rs 9.4285
1
<?php
2
3
class DbSqlAware {
4
  /**
5
   * @var db_mysql $db
6
   */
7
  protected $db;
8
9
  /**
10
   * DbSqlAware constructor.
11
   *
12
   * @param db_mysql|null $db
13
   */
14
  public function __construct($db = null) {
15
    $this->db = (!empty($db) && $db instanceof db_mysql) || !class_exists('classSupernova', false) ? $db : classSupernova::$db;
16
  }
17
18
  /**
19
   * @param db_mysql|null $db
20
   *
21
   * @return static
22
   */
23
  public static function build($db = null) {
24
    $result = new static($db);
25
26
    return $result;
27
  }
28
29
  /**
30
   * @param string $string
31
   *
32
   * @return mixed|string
33
   */
34
  protected function stringEscape($string) {
35
    return
36
      method_exists($this->db, 'db_escape')
37
        ? $this->db->db_escape($string)
38
        : str_replace('`', '\`', addslashes($string));
39
  }
40
41
  /**
42
   * Quotes string by ref as field - except '*'
43
   *
44
   * @param string &$string
45
   */
46
  protected function quoteStringAsFieldByRef(&$string) {
47
    $string = $this->stringEscape($string);
48
    if ((string)$string && '*' != $string) {
49
      $string = '`' . $string . '`';
50
    }
51
  }
52
53
  /**
54
   * Escapes field
55
   *
56
   * @param string $string
57
   *
58
   * @return string
59
   */
60
  protected function makeFieldFromString($string) {
61
    // Checking if string is just a '*' - to skip some code
62
    if ($string != '*') {
63
      $temp = explode('.', $string);
64
      array_walk($temp, array($this, 'quoteStringAsFieldByRef'));
65
      $string = implode('.', $temp);
66
    }
67
68
    return $string;
69
  }
70
71
  /**
72
   * @param string $functionName
73
   * @param string $field
74
   *
75
   * @return string
76
   */
77
  protected function makeAliasFromField($functionName, $field) {
78
    $alias = strtolower($functionName);
79
80
    // Checking if string is just a '*' - to skip some code
81
    if ($field != '*') {
82
      $temp = explode('.', $field);
83
      array_walk($temp, 'DbSqlHelper::UCFirstByRef');
84
      if (!empty($temp[1]) && $temp[1] == '*') {
85
        unset($temp[1]);
86
      }
87
      $temp = implode('', $temp);
88
      $alias .= ucfirst($temp);
89
    } else {
90
      $alias .= 'Value';
91
    }
92
93
    return $alias;
94
  }
95
96
}
97