Completed
Push — master ( 2aa650...c3039e )
by Jesse
01:58
created

ArrayEntryUpdater::for()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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