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\Queue;
4
 
5
use ReflectionClass;
6
use ReflectionProperty;
7
 
8
trait SerializesModels
9
{
10
    use SerializesAndRestoresModelIdentifiers;
11
 
12
    /**
13
     * Prepare the instance values for serialization.
14
     *
15
     * @return array
16
     */
17
    public function __serialize()
18
    {
19
        $values = [];
20
 
21
        $properties = (new ReflectionClass($this))->getProperties();
22
 
23
        $class = get_class($this);
24
 
25
        foreach ($properties as $property) {
26
            if ($property->isStatic()) {
27
                continue;
28
            }
29
 
30
            $property->setAccessible(true);
31
 
32
            if (! $property->isInitialized($this)) {
33
                continue;
34
            }
35
 
36
            $value = $this->getPropertyValue($property);
37
 
38
            if ($property->hasDefaultValue() && $value === $property->getDefaultValue()) {
39
                continue;
40
            }
41
 
42
            $name = $property->getName();
43
 
44
            if ($property->isPrivate()) {
45
                $name = "\0{$class}\0{$name}";
46
            } elseif ($property->isProtected()) {
47
                $name = "\0*\0{$name}";
48
            }
49
 
50
            $values[$name] = $this->getSerializedPropertyValue($value);
51
        }
52
 
53
        return $values;
54
    }
55
 
56
    /**
57
     * Restore the model after serialization.
58
     *
59
     * @param  array  $values
60
     * @return void
61
     */
62
    public function __unserialize(array $values)
63
    {
64
        $properties = (new ReflectionClass($this))->getProperties();
65
 
66
        $class = get_class($this);
67
 
68
        foreach ($properties as $property) {
69
            if ($property->isStatic()) {
70
                continue;
71
            }
72
 
73
            $name = $property->getName();
74
 
75
            if ($property->isPrivate()) {
76
                $name = "\0{$class}\0{$name}";
77
            } elseif ($property->isProtected()) {
78
                $name = "\0*\0{$name}";
79
            }
80
 
81
            if (! array_key_exists($name, $values)) {
82
                continue;
83
            }
84
 
85
            $property->setAccessible(true);
86
 
87
            $property->setValue(
88
                $this, $this->getRestoredPropertyValue($values[$name])
89
            );
90
        }
91
    }
92
 
93
    /**
94
     * Get the property value for the given property.
95
     *
96
     * @param  \ReflectionProperty  $property
97
     * @return mixed
98
     */
99
    protected function getPropertyValue(ReflectionProperty $property)
100
    {
101
        $property->setAccessible(true);
102
 
103
        return $property->getValue($this);
104
    }
105
}