Passed
Push — master ( ae720b...07d247 )
by Michael
02:20
created

HelpersTrait::setAttributesNull()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 4
nop 0
dl 0
loc 6
rs 9.2
c 0
b 0
f 0
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\BelongsTo;
8
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
9
use Illuminate\Database\Eloquent\Relations\Relation;
10
11
/**
12
 * Trait RelatedPlusHelpers
13
 *
14
 * Static helper functions
15
 */
16
trait HelpersTrait
17
{
18
    /**
19
     * Return the sql for a query with the bindings replaced with the binding values
20
     *
21
     * @param Builder $builder
22
     * @return string
23
     */
24
    public function toSqlWithBindings($builder)
25
    {
26
        $replacements = $builder->getBindings();
27
        $sql = $builder->toSql();
28
29
        foreach ($replacements as &$replacement) {
30
            $replacement = addslashes($replacement);
31
            if (!is_numeric($replacement)) {
32
                $replacement = '"' . $replacement . '"';
33
            }
34
        }
35
36
        return preg_replace_callback(
37
            '/(\?)(?=(?:[^\'"]|["\'][^\'"]*["\'])*$)/',
38
            function () use (&$replacements) {
39
                return array_shift($replacements);
40
            },
41
            $sql
42
        );
43
    }
44
45
    /**
46
     * Get table name from column name
47
     *
48
     * @param string $column
49
     * @return string
50
     */
51
    public function getTableFromColumn($column)
52
    {
53
        $periodPos = strpos($column, '.');
54
55
        return ($periodPos === false ? $column : substr($column, 0, $periodPos));
0 ignored issues
show
introduced by
The condition $periodPos === false can never be true.
Loading history...
56
    }
57
58
    /**
59
     * Remove any global scopes which contain $scopeName in their name
60
     *
61
     * @param RelatedPlusTrait|Model $model
62
     * @param Relation|Builder $query
63
     * @param string $scopeName
64
     * @return Relation|BelongsTo|HasOneOrMany|Builder
65
     */
66
    public function removeGlobalScopes($model, $query, $scopeName)
67
    {
68
        $query->withoutGlobalScopes(collect($model->getGlobalScopes())->keys()->filter(function (
0 ignored issues
show
Bug introduced by
It seems like getGlobalScopes() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

68
        $query->withoutGlobalScopes(collect($model->/** @scrutinizer ignore-call */ getGlobalScopes())->keys()->filter(function (
Loading history...
69
            $value
70
        ) use ($scopeName) {
71
            return stripos($value, $scopeName) !== false;
72
        })->toArray());
73
74
        return $query;
75
    }
76
77
    /**
78
     * Set empty fields to null
79
     */
80
    public function setAttributesNull()
81
    {
82
        /** @var Model $this */
83
        foreach ($this->attributes as $key => $value) {
0 ignored issues
show
Bug Best Practice introduced by
The property attributes does not exist on Blasttech\EloquentRelatedPlus\HelpersTrait. Did you maybe forget to declare it?
Loading history...
84
            if (isset($this->nullable[$key])) {
85
                $this->{$key} = empty(trim($value)) ? null : $value;
86
            }
87
        }
88
    }
89
90
}
91