Skip to content

Commit 10fd3a5

Browse files
committed
Fix Bugs for #40, #46, #41, #42
1 parent 4e5370a commit 10fd3a5

File tree

9 files changed

+149
-22
lines changed

9 files changed

+149
-22
lines changed

src/Core/Http/Serialization/JsonObjectSerializer.php

+39-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use QuickBooksOnline\API\Diagnostics\TraceLogger;
77
use QuickBooksOnline\API\Exception\IdsExceptionManager;
88
use QuickBooksOnline\API\Core\CoreConstants;
9+
use QuickBooksOnline\API\Facades\FacadeHelper;
910

1011
/**
1112
* Json Serializer to serialize and de serialize.
@@ -163,8 +164,44 @@ public function getResourceURL()
163164
public function Serialize($entity)
164165
{
165166
$this->collectResourceURL($entity);
166-
//TODO pre-processing for objects (ones which lack support in PHP 5.2)
167-
return $this->checkResult(json_encode($entity));
167+
$arrayObj = $this->customerConvertObjectToArray($entity);
168+
$array = $this->removeNullProperties($arrayObj);
169+
return $this->checkResult(json_encode($array, true));
170+
}
171+
172+
private function customerConvertObjectToArray($obj){
173+
if(is_object($obj)) $obj = (array) $obj;
174+
if(is_array($obj)) {
175+
$new = array();
176+
foreach($obj as $key => $val) {
177+
$new[$key] = $this->customerConvertObjectToArray($val);
178+
}
179+
}
180+
else $new = $obj;
181+
return $new;
182+
}
183+
184+
/**
185+
* The input will always be an asscoiate array
186+
* So we will judge based on this two situration
187+
*/
188+
private function removeNullProperties($val){
189+
$filterArray = array_filter($val);
190+
$returned = array();
191+
foreach($filterArray as $k => $v){
192+
if(is_array($v)){
193+
if(FacadeHelper::isRecurrsiveArray($v)){
194+
$list = array();
195+
foreach($v as $kk => $vv){
196+
$list[] = array_filter($vv);
197+
}
198+
$returned[$k] = $list;
199+
}
200+
}else{
201+
$returned[$k] = $v;
202+
}
203+
}
204+
return $returned;
168205
}
169206

170207
/**

src/Core/HttpClients/FaultHandler.php

+40-4
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,46 @@ public function getIntuitErrorDetail(){
111111
return $this->intuitErrorDetail;
112112
}
113113
public function parseResponse($message){
114+
if(!$this->isValidXml($message)){
115+
return;
116+
}
117+
114118
$xmlObj = simplexml_load_string($message);
115-
$this->intuitErrorType = (string)$xmlObj->Fault->attributes()['type'];
116-
$this->intuitErrorCode = (string)$xmlObj->Fault->Error->attributes()['code'];
117-
$this->intuitErrorMessage = (string)$xmlObj->Fault->Error->Message;
118-
$this->intuitErrorDetail = (string)$xmlObj->Fault->Error->Detail;
119+
$type = (string)$xmlObj->Fault->attributes()['type'];
120+
if(isset($type) && !empty($type)){
121+
$this->intuitErrorType = $type;
122+
}
123+
124+
$code = (string)$xmlObj->Fault->Error->attributes()['code'];
125+
if(isset($code) && !empty($code)){
126+
$this->intuitErrorCode = $code;
127+
}
128+
129+
$message = (string)$xmlObj->Fault->Error->Message;
130+
if(isset($message) && !empty($message)){
131+
$this->intuitErrorMessage = $message;
132+
}
133+
134+
$detail = (string)$xmlObj->Fault->Error->Detail;
135+
if(isset($detail) && !empty($detail)){
136+
$this->intuitErrorDetail = $detail;
137+
}
138+
139+
}
140+
141+
private function isValidXml($content)
142+
{
143+
$content = trim($content);
144+
if (empty($content)) {
145+
return false;
146+
}
147+
148+
libxml_use_internal_errors(true);
149+
simplexml_load_string($content);
150+
$errors = libxml_get_errors();
151+
libxml_clear_errors();
152+
153+
return empty($errors);
119154
}
155+
120156
}

src/Core/HttpClients/IntuitResponse.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function __construct($passedHeaders, $passedBody, $passedHttpResponseCode
3434
$this->faultHandler->setResponseBody($this->body);
3535
$this->faultHandler->parseResponse($this->body);
3636
//Manually set the error message
37-
$this->faultHandler->setOAuthHelperError("Invalid auth/bad request (got a 401, expected HTTP/1.1 20X or a redirect)");
37+
$this->faultHandler->setOAuthHelperError("Invalid auth/bad request (got a " .$passedHttpResponseCode . ", expected HTTP/1.1 20X or a redirect)");
3838
}
3939
}else{
4040
throw new SdkException("Passed Http status code is null.");

src/Data/IPPSalesFormsPrefs.php

+12
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,18 @@ public function __construct($keyValInitializers=array(), $verbose=false)
285285
* @var boolean
286286
*/
287287
public $PrintItemWithZeroAmount;
288+
289+
/**
290+
* @Definition
291+
Product:QBO
292+
If Email Copy To Company
293+
* @xmlType element
294+
* @xmlNamespace http://schema.intuit.com/finance/v3
295+
* @xmlMinOccurs 0
296+
* @xmlName EmailCopyToCompany
297+
* @var boolean
298+
*/
299+
public $EmailCopyToCompany;
288300
/**
289301
* @Definition
290302
Product:QBW

src/DataService/Batch.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function __construct($serviceContext, $restHandler)
9595
*/
9696
public function Count()
9797
{
98-
return count($this->batchRequest);
98+
return count($this->batchRequests);
9999
}
100100

