Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 1
<?php
2
 
3
namespace Spatie\Cloneable;
4
 
5
use ReflectionClass;
6
 
7
trait Cloneable
8
{
9
    public function with(...$values): static
10
    {
11
        $refClass = new ReflectionClass(static::class);
12
        $clone = $refClass->newInstanceWithoutConstructor();
13
 
14
        foreach ($refClass->getProperties() as $property) {
15
            if ($property->isStatic()) {
16
                continue;
17
            }
18
 
19
            $objectField = $property->getName();
20
 
21
            if (array_key_exists($objectField, $values)) {
22
                $objectValue = $values[$objectField];
23
            } elseif ($property->isInitialized($this)) {
24
                $objectValue = $property->getValue($this);
25
            } else {
26
                continue;
27
            }
28
 
29
            $declarationScope = $property->getDeclaringClass()->getName();
30
            if ($declarationScope === self::class) {
31
                $clone->$objectField = $objectValue;
32
            } else {
33
                (fn () => $this->$objectField = $objectValue)
34
                    ->bindTo($clone, $declarationScope)();
35
            }
36
        }
37
 
38
        return $clone;
39
    }
40
}