-
Notifications
You must be signed in to change notification settings - Fork 17
/
RequestHandler.php
executable file
·104 lines (85 loc) · 3.42 KB
/
RequestHandler.php
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
declare(strict_types=1);
namespace KejawenLab\Semart\Skeleton\Request;
use Doctrine\Common\Inflector\Inflector;
use KejawenLab\Semart\Collection\Collection;
use KejawenLab\Semart\Skeleton\Application;
use ReflectionProperty;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\PropertyAccess\PropertyAccessor;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Muhamad Surya Iksanudin <[email protected]>
*/
class RequestHandler
{
public const REQUEST_TOKEN_NAME = 'APP_CSRF_TOKEN';
private $application;
private $propertyAccessor;
private $validator;
private $eventDispatcher;
private $translator;
private $errors;
public function __construct(Application $application, ValidatorInterface $validator, EventDispatcherInterface $eventDispatcher, TranslatorInterface $translator)
{
$this->propertyAccessor = new PropertyAccessor();
$this->application = $application;
$this->validator = $validator;
$this->eventDispatcher = $eventDispatcher;
$this->translator = $translator;
$this->errors = [];
}
public function handle(Request $request, object $object)
{
$filterEvent = new RequestEvent($request, $object);
$this->eventDispatcher->dispatch($filterEvent);
$reflection = new \ReflectionObject($object);
if ($parent = $reflection->getParentClass()) {
$reflection = $parent;
}
Collection::collect($reflection->getProperties(\ReflectionProperty::IS_PRIVATE | \ReflectionProperty::IS_PROTECTED))
->each(function ($value) use ($request, $object) {
/** @var ReflectionProperty $value */
$field = $value->getName();
$value = $request->request->get($field);
if ('id' !== strtolower($field) && null !== $value && '' !== $value) {
$this->bindValue($object, $field, $value);
}
})
;
$this->validate($object, $reflection);
}
public function isValid()
{
return empty($this->errors) ? true : false;
}
public function getErrors(): array
{
return $this->errors;
}
private function validate(object $object, \ReflectionClass $reflection): void
{
$this->errors = Collection::collect($this->validator->validate($object))
->flatten()
->map(function ($value) use ($reflection) {
/* @var ConstraintViolationInterface $value */
return sprintf('<b><i>%s</i></b>: %s', $this->translator->trans(sprintf('label.%s.%s', Inflector::tableize($reflection->getShortName()), Inflector::tableize($value->getPropertyPath()))), $this->translator->trans($value->getMessage()));
})
->toArray()
;
}
private function bindValue(object $object, string $field, $value): void
{
try {
$this->propertyAccessor->setValue($object, $field, $value);
} catch (\Exception $e) {
$service = $this->application->getService(new \ReflectionObject($object), $field);
if ($service) {
$this->propertyAccessor->setValue($object, $field, $service->get($value));
}
}
}
}