1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Stratadox\TableLoader; |
5
|
|
|
|
6
|
|
|
use function is_null; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Locates the source object for the relationship. |
10
|
|
|
* |
11
|
|
|
* @author Stratadox |
12
|
|
|
*/ |
13
|
|
|
final class From implements KnowsWhereToLookFrom |
14
|
|
|
{ |
15
|
|
|
private $who; |
16
|
|
|
private $identity; |
17
|
|
|
private $class; |
18
|
|
|
|
19
|
|
|
private function __construct(string $who, IdentifiesEntities $identity, ?string $class) |
20
|
|
|
{ |
21
|
|
|
$this->who = $who; |
22
|
|
|
$this->identity = $identity; |
23
|
|
|
$this->class = $class; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Makes a new source locator. |
28
|
|
|
* |
29
|
|
|
* @param string $label The label of the source objects. |
30
|
|
|
* @param IdentifiesEntities $identity The mechanism to identify the source |
31
|
|
|
* entity of the row. |
32
|
|
|
* @return KnowsWhereToLookFrom The source locator. |
33
|
|
|
*/ |
34
|
|
|
public static function the( |
35
|
|
|
string $label, |
36
|
|
|
IdentifiesEntities $identity |
37
|
|
|
): KnowsWhereToLookFrom { |
38
|
|
|
return new self($label, $identity, null); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Makes a new source locator for a concrete class. |
43
|
|
|
* |
44
|
|
|
* @param string $class The name of the concrete class. |
45
|
|
|
* @param string $label The label of the source objects. |
46
|
|
|
* @param IdentifiesEntities $identity The mechanism to identify the source |
47
|
|
|
* entity of the row. |
48
|
|
|
* @return KnowsWhereToLookFrom The source locator. |
49
|
|
|
*/ |
50
|
|
|
public static function onlyThe( |
51
|
|
|
string $class, |
52
|
|
|
string $label, |
53
|
|
|
IdentifiesEntities $identity |
54
|
|
|
): KnowsWhereToLookFrom { |
55
|
|
|
return new self($label, $identity, $class); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @inheritdoc */ |
59
|
|
|
public function label(): string |
60
|
|
|
{ |
61
|
|
|
return $this->who; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** @inheritdoc */ |
65
|
|
|
public function this(array $relationship): string |
66
|
|
|
{ |
67
|
|
|
return $this->identity->forLoading($relationship); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @inheritdoc */ |
71
|
|
|
public function hereToo(object $shouldWeConnectIt): bool |
72
|
|
|
{ |
73
|
|
|
return is_null($this->class) || $shouldWeConnectIt instanceof $this->class; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|