1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cypress\PatchManager\Handler; |
4
|
|
|
|
5
|
|
|
use Cypress\PatchManager\OperationData; |
6
|
|
|
use Cypress\PatchManager\Patchable; |
7
|
|
|
use Cypress\PatchManager\PatchOperationHandler; |
8
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
10
|
|
|
|
11
|
|
|
class DataHandler implements PatchOperationHandler |
12
|
|
|
{ |
13
|
|
|
protected bool $magicCall = false; |
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param bool $magicCall |
17
|
|
|
*/ |
18
|
2 |
|
public function useMagicCall(bool $magicCall): void |
19
|
|
|
{ |
20
|
2 |
|
$this->magicCall = $magicCall; |
21
|
2 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Patchable $subject |
25
|
|
|
* @param OperationData $operationData |
26
|
|
|
*/ |
27
|
2 |
|
public function handle(Patchable $subject, OperationData $operationData): void |
28
|
|
|
{ |
29
|
2 |
|
$propertyAccessorBuilder = PropertyAccess::createPropertyAccessorBuilder(); |
30
|
2 |
|
$propertyAccessorBuilder = $this->magicCall ? $propertyAccessorBuilder->enableMagicCall() : $propertyAccessorBuilder; |
31
|
|
|
|
32
|
2 |
|
$propertyAccessor = $propertyAccessorBuilder->getPropertyAccessor(); |
33
|
2 |
|
$propertyAccessor->setValue( |
34
|
2 |
|
$subject, |
35
|
2 |
|
$operationData->get('property')->get(), |
36
|
2 |
|
$operationData->get('value')->get() |
37
|
|
|
); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* the operation name |
42
|
|
|
* |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
3 |
|
public function getName(): string |
46
|
|
|
{ |
47
|
3 |
|
return 'data'; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* use the OptionResolver instance to configure the required and optional fields that needs to be passed |
52
|
|
|
* with the request body. See http://symfony.com/doc/current/components/options_resolver.html to check all |
53
|
|
|
* possible options |
54
|
|
|
* |
55
|
|
|
* @param OptionsResolver $optionsResolver |
56
|
|
|
*/ |
57
|
1 |
|
public function configureOptions(OptionsResolver $optionsResolver): void |
58
|
|
|
{ |
59
|
1 |
|
$optionsResolver->setRequired(['property', 'value']); |
60
|
1 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* whether the handler is able to handle the given subject |
64
|
|
|
* |
65
|
|
|
* @param Patchable $subject |
66
|
|
|
* @return bool |
67
|
|
|
*/ |
68
|
|
|
public function canHandle(Patchable $subject): bool |
69
|
|
|
{ |
70
|
|
|
return true; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|