PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / includes / Utils / Response / RemoteResponse.php

RemoteResponse.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at includes/Utils/Response/RemoteResponse.php

103 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Utils\Response;
4
5 /**
6 * The typed result of one outbound cloud call (spec 043 / FR-003).
7 *
8 * `ResponseNormalizer` returns this instead of the historical
9 * "array | WP_Error | [] | string" grab-bag. It is intentionally tiny: either it
10 * carries a payload, or it carries a `TemplatelyError`. Never both, never
11 * neither — that ambiguity (an empty array standing in for an error) is the
12 * INV-2 bug this contract removes.
13 */
14 class RemoteResponse {
15
16 /**
17 * @var mixed
18 */
19 private $payload;
20
21 /**
22 * @var TemplatelyError|null
23 */
24 private $error;
25
26 /**
27 * Raw HTTP status of the upstream response (0 when there was no response).
28 *
29 * @var int
30 */
31 private $status;
32
33 /**
34 * @param mixed $payload
35 * @param TemplatelyError|null $error
36 * @param int $status
37 */
38 private function __construct( $payload, $error, $status = 0 ) {
39 $this->payload = $payload;
40 $this->error = $error;
41 $this->status = (int) $status;
42 }
43
44 /**
45 * @param mixed $payload
46 * @param int $status
47 * @return RemoteResponse
48 */
49 public static function success( $payload, $status = 200 ) {
50 return new self( $payload, null, $status );
51 }
52
53 /**
54 * @param TemplatelyError $error
55 * @param int $status Upstream HTTP status; defaults to the error's own.
56 * @return RemoteResponse
57 */
58 public static function failure( TemplatelyError $error, $status = null ) {
59 return new self( null, $error, null === $status ? $error->status() : $status );
60 }
61
62 /**
63 * @return bool
64 */
65 public function is_error() {
66 return $this->error instanceof TemplatelyError;
67 }
68
69 /**
70 * The successful payload, or `null` when this is an error.
71 *
72 * @return mixed
73 */
74 public function payload() {
75 return $this->is_error() ? null : $this->payload;
76 }
77
78 /**
79 * @return TemplatelyError|null
80 */
81 public function error() {
82 return $this->error;
83 }
84
85 /**
86 * @return int upstream HTTP status.
87 */
88 public function status() {
89 return $this->status;
90 }
91
92 /**
93 * Back-compat bridge for call sites that still expect
94 * `array|WP_Error` from the old helpers. New code should branch on
95 * `is_error()` instead.
96 *
97 * @return mixed|TemplatelyError
98 */
99 public function unwrap() {
100 return $this->is_error() ? $this->error : $this->payload;
101 }
102 }
103