-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPropertyValueProviderForModels.php
More file actions
76 lines (62 loc) · 2.35 KB
/
PropertyValueProviderForModels.php
File metadata and controls
76 lines (62 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace Prosopo\Views\PrivateClasses\Object;
use Prosopo\Views\Interfaces\Model\ModelFactoryInterface;
use Prosopo\Views\Interfaces\Model\TemplateModelInterface;
use Prosopo\Views\Interfaces\Object\PropertyValueProviderInterface;
use ReflectionProperty;
/**
* This class is marked as a final and placed under the 'Private' namespace to prevent anyone from using it directly.
* We reserve the right to change its name and implementation.
*/
final class PropertyValueProviderForModels implements PropertyValueProviderInterface
{
private PropertyValueProviderInterface $propertyValueProvider;
private ModelFactoryInterface $modelFactory;
public function __construct(
PropertyValueProviderInterface $propertyValueProvider,
ModelFactoryInterface $viewFactory
) {
$this->propertyValueProvider = $propertyValueProvider;
$this->modelFactory = $viewFactory;
}
public function supportsProperty(ReflectionProperty $property): bool
{
if ($this->propertyValueProvider->supportsProperty($property)) {
return true;
}
$type = $this->getPropertyType($property);
return null !== $this->getValidModelClass($type);
}
public function getPropertyValue(ReflectionProperty $property)
{
if ($this->propertyValueProvider->supportsProperty($property)) {
return $this->propertyValueProvider->getPropertyValue($property);
}
$type = $this->getPropertyType($property);
$modelClassString = $this->getValidModelClass($type);
return null !== $modelClassString ?
$this->modelFactory->createModel($modelClassString) :
null;
}
/**
* @param class-string<TemplateModelInterface>|string $propertyType
*
* @return class-string<TemplateModelInterface>|null
*/
protected function getValidModelClass(string $propertyType)
{
return class_exists($propertyType) &&
is_a($propertyType, TemplateModelInterface::class, true) ?
$propertyType :
null;
}
protected function getPropertyType(ReflectionProperty $property): string
{
$reflectionType = $property->getType();
return null !== $reflectionType ?
// @phpstan-ignore-next-line
$reflectionType->getName() :
'';
}
}