Projekt

Obecné

Profil

Stáhnout (3.15 KB) Statistiky
| Větev: | Revize:
1
<?php
2

    
3
namespace Illuminate\Validation;
4

    
5
use Illuminate\Support\Str;
6
use Illuminate\Database\ConnectionResolverInterface;
7

    
8
class DatabasePresenceVerifier implements PresenceVerifierInterface
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 = null;
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     $excludeId
42
     * @param  string  $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
        foreach ($extra as $key => $extraValue) {
55
            $this->addWhere($query, $key, $extraValue);
56
        }
57

    
58
        return $query->count();
59
    }
60

    
61
    /**
62
     * Count the number of objects in a collection with the given values.
63
     *
64
     * @param  string  $collection
65
     * @param  string  $column
66
     * @param  array   $values
67
     * @param  array   $extra
68
     * @return int
69
     */
70
    public function getMultiCount($collection, $column, array $values, array $extra = [])
71
    {
72
        $query = $this->table($collection)->whereIn($column, $values);
73

    
74
        foreach ($extra as $key => $extraValue) {
75
            $this->addWhere($query, $key, $extraValue);
76
        }
77

    
78
        return $query->count();
79
    }
80

    
81
    /**
82
     * Add a "where" clause to the given query.
83
     *
84
     * @param  \Illuminate\Database\Query\Builder  $query
85
     * @param  string  $key
86
     * @param  string  $extraValue
87
     * @return void
88
     */
89
    protected function addWhere($query, $key, $extraValue)
90
    {
91
        if ($extraValue === 'NULL') {
92
            $query->whereNull($key);
93
        } elseif ($extraValue === 'NOT_NULL') {
94
            $query->whereNotNull($key);
95
        } elseif (Str::startsWith($extraValue, '!')) {
96
            $query->where($key, '!=', mb_substr($extraValue, 1));
97
        } else {
98
            $query->where($key, $extraValue);
99
        }
100
    }
101

    
102
    /**
103
     * Get a query builder for the given table.
104
     *
105
     * @param  string  $table
106
     * @return \Illuminate\Database\Query\Builder
107
     */
108
    protected function table($table)
109
    {
110
        return $this->db->connection($this->connection)->table($table)->useWritePdo();
111
    }
112

    
113
    /**
114
     * Set the connection to be used.
115
     *
116
     * @param  string  $connection
117
     * @return void
118
     */
119
    public function setConnection($connection)
120
    {
121
        $this->connection = $connection;
122
    }
123
}
(1-1/8)