2121 * Class Product_Attribute_Connection_Resolver
2222 */
2323class Product_Attribute_Connection_Resolver {
24+
25+ /**
26+ * The source from the field calling the connection.
27+ *
28+ * @var \WPGraphQL\WooCommerce\Model\Product|null
29+ */
30+ protected $ source ;
31+
32+ /**
33+ * The args input on the field calling the connection.
34+ *
35+ * @var ?array<string,mixed>
36+ */
37+ protected $ args ;
38+
39+ /**
40+ * The AppContext for the GraphQL Request
41+ *
42+ * @var \WPGraphQL\AppContext
43+ */
44+ protected $ context ;
45+
46+ /**
47+ * The ResolveInfo for the GraphQL Request
48+ *
49+ * @var \GraphQL\Type\Definition\ResolveInfo
50+ */
51+ protected $ info ;
52+
53+ /**
54+ * The attribute type.
55+ *
56+ * @var string
57+ */
58+ protected $ type ;
59+
60+ /**
61+ * Product_Attribute_Connection_Resolver constructor.
62+ *
63+ * @param \WPGraphQL\WooCommerce\Model\Product|null $source Source node.
64+ * @param array $args Connection arguments.
65+ * @param \WPGraphQL\AppContext $context AppContext object.
66+ * @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo object.
67+ * @param string $type Attribute type.
68+ */
69+ public function __construct ( $ source , array $ args , AppContext $ context , ResolveInfo $ info , string $ type = null ) {
70+ $ this ->source = $ source ;
71+ $ this ->args = $ args ;
72+ $ this ->context = $ context ;
73+ $ this ->info = $ info ;
74+ $ this ->type = $ type ;
75+ }
76+
2477 /**
2578 * Builds Product attribute items
2679 *
@@ -33,25 +86,82 @@ class Product_Attribute_Connection_Resolver {
3386 *
3487 * @throws \GraphQL\Error\UserError Invalid product attribute enumeration value.
3588 * @return array
89+ *
90+ * @deprecated TBD
3691 */
3792 private function get_items ( $ attributes , $ source , $ args , $ context , $ info , $ type = null ) {
93+ _deprecated_function ( __METHOD__ , 'TBD ' , static ::class . '::build_nodes_from_product_attributes() ' );
94+
95+ $ this ->source = $ source ;
96+ $ this ->args = $ args ;
97+ $ this ->context = $ context ;
98+ $ this ->info = $ info ;
99+ $ this ->type = $ type ;
100+
101+ return $ this ->build_nodes_from_source_attributes ();
102+ }
103+
104+ /**
105+ * Creates connection
106+ *
107+ * @param mixed $source Connection source Model instance.
108+ * @param array $args Connection arguments.
109+ * @param \WPGraphQL\AppContext $context AppContext object.
110+ * @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo object.
111+ * @param string $type Attribute type.
112+ *
113+ * @return array|null
114+ *
115+ * @deprecated TBD
116+ */
117+ public function resolve ( $ source , array $ args , AppContext $ context , ResolveInfo $ info , $ type = null ) {
118+ _deprecated_function ( __METHOD__ , 'TBD ' , static ::class . '::get_connection() ' );
119+
120+ $ this ->source = $ source ;
121+ $ this ->args = $ args ;
122+ $ this ->context = $ context ;
123+ $ this ->info = $ info ;
124+ $ this ->type = $ type ;
125+
126+ return $ this ->get_connection ();
127+ }
128+
129+ /**
130+ * Builds connection nodes from source product's attributes.
131+ *
132+ * @return array
133+ */
134+ private function build_nodes_from_source_attributes () {
38135 $ items = [];
136+ if ( ! $ this ->source ) {
137+ return $ items ;
138+ }
139+
140+ $ attributes = $ this ->source ->attributes ;
141+
142+
143+ if ( empty ( $ attributes ) ) {
144+ return $ items ;
145+ }
146+
39147 foreach ( $ attributes as $ attribute_name => $ data ) {
40148 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
41149 $ data ->_relay_id = base64_encode (
42150 $ attribute_name
43151 . GLOBAL_ID_DELIMITER
44- . $ source ->ID
152+ . $ this -> source ->ID
45153 . GLOBAL_ID_DELIMITER
46154 . $ data ->get_name ()
47155 );
48156 $ items [] = $ data ;
49157 }
50158
51- $ type = ! empty ( $ args ['where ' ]['type ' ] ) ? $ args ['where ' ]['type ' ] : $ type ;
159+ $ attribute_type = ! empty ( $ this ->args ['where ' ] ) && ! empty ( $ this ->args ['where ' ]['type ' ] )
160+ ? $ this ->args ['where ' ]['type ' ]
161+ : $ this ->type ;
52162
53- if ( ! is_null ( $ type ) ) {
54- switch ( $ type ) {
163+ if ( ! is_null ( $ attribute_type ) ) {
164+ switch ( $ attribute_type ) {
55165 case 'local ' :
56166 $ items = array_filter (
57167 $ items ,
@@ -77,20 +187,25 @@ static function ( $item ) {
77187 }
78188
79189 /**
80- * Creates connection
190+ * Builds connection nodes from woocommerce global product attributes.
81191 *
82- * @param mixed $source Connection source Model instance.
83- * @param array $args Connection arguments.
84- * @param \WPGraphQL\AppContext $context AppContext object.
85- * @param \GraphQL\Type\Definition\ResolveInfo $info ResolveInfo object.
86- * @param string $type Attribute type.
192+ * @return array
193+ */
194+ private function build_nodes_from_global_attributes () {
195+ // TODO: Implement this method.
196+ return [];
197+ }
198+
199+ /**
200+ * Builds connection from nodes array.
201+ *
202+ * @param array $nodes Array of connection nodes.
87203 *
88204 * @return array|null
89205 */
90- public function resolve ( $ source , array $ args , AppContext $ context , ResolveInfo $ info , $ type = null ) {
91- $ attributes = $ this ->get_items ( $ source ->attributes , $ source , $ args , $ context , $ info , $ type );
92-
93- $ connection = Relay::connectionFromArray ( $ attributes , $ args );
206+ private function build_connection ( $ nodes = [] ) {
207+ $ connection = $ this ->build_connection ( $ nodes );
208+ $ connection = Relay::connectionFromArray ( $ nodes , $ this ->args );
94209 $ nodes = [];
95210 if ( ! empty ( $ connection ['edges ' ] ) && is_array ( $ connection ['edges ' ] ) ) {
96211 foreach ( $ connection ['edges ' ] as $ edge ) {
@@ -100,4 +215,19 @@ public function resolve( $source, array $args, AppContext $context, ResolveInfo
100215 $ connection ['nodes ' ] = ! empty ( $ nodes ) ? $ nodes : null ;
101216 return ! empty ( $ attributes ) ? $ connection : null ;
102217 }
218+
219+ /**
220+ * Constructs the connection.
221+ *
222+ * @return array|null
223+ */
224+ public function get_connection () {
225+ if ( ! $ this ->source ) {
226+ $ attributes = $ this ->build_nodes_from_global_attributes ();
227+ } else {
228+ $ attributes = $ this ->build_nodes_from_source_attributes ();
229+ }
230+
231+ return $ this ->build_connection ( $ attributes );
232+ }
103233}
0 commit comments