/var/www/hkosl.com/b2b2c/webadmin/libraies/illuminate/validation/DatabasePresenceVerifier.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php

namespace Illuminate\Validation;

use 
Closure;
use 
Illuminate\Support\Str;
use 
Illuminate\Database\ConnectionResolverInterface;

class 
DatabasePresenceVerifier implements PresenceVerifierInterface
{
    
/**
     * The database connection instance.
     *
     * @var \Illuminate\Database\ConnectionResolverInterface
     */
    
protected $db;

    
/**
     * The database connection to use.
     *
     * @var string
     */
    
protected $connection null;

    
/**
     * Create a new database presence verifier.
     *
     * @param  \Illuminate\Database\ConnectionResolverInterface  $db
     * @return void
     */
    
public function __construct(ConnectionResolverInterface $db)
    {
        
$this->db $db;
    }

    
/**
     * Count the number of objects in a collection having the given value.
     *
     * @param  string  $collection
     * @param  string  $column
     * @param  string  $value
     * @param  int     $excludeId
     * @param  string  $idColumn
     * @param  array   $extra
     * @return int
     */
    
public function getCount($collection$column$value$excludeId null$idColumn null, array $extra = [])
    {
        
$query $this->table($collection)->where($column'='$value);

        if (! 
is_null($excludeId) && $excludeId != 'NULL') {
            
$query->where($idColumn ?: 'id''<>'$excludeId);
        }

        foreach (
$extra as $key => $extraValue) {
            if (
$extraValue instanceof Closure) {
                
$query->where(function ($query) use ($extraValue) {
                    
$extraValue($query);
                });
            } else {
                
$this->addWhere($query$key$extraValue);
            }
        }

        return 
$query->count();
    }

    
/**
     * Count the number of objects in a collection with the given values.
     *
     * @param  string  $collection
     * @param  string  $column
     * @param  array   $values
     * @param  array   $extra
     * @return int
     */
    
public function getMultiCount($collection$column, array $values, array $extra = [])
    {
        
$query $this->table($collection)->whereIn($column$values);

        foreach (
$extra as $key => $extraValue) {
            if (
$extraValue instanceof Closure) {
                
$query->where(function ($query) use ($extraValue) {
                    
$extraValue($query);
                });
            } else {
                
$this->addWhere($query$key$extraValue);
            }
        }

        return 
$query->count();
    }

    
/**
     * Add a "where" clause to the given query.
     *
     * @param  \Illuminate\Database\Query\Builder  $query
     * @param  string  $key
     * @param  string  $extraValue
     * @return void
     */
    
protected function addWhere($query$key$extraValue)
    {
        if (
$extraValue === 'NULL') {
            
$query->whereNull($key);
        } elseif (
$extraValue === 'NOT_NULL') {
            
$query->whereNotNull($key);
        } elseif (
Str::startsWith($extraValue'!')) {
            
$query->where($key'!='mb_substr($extraValue1));
        } else {
            
$query->where($key$extraValue);
        }
    }

    
/**
     * Get a query builder for the given table.
     *
     * @param  string  $table
     * @return \Illuminate\Database\Query\Builder
     */
    
protected function table($table)
    {
        return 
$this->db->connection($this->connection)->table($table)->useWritePdo();
    }

    
/**
     * Set the connection to be used.
     *
     * @param  string  $connection
     * @return void
     */
    
public function setConnection($connection)
    {
        
$this->connection $connection;
    }
}