Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Validation;
4
 
5
use Closure;
6
use Illuminate\Database\ConnectionResolverInterface;
7
 
8
class DatabasePresenceVerifier implements DatabasePresenceVerifierInterface
9
{
10
    /**
11
     * The database connection instance.
12
     *
13
     * @var \Illuminate\Database\ConnectionResolverInterface
14
     */
15
    protected $db;
16
 
17
    /**
18
     * The database connection to use.
19
     *
20
     * @var string
21
     */
22
    protected $connection;
23
 
24
    /**
25
     * Create a new database presence verifier.
26
     *
27
     * @param  \Illuminate\Database\ConnectionResolverInterface  $db
28
     * @return void
29
     */
30
    public function __construct(ConnectionResolverInterface $db)
31
    {
32
        $this->db = $db;
33
    }
34
 
35
    /**
36
     * Count the number of objects in a collection having the given value.
37
     *
38
     * @param  string  $collection
39
     * @param  string  $column
40
     * @param  string  $value
41
     * @param  int|null  $excludeId
42
     * @param  string|null  $idColumn
43
     * @param  array  $extra
44
     * @return int
45
     */
46
    public function getCount($collection, $column, $value, $excludeId = null, $idColumn = null, array $extra = [])
47
    {
48
        $query = $this->table($collection)->where($column, '=', $value);
49
 
50
        if (! is_null($excludeId) && $excludeId !== 'NULL') {
51
            $query->where($idColumn ?: 'id', '<>', $excludeId);
52
        }
53
 
54
        return $this->addConditions($query, $extra)->count();
55
    }
56
 
57
    /**
58
     * Count the number of objects in a collection with the given values.
59
     *
60
     * @param  string  $collection
61
     * @param  string  $column
62
     * @param  array  $values
63
     * @param  array  $extra
64
     * @return int
65
     */
66
    public function getMultiCount($collection, $column, array $values, array $extra = [])
67
    {
68
        $query = $this->table($collection)->whereIn($column, $values);
69
 
70
        return $this->addConditions($query, $extra)->distinct()->count($column);
71
    }
72
 
73
    /**
74
     * Add the given conditions to the query.
75
     *
76
     * @param  \Illuminate\Database\Query\Builder  $query
77
     * @param  array  $conditions
78
     * @return \Illuminate\Database\Query\Builder
79
     */
80
    protected function addConditions($query, $conditions)
81
    {
82
        foreach ($conditions as $key => $value) {
83
            if ($value instanceof Closure) {
84
                $query->where(function ($query) use ($value) {
85
                    $value($query);
86
                });
87
            } else {
88
                $this->addWhere($query, $key, $value);
89
            }
90
        }
91
 
92
        return $query;
93
    }
94
 
95
    /**
96
     * Add a "where" clause to the given query.
97
     *
98
     * @param  \Illuminate\Database\Query\Builder  $query
99
     * @param  string  $key
100
     * @param  string  $extraValue
101
     * @return void
102
     */
103
    protected function addWhere($query, $key, $extraValue)
104
    {
105
        if ($extraValue === 'NULL') {
106
            $query->whereNull($key);
107
        } elseif ($extraValue === 'NOT_NULL') {
108
            $query->whereNotNull($key);
109
        } elseif (str_starts_with($extraValue, '!')) {
110
            $query->where($key, '!=', mb_substr($extraValue, 1));
111
        } else {
112
            $query->where($key, $extraValue);
113
        }
114
    }
115
 
116
    /**
117
     * Get a query builder for the given table.
118
     *
119
     * @param  string  $table
120
     * @return \Illuminate\Database\Query\Builder
121
     */
122
    protected function table($table)
123
    {
124
        return $this->db->connection($this->connection)->table($table)->useWritePdo();
125
    }
126
 
127
    /**
128
     * Set the connection to be used.
129
     *
130
     * @param  string  $connection
131
     * @return void
132
     */
133
    public function setConnection($connection)
134
    {
135
        $this->connection = $connection;
136
    }
137
}