Repository navigation
Expand file tree
/
Copy pathResourceItem.php
More file actions
114 lines (93 loc) · 3.08 KB
/
Copy pathResourceItem.php
File metadata and controls
114 lines (93 loc) · 3.08 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
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
105
106
107
108
109
110
111
112
113
114
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Resource;
use Klipper\Component\Resource\Domain\WrapperInterface;
use Klipper\Component\Resource\Exception\InvalidArgumentException;
use Klipper\Component\Resource\Exception\UnexpectedTypeException;
use Symfony\Component\Form\FormErrorIterator;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* Action resource for domain.
*
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class ResourceItem implements ResourceInterface
{
protected string $status;
protected object $data;
protected ConstraintViolationListInterface $errors;
/**
* @param FormInterface|object $data The data instance or form with data instance
* @param ConstraintViolationListInterface $errors The list of errors
*/
public function __construct($data, ?ConstraintViolationListInterface $errors = null)
{
$this->validateData($data);
$this->status = ResourceStatutes::PENDING;
$this->data = $data;
$this->errors = $errors ?? new ConstraintViolationList();
}
public function setStatus(string $status): void
{
$this->status = $status;
}
public function getStatus(): string
{
return $this->status;
}
public function getData()
{
return $this->data;
}
public function getRealData()
{
$data = $this->data instanceof WrapperInterface ? $this->data->getData() : $this->data;
return $data instanceof FormInterface ? $data->getData() : $data;
}
public function getErrors(): ConstraintViolationListInterface
{
return $this->errors;
}
public function getFormErrors(): FormErrorIterator
{
if ($this->data instanceof FormInterface) {
return $this->data->getErrors(true);
}
throw new InvalidArgumentException('The data of resource is not a form instance, used the "getErrors()" method');
}
public function isForm(): bool
{
return $this->getData() instanceof FormInterface;
}
public function isValid(): bool
{
$formSuccess = $this->isForm()
? 0 === $this->getFormErrors()->count()
: true;
return 0 === $this->getErrors()->count() && $formSuccess;
}
/**
* Validate the data.
*
* @param mixed $data
*
* @throws UnexpectedTypeException When the data or form data is not an instance of object
*/
protected function validateData($data): void
{
$data = $data instanceof WrapperInterface ? $data->getData() : $data;
$data = $data instanceof FormInterface ? $data->getData() : $data;
if (!\is_object($data)) {
throw new UnexpectedTypeException($data, 'object');
}
}
}