101101
/**

src/Facades/TaxRate.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace QuickBooksOnline\API\Facades;
3+
4+
class TaxRate{
5+
6+
public static function create(array $data, $throwException = TRUE){
7+
if(!isset($data) || empty($data)) throw new \Exception("Passed array for creating TaxRate is Empty.");
8+
$TaxRateObject = FacadeHelper::reflectArrayToObject("TaxRateDetails", $data, $throwException );
9+
TaxRate::changeTaxApplicationOnValueToString($TaxRateObject);
10+
return $TaxRateObject;
11+
}
12+
13+
private static function changeTaxApplicationOnValueToString($TaxServiceObject){
14+
$val = $TaxServiceObject->TaxApplicableOn;
15+
if(is_object($val)){
16+
if(isset($val->value)){
17+
$newValue = $val->value;
18+
$TaxServiceObject->TaxApplicableOn = $newValue;
19+
}
20+
}
21+
}
22+
23+
}

src/Facades/TaxService.php

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace QuickBooksOnline\API\Facades;
3+
4+
class TaxService{
5+
6+
public static function create(array $data, $throwException = TRUE){
7+
if(!isset($data) || empty($data)) throw new \Exception("Passed array for creating TaxService is Empty.");
8+
$TaxServiceObject = FacadeHelper::reflectArrayToObject("TaxService", $data, $throwException );
9+
return $TaxServiceObject;
10+
}
11+
}

src/XSD2PHP/src/com/mikebevz/xsd2php/Php2Xml.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ private function parseObjectValue($obj, $element)
311311
}
312312
} else {
313313
//SDK-39
314-
$el = $this->dom->createElement($code.":".$propDocs['xmlName'], $value);
314+
$el = $this->dom->createElement($code.":".$propDocs['xmlName'], htmlspecialchars($value));
315315
$element->appendChild($el);
316316
}
317317
//print_r("Added element ".$propDocs['xmlName']." with NS = ".$propDocs['xmlNamespace']." \n");

src/_Samples/TaxServiceExample.php

+21-13
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
use QuickBooksOnline\API\DataService\DataService;
88
use QuickBooksOnline\API\PlatformService\PlatformService;
99
use QuickBooksOnline\API\Core\Http\Serialization\XmlObjectSerializer;
10-
use QuickBooksOnline\API\Data\IPPTaxRateDetails;
11-
use QuickBooksOnline\API\Data\IPPTaxService;
10+
use QuickBooksOnline\API\Facades\TaxService;
11+
use QuickBooksOnline\API\Facades\TaxRate;
12+
1213

1314
// Prep Data Services
1415
$dataService = DataService::Configure(array(
@@ -23,19 +24,26 @@
2324

2425
$dataService->setLogLocation("/Users/hlu2/Desktop/newFolderForLog");
2526

27+
$TaxRateDetails = array();
2628
$rnd = rand();
27-
$taxRateDetails = new IPPTaxRateDetails();
28-
$taxRateDetails->TaxRateName = "myNewTaxRateName_$rnd";
29-
$taxRateDetails->RateValue = "7777";//As Invalid number
30-
$taxRateDetails->TaxAgencyId = "1";
31-
$taxRateDetails->TaxApplicableOn = "Sales";
32-
33-
$taxService = new IPPTaxService();
34-
$taxService->TaxCode = 'MyTaxCodeName_' . $rnd;
35-
$taxService->TaxRateDetails = array($taxRateDetails);
29+
for($int = 1; $int <=2 ; $int++){
30+
$rateValue = $int + 5;
31+
$currentTaxServiceDetail = TaxRate::create([
32+
"TaxRateName" => "myNewTaxRateName_" . $int . "_$rnd",
33+
"RateValue" => $rateValue,
34+
"TaxAgencyId" => "1",
35+
"TaxApplicableOn" => "Sales"
36+
]);
37+
$TaxRateDetails[] = $currentTaxServiceDetail;
38+
}
3639

40+
$TaxService = TaxService::create([
41+
"TaxCode" => "TestValue_$rnd",
42+
"TaxRateDetails" => $TaxRateDetails
43+
]);
3744

38-
$result = $dataService->Add($taxService);
45+
var_dump($TaxService);
46+
$result = $dataService->Add($TaxService);
3947
$error = $dataService->getLastError();
4048
if ($error != null) {
4149
echo "The Status code is: " . $error->getHttpStatusCode() . "\n";
@@ -45,7 +53,7 @@
4553
}
4654

4755

48-
print_r($result);
56+
print_r($result->TaxService->TaxCodeId);
4957

5058

5159

0 commit comments

Comments
 (0)