|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Stratadox\Hydration\Mapper\Instruction; |
|
5
|
|
|
|
|
6
|
|
|
use Stratadox\Hydration\Mapping\Property\Check; |
|
7
|
|
|
use Stratadox\Hydration\Mapping\Property\Scalar\OriginalValue; |
|
8
|
|
|
use Stratadox\HydrationMapper\FindsKeys; |
|
9
|
|
|
use Stratadox\HydrationMapper\InstructsHowToMap; |
|
10
|
|
|
use Stratadox\HydrationMapping\MapsProperty; |
|
11
|
|
|
use Stratadox\Specification\Contract\Satisfiable; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Indicates a change in data key. |
|
15
|
|
|
* |
|
16
|
|
|
* @package Stratadox\Hydrate |
|
17
|
|
|
* @author Stratadox |
|
18
|
|
|
*/ |
|
19
|
|
|
final class In implements FindsKeys, InstructsHowToMap |
|
20
|
|
|
{ |
|
21
|
|
|
private $key; |
|
22
|
|
|
private $constraint; |
|
23
|
|
|
|
|
24
|
|
|
private function __construct(string $key, ?Satisfiable $constraint) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->key = $key; |
|
27
|
|
|
$this->constraint = $constraint; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Indicates that the data for this property can be found in a different key. |
|
32
|
|
|
* |
|
33
|
|
|
* @param string $key The offset to use in the input array. |
|
34
|
|
|
* @return In The instruction object. |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function key(string $key): In |
|
37
|
|
|
{ |
|
38
|
|
|
return new In($key, null); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** @inheritdoc */ |
|
42
|
|
|
public function that(Satisfiable $constraint): InstructsHowToMap |
|
43
|
|
|
{ |
|
44
|
|
|
return new In($this->key, $constraint); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** @inheritdoc */ |
|
48
|
|
|
public function find(): string |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->key; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** @inheritdoc */ |
|
54
|
|
|
public function followFor(string $property): MapsProperty |
|
55
|
|
|
{ |
|
56
|
|
|
$mapping = OriginalValue::inPropertyWithDifferentKey($property, $this->key); |
|
57
|
|
|
if (isset($this->constraint)) { |
|
58
|
|
|
$mapping = Check::that($this->constraint, $mapping); |
|
59
|
|
|
} |
|
60
|
|
|
return $mapping; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|