|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) Nate Brunette. |
|
4
|
|
|
* Distributed under the MIT License (http://opensource.org/licenses/MIT) |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace Tebru\Gson\Internal; |
|
10
|
|
|
|
|
11
|
|
|
use ReflectionMethod; |
|
12
|
|
|
use ReflectionProperty; |
|
13
|
|
|
use Tebru\Gson\Internal\AccessorStrategy\GetByClosure; |
|
14
|
|
|
use Tebru\Gson\Internal\AccessorStrategy\GetByMethod; |
|
15
|
|
|
use Tebru\Gson\Internal\AccessorStrategy\GetByPublicProperty; |
|
16
|
|
|
use Tebru\Gson\Internal\AccessorStrategy\SetByClosure; |
|
17
|
|
|
use Tebru\Gson\Internal\AccessorStrategy\SetByMethod; |
|
18
|
|
|
use Tebru\Gson\Internal\AccessorStrategy\SetByPublicProperty; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class AccessorStrategyFactory |
|
22
|
|
|
* |
|
23
|
|
|
* Creates the strategy that will be used to get or set values |
|
24
|
|
|
* |
|
25
|
|
|
* @author Nate Brunette <[email protected]> |
|
26
|
|
|
*/ |
|
27
|
|
|
final class AccessorStrategyFactory |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Create strategy to access a value |
|
31
|
|
|
* |
|
32
|
|
|
* - If there's a method, use the method |
|
33
|
|
|
* - If the property is public, use the property |
|
34
|
|
|
* - Bind a closure |
|
35
|
|
|
* |
|
36
|
|
|
* @param ReflectionProperty $reflectionProperty |
|
37
|
|
|
* @param ReflectionMethod|null $getterMethod |
|
38
|
|
|
* @return GetterStrategy |
|
39
|
|
|
*/ |
|
40
|
5 |
|
public function getterStrategy(ReflectionProperty $reflectionProperty, ReflectionMethod $getterMethod = null): GetterStrategy |
|
41
|
|
|
{ |
|
42
|
5 |
|
if (null !== $getterMethod) { |
|
43
|
3 |
|
return new GetByMethod($getterMethod->getName()); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
2 |
|
if ($reflectionProperty->isPublic()) { |
|
47
|
1 |
|
return new GetByPublicProperty($reflectionProperty->getName()); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
return new GetByClosure($reflectionProperty->getName(), $reflectionProperty->getDeclaringClass()->getName()); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Create strategy to set a value |
|
55
|
|
|
* |
|
56
|
|
|
* - If there's a method, use the method |
|
57
|
|
|
* - If the property is public, use the property |
|
58
|
|
|
* - Bind a closure |
|
59
|
|
|
* |
|
60
|
|
|
* @param ReflectionProperty $reflectionProperty |
|
61
|
|
|
* @param ReflectionMethod|null $setterMethod |
|
62
|
|
|
* @return SetterStrategy |
|
63
|
|
|
*/ |
|
64
|
5 |
|
public function setterStrategy(ReflectionProperty $reflectionProperty, ReflectionMethod $setterMethod = null): SetterStrategy |
|
65
|
|
|
{ |
|
66
|
5 |
|
if (null !== $setterMethod) { |
|
67
|
1 |
|
return new SetByMethod($setterMethod->getName()); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
4 |
|
if ($reflectionProperty->isPublic()) { |
|
71
|
1 |
|
return new SetByPublicProperty($reflectionProperty->getName()); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return new SetByClosure($reflectionProperty->getName(), $reflectionProperty->getDeclaringClass()->getName()); |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
|