Passed
Push — master ( b3f3ee...d2f940 )
by Michael
02:20
created

RelatedPlusHelpers::removeGlobalScope()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 3
1
<?php
2
3
namespace Blasttech\EloquentRelatedPlus;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\Relation;
8
9
/**
10
 * Class RelatedPlusHelpers
11
 *
12
 * @property array order_fields
13
 * @property array order_defaults
14
 * @property array order_relations
15
 * @property array order_with
16
 * @property array search_fields
17
 * @property string connection
18
 */
19
class RelatedPlusHelpers
20
{
21
    /**
22
     * Get relation table name and alias
23
     *
24
     * @param Relation $relation
25
     * @return \stdClass
26
     */
27
    public static function getRelationTables($relation)
28
    {
29
        $table = new \stdClass();
30
        $table->name = $relation->getRelated()->getTable();
31
        // if using a 'table' AS 'tableAlias' in a from statement, otherwise alias will be the table name
32
        $from = explode(' ', $relation->getQuery()->getQuery()->from);
33
        $table->alias = array_pop($from);
34
35
        return $table;
36
    }
37
38
    /**
39
     * Get the relations from a relation name
40
     * $relationName can be a single relation
41
     * Usage for User model:
42
     * parseRelationNames('customer') returns [$user->customer()]
43
     * parseRelationNames('customer.contact') returns [$user->customer(), $user->customer->contact()]
44
     *
45
     * @param Model $model
46
     * @param string $relationName
47
     * @return Relation[]
48
     */
49
    public static function parseRelationNames($model, $relationName)
50
    {
51
        $relationNames = explode('.', $relationName);
52
        $parentRelationName = null;
53
        $relations = [];
54
55
        foreach ($relationNames as $relationName) {
0 ignored issues
show
introduced by
$relationName is overwriting one of the parameters of this function.
Loading history...
56
            if (is_null($parentRelationName)) {
57
                $relations[] = $model->$relationName();
58
                $parentRelationName = $model->$relationName()->getRelated();
59
            } else {
60
                $relations[] = $parentRelationName->$relationName();
61
            }
62
        }
63
64
        return $relations;
65
    }
66
67
    /**
68
     * Return the sql for a query with the bindings replaced with the binding values
69
     *
70
     * @param Builder $builder
71
     * @return string
72
     */
73
    public static function toSqlWithBindings($builder)
74
    {
75
        $replacements = array_map('addslashes', $builder->getBindings());
0 ignored issues
show
Bug introduced by
It seems like $builder->getBindings() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $arr1 of array_map() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

75
        $replacements = array_map('addslashes', /** @scrutinizer ignore-type */ $builder->getBindings());
Loading history...
76
        $sql = $builder->toSql();
77
78
        foreach ($replacements as &$replacement) {
79
            if (!is_numeric($replacement)) {
80
                $replacement = '"' . $replacement . '"';
81
            }
82
        }
83
84
        return preg_replace_callback(
85
            '/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/',
86
            function () use (&$replacements) {
87
                return array_shift($replacements);
88
            },
89
            $sql
90
        );
91
    }
92
93
    /**
94
     * Add table name to column name if table name not already included in column name
95
     *
96
     * @param string $table
97
     * @param string $column
98
     * @return string
99
     */
100
    public static function columnWithTableName($table, $column)
101
    {
102
        return (preg_match('/(' . $table . '\.|`' . $table . '`)/i', $column) > 0 ? '' : $table . '.') . $column;
103
    }
104
105
    /**
106
     * Get table name from column name
107
     *
108
     * @param string $column
109
     * @return string
110
     */
111
    public static function getTableFromColumn($column)
112
    {
113
        $periodPos = strpos($column, '.');
114
115
        return ($periodPos !== false ? substr($column, 0, $periodPos) : $column);
0 ignored issues
show
introduced by
The condition $periodPos !== false can never be false.
Loading history...
116
    }
117
118
    /**
119
     * Get table name with alias if different to table name
120
     *
121
     * @param \stdClass $table
122
     * @return string
123
     */
124
    public static function getTableWithAlias($table)
125
    {
126
        if ($table->alias !== '' && $table->name !== $table->alias) {
127
            return $table->name . ' AS ' . $table->alias;
128
        }
129
130
        return $table->name;
131
    }
132
133
    /**
134
     * Remove a global scope if it exists
135
     *
136
     * @param Model $model
137
     * @param Builder $query
138
     * @param string $scopeName
139
     * @return Builder
140
     */
141
    public static function removeGlobalScope($model, $query, $scopeName)
142
    {
143
        $globalScopes = $model->getGlobalScopes();
144
        if (isset($globalScopes[$scopeName])) {
145
            $query->withoutGlobalScope($scopeName);
146
        }
147
148
        return $query;
149
    }
150
}
151