| @@ -7,10 +7,15 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace SeQura\WC\Controllers\Rest; |
| 10 | 10 | |
| 11 | -use SeQura\WC\Services\Interface_Logger_Service; | |
| 11 | +use SeQura\Core\BusinessLogic\AdminAPI\Response\Response; | |
| 12 | +use SeQura\Core\Infrastructure\Utility\RegexProvider; | |
| 13 | +use SeQura\WC\Services\Log\Interface_Logger_Service; | |
| 14 | +use WP; | |
| 15 | +use WP_Error; | |
| 12 | 16 | use WP_REST_Request; |
| 17 | +use WP_REST_Response; | |
| 13 | 18 | |
| 14 | 19 | /** |
| 15 | 20 | * Helper for REST Controllers |
| 16 | 21 | */ |
| @@ -26,14 +31,23 @@ | ||
| 26 | 31 | */ |
| 27 | 32 | protected $logger; |
| 28 | 33 | |
| 29 | 34 | /** |
| 35 | + * RegEx service | |
| 36 | + * | |
| 37 | + * @var RegexProvider | |
| 38 | + */ | |
| 39 | + protected $regex; | |
| 40 | + | |
| 41 | + /** | |
| 30 | 42 | * Constructor. |
| 31 | 43 | * |
| 32 | 44 | * @param Interface_Logger_Service $logger The logger service. |
| 45 | + * @param RegexProvider $regex The regex provider. | |
| 33 | 46 | */ |
| 34 | - public function __construct( Interface_Logger_Service $logger ) { | |
| 47 | + public function __construct( Interface_Logger_Service $logger, RegexProvider $regex ) { | |
| 35 | 48 | $this->logger = $logger; |
| 49 | + $this->regex = $regex; | |
| 36 | 50 | } |
| 37 | 51 | |
| 38 | 52 | /** |
| 39 | 53 | * Check if the current user can manage options. |
| @@ -139,10 +153,9 @@ | ||
| 139 | 153 | * @param WP_REST_Request $request The request. |
| 140 | 154 | * @param string $key The key. |
| 141 | 155 | */ |
| 142 | 156 | public function validate_ip_list( $param, $request, $key ): bool { |
| 143 | - // phpcs:ignore Generic.Files.LineLength.TooLong | |
| 144 | - $ip_regex = '/^(((25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?)\.(25[0-5]|2[0-4]\d|[01]?\d\d?))|([0-9a-fA-F]{1,4}:){7}([0-9a-fA-F]{1,4}))$/'; | |
| 157 | + $ip_regex = $this->regex->getIpRegex(); | |
| 145 | 158 | if ( ! is_array( $param ) ) { |
| 146 | 159 | return false; |
| 147 | 160 | } |
| 148 | 161 | foreach ( $param as $ip ) { |
| @@ -160,10 +173,9 @@ | ||
| 160 | 173 | * @param WP_REST_Request $request The request. |
| 161 | 174 | * @param string $key The key. |
| 162 | 175 | */ |
| 163 | 176 | public function validate_time_duration( $param, $request, $key ): bool { |
| 164 | - // phpcs:ignore Generic.Files.LineLength.TooLong | |
| 165 | - $regex = '/^((?:\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|1\d|2[0-8]))|(?:\d{4}-(?:0[13-9]|1[0-2])-(?:29|30))|(?:\d{4}-(?:0[13578]|1[012])-(?:31))|(?:\d{2}(?:[02468][048]|[13579][26])-(?:02)-29)|(P(?:\d+Y)?(?:\d+M)?(?:\d+W)?(?:\d+D)?(?:T(?:\d+H)?(?:\d+M)?(?:\d+S)?)?))$/'; | |
| 177 | + $regex = $this->regex->getDateOrDurationRegex(); | |
| 166 | 178 | return is_string( $param ) && preg_match( $regex, $param ) === 1 && 'P' !== $param && ! str_ends_with( $param, 'T' ); |
| 167 | 179 | } |
| 168 | 180 | |
| 169 | 181 | /** |
| @@ -298,6 +310,29 @@ | ||
| 298 | 310 | return '(?P<' . $param_name . '>[\w]+)'; |
| 299 | 311 | default: |
| 300 | 312 | return ''; |
| 301 | 313 | } |
| 314 | + } | |
| 315 | + | |
| 316 | + /** | |
| 317 | + * Build a valid response for the REST API | |
| 318 | + * | |
| 319 | + * @return WP_REST_Response|WP_Error | |
| 320 | + */ | |
| 321 | + protected function build_response( Response $response ) { | |
| 322 | + return $this->build_response_from_array( $response->toArray(), $response->isSuccessful() ); | |
| 323 | + } | |
| 324 | + | |
| 325 | + /** | |
| 326 | + * Build a valid response for the REST API from an array | |
| 327 | + * | |
| 328 | + * @return WP_REST_Response|WP_Error | |
| 329 | + */ | |
| 330 | + protected function build_response_from_array( array $response_array, bool $is_successful ) { | |
| 331 | + if ( ! $is_successful ) { | |
| 332 | + $code = strval( $response_array['statusCode'] ?? '500' ); | |
| 333 | + $message = strval( $response_array['errorMessage'] ?? 'Unknown error' ); | |
| 334 | + return new WP_Error( $code, $message ); | |
| 335 | + } | |
| 336 | + return \rest_ensure_response( $response_array ); | |
| 302 | 337 | } |
| 303 | 338 | } |