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\Rules;
4
 
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Traits\Conditionable;
7
 
8
class Unique
9
{
10
    use Conditionable, DatabaseRule;
11
 
12
    /**
13
     * The ID that should be ignored.
14
     *
15
     * @var mixed
16
     */
17
    protected $ignore;
18
 
19
    /**
20
     * The name of the ID column.
21
     *
22
     * @var string
23
     */
24
    protected $idColumn = 'id';
25
 
26
    /**
27
     * Ignore the given ID during the unique check.
28
     *
29
     * @param  mixed  $id
30
     * @param  string|null  $idColumn
31
     * @return $this
32
     */
33
    public function ignore($id, $idColumn = null)
34
    {
35
        if ($id instanceof Model) {
36
            return $this->ignoreModel($id, $idColumn);
37
        }
38
 
39
        $this->ignore = $id;
40
        $this->idColumn = $idColumn ?? 'id';
41
 
42
        return $this;
43
    }
44
 
45
    /**
46
     * Ignore the given model during the unique check.
47
     *
48
     * @param  \Illuminate\Database\Eloquent\Model  $model
49
     * @param  string|null  $idColumn
50
     * @return $this
51
     */
52
    public function ignoreModel($model, $idColumn = null)
53
    {
54
        $this->idColumn = $idColumn ?? $model->getKeyName();
55
        $this->ignore = $model->{$this->idColumn};
56
 
57
        return $this;
58
    }
59
 
60
    /**
61
     * Convert the rule to a validation string.
62
     *
63
     * @return string
64
     */
65
    public function __toString()
66
    {
67
        return rtrim(sprintf('unique:%s,%s,%s,%s,%s',
68
            $this->table,
69
            $this->column,
70
            $this->ignore ? '"'.addslashes($this->ignore).'"' : 'NULL',
71
            $this->idColumn,
72
            $this->formatWheres()
73
        ), ',');
74
    }
75
}