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\Database\Eloquent\Concerns;
4
 
5
use Illuminate\Database\Eloquent\ModelNotFoundException;
6
use Illuminate\Support\Str;
7
 
8
trait HasUuids
9
{
10
    /**
11
     * Generate a primary UUID for the model.
12
     *
13
     * @return void
14
     */
15
    public static function bootHasUuids()
16
    {
17
        static::creating(function (self $model) {
18
            foreach ($model->uniqueIds() as $column) {
19
                if (empty($model->{$column})) {
20
                    $model->{$column} = $model->newUniqueId();
21
                }
22
            }
23
        });
24
    }
25
 
26
    /**
27
     * Generate a new UUID for the model.
28
     *
29
     * @return string
30
     */
31
    public function newUniqueId()
32
    {
33
        return (string) Str::orderedUuid();
34
    }
35
 
36
    /**
37
     * Get the columns that should receive a unique identifier.
38
     *
39
     * @return array
40
     */
41
    public function uniqueIds()
42
    {
43
        return [$this->getKeyName()];
44
    }
45
 
46
    /**
47
     * Retrieve the model for a bound value.
48
     *
49
     * @param  \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Relations\Relation  $query
50
     * @param  mixed  $value
51
     * @param  string|null  $field
52
     * @return \Illuminate\Database\Eloquent\Relations\Relation
53
     *
54
     * @throws \Illuminate\Database\Eloquent\ModelNotFoundException
55
     */
56
    public function resolveRouteBindingQuery($query, $value, $field = null)
57
    {
58
        if ($field && in_array($field, $this->uniqueIds()) && ! Str::isUuid($value)) {
59
            throw (new ModelNotFoundException)->setModel(get_class($this), $value);
60
        }
61
 
62
        if (! $field && in_array($this->getRouteKeyName(), $this->uniqueIds()) && ! Str::isUuid($value)) {
63
            throw (new ModelNotFoundException)->setModel(get_class($this), $value);
64
        }
65
 
66
        return parent::resolveRouteBindingQuery($query, $value, $field);
67
    }
68
 
69
    /**
70
     * Get the auto-incrementing key type.
71
     *
72
     * @return string
73
     */
74
    public function getKeyType()
75
    {
76
        if (in_array($this->getKeyName(), $this->uniqueIds())) {
77
            return 'string';
78
        }
79
 
80
        return $this->keyType;
81
    }
82
 
83
    /**
84
     * Get the value indicating whether the IDs are incrementing.
85
     *
86
     * @return bool
87
     */
88
    public function getIncrementing()
89
    {
90
        if (in_array($this->getKeyName(), $this->uniqueIds())) {
91
            return false;
92
        }
93
 
94
        return $this->incrementing;
95
    }
96
}