PropertyUpdater::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stratadox\Hydration\Proxying;
6
7
use Closure;
8
use Stratadox\Hydration\UpdatesTheProxyOwner;
9
10
/**
11
 * Updates a property with the newly loaded value.
12
 *
13
 * @package Stratadox\Hydrate
14
 * @author Stratadox
15
 */
16
class PropertyUpdater implements UpdatesTheProxyOwner
17
{
18
    private $owner;
19
    private $propertyShouldReference;
20
    private $setter;
21
22
    public function __construct(
23
        $theOwner,
24
        string $theProperty,
25
        Closure $setter = null
26
    ) {
27
        $this->owner = $theOwner;
28
        $this->propertyShouldReference = $theProperty;
29
        $this->setter = $setter ?: function (string $property, $value) : void
30
        {
31
            $this->$property = $value;
32
        };
33
    }
34
35
    public static function for(
36
        $theOwner,
37
        string $ofTheProperty,
38
        Closure $setter = null
39
    ) : UpdatesTheProxyOwner
40
    {
41
        return new static($theOwner, $ofTheProperty, $setter);
42
    }
43
44
    public function updateWith($theLoadedInstance) : void
45
    {
46
        $this->setter->call($this->owner,
47
            $this->propertyShouldReference,
48
            $theLoadedInstance
49
        );
50
    }
51
}
52