PluginProbe
WooCommerce Square / 3.4.2
WooCommerce Square v3.4.2
5.5.0 5.4.3 5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 All 132 releases
woocommerce-square / includes / API / Response.php
Response.php
146 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce Square
4 *
5 * This source file is subject to the GNU General Public License v3.0
6 * that is bundled with this package in the file license.txt.
7 * It is also available through the world-wide-web at this URL:
8 * http://www.gnu.org/licenses/gpl-3.0.html
9 * If you did not receive a copy of the license and are unable to
10 * obtain it through the world-wide-web, please send an email
11 * to license@woocommerce.com so we can send you a copy immediately.
12 *
13 * DISCLAIMER
14 *
15 * Do not edit or add to this file if you wish to upgrade WooCommerce Square to newer
16 * versions in the future. If you wish to customize WooCommerce Square for your
17 * needs please refer to https://docs.woocommerce.com/document/woocommerce-square/
18 *
19 * @author WooCommerce
20 * @copyright Copyright: (c) 2019, Automattic, Inc.
21 * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
22 */
23
24 namespace WooCommerce\Square\API;
25 use \WooCommerce\Square\Framework\Api\API_Response;
26
27 defined( 'ABSPATH' ) || exit;
28
29 /**
30 * Base WooCommerce Square API response object.
31 *
32 * @since 2.0.0
33 */
34 class Response implements API_Response {
35 /** @var mixed raw response data */
36 protected $raw_response_data;
37
38 /**
39 * Constructs the response object.
40 *
41 * @since 2.0.0
42 *
43 * @param Object|array $raw_response_data
44 */
45 public function __construct( $raw_response_data ) {
46
47 $this->raw_response_data = $raw_response_data;
48 }
49
50
51 /**
52 * Gets the response data.
53 *
54 * @since 2.0.0
55 *
56 * @return Object
57 */
58 public function get_data() {
59
60 return $this->raw_response_data ?: null;
61 }
62
63
64 /**
65 * Gets errors returned by the Square API.
66 *
67 * @since 2.0.0
68 *
69 * @return \stdClass[]
70 */
71 public function get_errors() {
72 if ( is_array( $this->raw_response_data ) && count( $this->raw_response_data ) > 0 ) {
73 if ( $this->raw_response_data[0] instanceof \Square\Models\Error ) {
74 return $this->raw_response_data;
75 }
76 }
77
78 return array();
79 }
80
81
82 /**
83 * Determines if the API response contains errors.
84 *
85 * @since 2.0.0
86 *
87 * @return bool
88 */
89 public function has_errors() {
90
91 return ! empty( $this->get_errors() );
92 }
93
94
95 /**
96 * Determines if the API response contains a particular error code.
97 *
98 * @since 2.1.6
99 * @param $error \Square\Models\Error
100 * @return bool
101 */
102 public function has_error_code( $error_code ) {
103 foreach ( $this->get_errors() as $error ) {
104 if ( $error_code === $error->getCode() ) {
105 return true;
106 }
107 }
108 return false;
109 }
110
111
112 /**
113 * Gets the response data as a string.
114 *
115 * @since 2.0.0
116 *
117 * @return string
118 */
119 public function to_string() {
120 $response_data = $this->get_data();
121
122 if ( is_callable( array( $response_data, '__toString' ) ) ) {
123 return $this->get_data();
124 } else if ( is_callable( array( $response_data, 'jsonSerialize' ) ) ) {
125 return wp_json_encode( $response_data, JSON_PRETTY_PRINT );
126 }
127
128 return '';
129 }
130
131
132 /**
133 * Gets the response data a string with all sensitive information masked.
134 *
135 * @since 2.0.0
136 *
137 * @return string
138 */
139 public function to_string_safe() {
140
141 return $this->to_string();
142 }
143
144
145 }
146