1- <?php namespace Swipbox ;
1+ <?php
2+ namespace Swipbox ;
23
4+ /**
5+ * SwipBox PHP API Integration
6+ *
7+ * SwipBox is a new shipping option, primarely focused on e-commerce solutions.
8+ *
9+ * This is a PHP composer package for simplifying SwipBox integration
10+ * into PHP based webshops.
11+ *
12+ * @author Dan Storm <[email protected] > 13+ * @link http://www.catalystcode.net
14+ */
15+
316class Client
417{
518 private $ _http , $ _guid ;
619
20+ /**
21+ * The constructor needs your Webshop GUID provided by SwipBox
22+ * http://www.swipbox.com/
23+ *
24+ * By setting the second parameter to (boolean) true you enable
25+ * testing mode.
26+ *
27+ * @param string $guid The Webshop GUID provided by Swipbox
28+ * @param boolean $test Set to true if you are testing. Defaults to false
29+ */
730 public function __construct ($ guid , $ test = false )
831 {
932 if ( $ test )
@@ -19,21 +42,159 @@ public function __construct($guid, $test = false)
1942 $ this ->_guid = $ guid ;
2043 }
2144
22- public function parcel ()
45+ /**
46+ * Creates a parcel
47+ *
48+ * @param Array $params An array of parameters as specified in the docs.
49+ * @throws \Swipbox\Exception
50+ * @return Array A decoded JSON array
51+ */
52+ public function create ( Array $ params )
2353 {
24- return new \ Swipbox \ Parcel ( $ this );
54+ return $ this -> _execute ( ' create ' , $ params );
2555 }
26-
27- public function stations ()
56+
57+ /**
58+ * Activates a parcel
59+ *
60+ * @param Array $params An array of parameters as specified in the docs.
61+ * @throws \Swipbox\Exception
62+ * @return Mixed A decoded JSON array, XML document or PDF
63+ */
64+ public function activate ( Array $ params )
65+ {
66+ return $ this ->_execute ('activate ' , $ params );
67+ }
68+
69+ /**
70+ * Gets the label for an activated parcel
71+ *
72+ * @param Array $params An array of parameters as specified in the docs.
73+ * @throws \Swipbox\Exception
74+ * @return Mixed A decoded JSON array, XML document or PDF
75+ */
76+ public function get_label ( Array $ params )
2877 {
29- return new \ Swipbox \ Stations ( $ this );
78+ return $ this -> _execute ( ' get_label ' , $ params );
3079 }
31-
32-
33- public function _execute ($ service_name , $ data )
80+
81+ /**
82+ * Cancels a parcel based on an activated parcels barcode
83+ *
84+ * @param Array $params An array of parameters as specified in the docs.
85+ * @throws \Swipbox\Exception
86+ * @return Array A decoded JSON array
87+ */
88+ public function cancel ( Array $ params )
89+ {
90+ return $ this ->_execute ('cancel ' , $ params );
91+ }
92+
93+ /**
94+ * Find the nearest parcel recieving stations based on specific
95+ * address.
96+ *
97+ * @param Array $params An array of parameters as specified in the docs.
98+ * @throws \Swipbox\Exception
99+ * @return Array A decoded JSON array
100+ */
101+ public function find_nearest ( Array $ params )
102+ {
103+ return $ this ->_execute ('find_nearest ' , $ params );
104+ }
105+
106+ /**
107+ * Gets favorite parcel stations attached to a customer.
108+ *
109+ * @param Array $params An array of parameters as specified in the docs.
110+ * @throws \Swipbox\Exception
111+ * @return Array A decoded JSON array
112+ */
113+ public function find_active_favorites ( Array $ params )
114+ {
115+ return $ this ->_execute ('find_active_favorites ' , $ params );
116+ }
117+
118+ /**
119+ * Gets parcel stations near to the first favorite stations
120+ * attached to a customer.
121+ *
122+ * @param Array $params An array of parameters as specified in the docs.
123+ * @throws \Swipbox\Exception
124+ * @return Array A decoded JSON array
125+ */
126+ public function find_near_to_favorite ( Array $ params )
127+ {
128+ return $ this ->_execute ('find_active_favorites ' , $ params );
129+ }
130+
131+ /**
132+ * Simpler method to get parcel stations by only providing a zip code.
133+ *
134+ * @param Array $params An array of parameters as specified in the docs.
135+ * @throws \Swipbox\Exception
136+ * @return Array A decoded JSON array
137+ */
138+ public function find_by_zip ( Array $ params )
139+ {
140+ return $ this ->_execute ('find_by_zip ' , $ params );
141+ }
142+
143+ /**
144+ * Track the parcels trip
145+ *
146+ * @param Array $params An array of parameters as specified in the docs.
147+ * @throws \Swipbox\Exception
148+ * @return Array A decoded JSON array
149+ */
150+ public function track ( Array $ params )
151+ {
152+ return $ this ->_execute ('track ' , $ params );
153+ }
154+ /**
155+ * Gets parcel stations near to the first favorite stations
156+ * attached to a customer.
157+ *
158+ * @param Array $params An array of parameters as specified in the docs.
159+ * @throws \Swipbox\Exception
160+ * @return Array A decoded JSON array
161+ */
162+ private function _execute ($ service_name , $ data )
34163 {
35- $ request = $ this ->_http ->get ($ service_name .'? ' .http_build_query (array_merge ($ data , array ('guid ' => $ this ->_guid ))))->send ();
36- return $ request ;
164+ try
165+ {
166+ $ response = $ this ->_http ->get ($ service_name .'? ' .http_build_query (array_merge ($ data , array ('guid ' => $ this ->_guid ))))->send ();
167+ }
168+ catch (Exception $ e )
169+ {
170+ return false ;
171+ }
172+
173+ //application/json
174+ //application/xml
175+ //application/pdf
176+ if ($ response ->isSuccessful ())
177+ {
178+ if (stristr ($ response ->getContentType (), 'application/json ' ))
179+ {
180+ $ data = $ response ->json ();
181+ if (isset ($ data ['error ' ]))
182+ {
183+ throw new \Swipbox \Exception ($ data ['error ' ]['description ' ], $ data ['error ' ]['code ' ]);
184+ }
185+ return $ data ;
186+ }
187+ elseif (stristr ($ response ->getContentType (), 'application/xml ' ))
188+ {
189+ return $ response ->xml ();
190+ }
191+ elseif (stristr ($ response ->getContentType (), 'application/pdf ' ))
192+ {
193+ return $ response ->getBody ();
194+ }
195+ }
196+
197+ return false ;
37198 }
38199
39200
0 commit comments