Code Duplication    Length = 154-154 lines in 2 locations

src/UI/Resolver/Validator/RequestAttributeValueValidator.php 1 location

@@ 15-168 (lines=154) @@
12
/**
13
 * @author Guillaume MOREL <[email protected]>
14
 */
15
class RequestAttributeValueValidator implements UIValidatorInterface
16
{
17
    /** @var RawValueValidator */
18
    protected $rawValueValidator;
19
20
    public function __construct(RawValueValidator $rawValueValidator)
21
    {
22
        $this->rawValueValidator = $rawValueValidator;
23
    }
24
25
    /**
26
     * Validate if request attribute $_POST field can be mapped to a Command
27
     * Throw CommandMappingException directly if any mapping issue
28
     * @param ServerRequestInterface $request
29
     * @param string $attributeKey
30
     *
31
     * @return mixed
32
     * @throws CommandMappingException If any mapping validation failed
33
     */
34
    public function extractValueFromRequestAttribute(ServerRequestInterface $request, string $attributeKey)
35
    {
36
        try {
37
            $attributes = $request->getParsedBody();
38
39
            Assertion::isArray($attributes);
40
            Assertion::keyExists($attributes, $attributeKey);
41
42
            $value = $attributes[$attributeKey];
43
44
            return $value;
45
        } catch (AssertionFailedException $exception) {
46
            throw CommandMappingException::fromAttribute(
47
                $exception->getMessage(),
48
                $exception->getPropertyPath()
49
            );
50
        }
51
    }
52
53
    /**
54
     * Exceptions are caught in order to be processed later
55
     *
56
     * @throws CommandMappingException If any mapping validation failed
57
     * @return mixed Untouched value
58
     */
59
    public function mustBeString(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
60
    {
61
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
62
63
        return $this->rawValueValidator->mustBeString(
64
            $value,
65
            $attributeKey,
66
            $this,
67
            $exceptionMessage
68
        );
69
    }
70
71
    /**
72
     * Exceptions are caught in order to be processed later
73
     *
74
     * @throws CommandMappingException If any mapping validation failed
75
     * @return mixed Untouched value
76
     */
77
    public function mustBeBoolean(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
78
    {
79
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
80
81
        return $this->rawValueValidator->mustBeBoolean(
82
            $value,
83
            $attributeKey,
84
            $this,
85
            $exceptionMessage
86
        );
87
    }
88
89
    /**
90
     * Exceptions are caught in order to be processed later
91
     *
92
     * @throws CommandMappingException If any mapping validation failed
93
     * @return mixed Untouched value
94
     */
95
    public function mustBeArray(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
96
    {
97
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
98
99
        return $this->rawValueValidator->mustBeArray(
100
            $value,
101
            $attributeKey,
102
            $this,
103
            $exceptionMessage
104
        );
105
    }
106
107
    /**
108
     * Exceptions are caught in order to be processed later
109
     *
110
     * @throws CommandMappingException If any mapping validation failed
111
     * @return mixed Untouched value
112
     */
113
    public function mustBeFloat(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
114
    {
115
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
116
117
        return $this->rawValueValidator->mustBeFloat(
118
            $value,
119
            $attributeKey,
120
            $this,
121
            $exceptionMessage
122
        );
123
    }
124
125
    /**
126
     * Exceptions are caught in order to be processed later
127
     *
128
     * @throws CommandMappingException If any mapping validation failed
129
     * @return mixed Untouched value
130
     */
131
    public function mustBeInteger(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
132
    {
133
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
134
135
        return $this->rawValueValidator->mustBeInteger(
136
            $value,
137
            $attributeKey,
138
            $this,
139
            $exceptionMessage
140
        );
141
    }
142
143
    /**
144
     * Exceptions are caught in order to be processed later
145
     *
146
     * @throws CommandMappingException If any mapping validation failed
147
     * @return mixed Untouched value
148
     */
149
    public function mustBeDate(ServerRequestInterface $request, string $attributeKey, string $exceptionMessage = null)
150
    {
151
        $value = $this->extractValueFromRequestAttribute($request, $attributeKey);
152
153
        return $this->rawValueValidator->mustBeDate(
154
            $value,
155
            $attributeKey,
156
            $this,
157
            $exceptionMessage
158
        );
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
165
    {
166
        return UIValidationException::fromAttribute($message, $propertyPath);
167
    }
168
}
169

src/UI/Resolver/Validator/RequestQueryStringValueValidator.php 1 location

@@ 15-168 (lines=154) @@
12
/**
13
 * @author Guillaume MOREL <[email protected]>
14
 */
15
class RequestQueryStringValueValidator implements UIValidatorInterface
16
{
17
    /** @var RawValueValidator */
18
    protected $rawValueValidator;
19
20
    public function __construct(RawValueValidator $rawValueValidator)
21
    {
22
        $this->rawValueValidator = $rawValueValidator;
23
    }
24
25
    /**
26
     * Validate if request query string $_GET field can be mapped to a Command
27
     * Throw CommandMappingException directly if any mapping issue
28
     * @param ServerRequestInterface $request
29
     * @param string $queryStringKey
30
     *
31
     * @return mixed
32
     * @throws CommandMappingException If any mapping validation failed
33
     */
34
    public function extractValueFromRequestQueryString(ServerRequestInterface $request, string $queryStringKey)
35
    {
36
        try {
37
            $queryParams = $request->getQueryParams();
38
39
            Assertion::isArray($queryParams);
40
            Assertion::keyExists($queryParams, $queryStringKey);
41
42
            $value = $queryParams[$queryStringKey];
43
44
            return $value;
45
        } catch (AssertionFailedException $exception) {
46
            throw CommandMappingException::fromParameter(
47
                $exception->getMessage(),
48
                $exception->getPropertyPath()
49
            );
50
        }
51
    }
52
53
    /**
54
     * Exceptions are caught in order to be processed later
55
     *
56
     * @throws CommandMappingException If any mapping validation failed
57
     * @return mixed Untouched value
58
     */
59
    public function mustBeString(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
60
    {
61
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
62
63
        return $this->rawValueValidator->mustBeString(
64
            $value,
65
            $queryStringKey,
66
            $this,
67
            $exceptionMessage
68
        );
69
    }
70
71
    /**
72
     * Exceptions are caught in order to be processed later
73
     *
74
     * @throws CommandMappingException If any mapping validation failed
75
     * @return mixed Untouched value
76
     */
77
    public function mustBeBoolean(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
78
    {
79
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
80
81
        return $this->rawValueValidator->mustBeBoolean(
82
            $value,
83
            $queryStringKey,
84
            $this,
85
            $exceptionMessage
86
        );
87
    }
88
89
    /**
90
     * Exceptions are caught in order to be processed later
91
     *
92
     * @throws CommandMappingException If any mapping validation failed
93
     * @return mixed Untouched value
94
     */
95
    public function mustBeArray(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
96
    {
97
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
98
99
        return $this->rawValueValidator->mustBeArray(
100
            $value,
101
            $queryStringKey,
102
            $this,
103
            $exceptionMessage
104
        );
105
    }
106
107
    /**
108
     * Exceptions are caught in order to be processed later
109
     *
110
     * @throws CommandMappingException If any mapping validation failed
111
     * @return mixed Untouched value
112
     */
113
    public function mustBeFloat(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
114
    {
115
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
116
117
        return $this->rawValueValidator->mustBeFloat(
118
            $value,
119
            $queryStringKey,
120
            $this,
121
            $exceptionMessage
122
        );
123
    }
124
125
    /**
126
     * Exceptions are caught in order to be processed later
127
     *
128
     * @throws CommandMappingException If any mapping validation failed
129
     * @return mixed Untouched value
130
     */
131
    public function mustBeInteger(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
132
    {
133
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
134
135
        return $this->rawValueValidator->mustBeInteger(
136
            $value,
137
            $queryStringKey,
138
            $this,
139
            $exceptionMessage
140
        );
141
    }
142
143
    /**
144
     * Exceptions are caught in order to be processed later
145
     *
146
     * @throws CommandMappingException If any mapping validation failed
147
     * @return mixed Untouched value
148
     */
149
    public function mustBeDate(ServerRequestInterface $request, string $queryStringKey, string $exceptionMessage = null)
150
    {
151
        $value = $this->extractValueFromRequestQueryString($request, $queryStringKey);
152
153
        return $this->rawValueValidator->mustBeDate(
154
            $value,
155
            $queryStringKey,
156
            $this,
157
            $exceptionMessage
158
        );
159
    }
160
161
    /**
162
     * @inheritdoc
163
     */
164
    public function createUIValidationException(string $message, string $propertyPath = null): UIValidationException
165
    {
166
        return UIValidationException::fromParameter($message, $propertyPath);
167
    }
168
}
169