Skip to content

Commit 937927e

Browse files
authored
Merge pull request #39 from Local-Data-Exchange/feature/upgrade
Syantax upgraded to support laravel 6.*
2 parents 8a907c3 + 811d980 commit 937927e

2 files changed

Lines changed: 41 additions & 39 deletions

File tree

src/ApiBuilder.php

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Lde\ApiHelper\Helpers\ObfuscationHelper;
1313
use Lde\ApiHelper\Helpers\StatsHelper;
1414
use Spatie\ArrayToXml\ArrayToXml;
15+
use Illuminate\Support\Arr;
1516

1617
class ApiBuilder
1718
{
@@ -52,11 +53,11 @@ public function api($connection)
5253
}
5354

5455
// Set the request options if provided for this conenction. Else use default ones.
55-
if (array_get($conn, 'default_request_options')) {
56+
if (Arr::get($conn, 'default_request_options')) {
5657
$additionalHeaders = $this->requestOptions['headers'];
57-
$default = array_get($conn, 'default_request_options.headers');
58+
$default = Arr::get($conn, 'default_request_options.headers');
5859
$headers = array_merge($additionalHeaders, $default);
59-
$this->requestOptions = array_get($conn, 'default_request_options');
60+
$this->requestOptions = Arr::get($conn, 'default_request_options');
6061
$this->requestOptions['headers'] = $headers;
6162
}
6263

@@ -105,7 +106,7 @@ public function __call($name, $arguments)
105106

106107
$config = config('api_helper.connections.' . $this->connection);
107108

108-
$api = array_get($config['routes'], $name);
109+
$api = Arr::get($config['routes'], $name);
109110

110111
$this->name = $this->connection . "\\" . $name;
111112
$object = new ApiResponse();
@@ -115,11 +116,11 @@ public function __call($name, $arguments)
115116
ApiCallStarting::dispatch($this->name, $api);
116117

117118
// Method
118-
$method = strtoupper(array_get($api, 'method', 'GET'));
119-
$requestType = array_get($api, 'request_type');
119+
$method = strtoupper(Arr::get($api, 'method', 'GET'));
120+
$requestType = Arr::get($api, 'request_type');
120121

121122
// Uri
122-
if (!$uri = $this->baseUrl . array_get($api, 'uri')) {
123+
if (!$uri = $this->baseUrl . Arr::get($api, 'uri')) {
123124
throw new HelperException("Uri is not configured for {$name} API!");
124125
}
125126

@@ -283,7 +284,7 @@ public function call($method, $uri, $params = [])
283284
$success = false;
284285

285286
// Check retries and fall back to global retries or fall back to 3
286-
$retries = array_get($config, 'number_of_retries', config('api_helper.retries', 3));
287+
$retries = Arr::get($config, 'number_of_retries', config('api_helper.retries', 3));
287288
$object = new ApiResponse();
288289
$xml_data = '';
289290
while ($success == false && $tries <= $retries) {
@@ -371,7 +372,7 @@ public function call($method, $uri, $params = [])
371372
$object->meta->tries = $tries;
372373

373374
// Check if we should retry
374-
$configStatus = array_get($config, 'status_not_to_retry', []);
375+
$configStatus = Arr::get($config, 'status_not_to_retry', []);
375376
$defaultStatus = [400, 401, 404, 406, 422];
376377
$statusesNotToRetry = array_merge($configStatus, $defaultStatus);
377378

@@ -439,14 +440,15 @@ public function call($method, $uri, $params = [])
439440
*/
440441
protected function processPathMappings($arguments, $api, $uri): string
441442
{
442-
foreach (array_get($api, 'mappings.path', []) as $key => $value) {
443-
$uri = str_ireplace('{' . $key . '}', array_get($arguments[0], $value, null), $uri);
443+
foreach (Arr::get($api, 'mappings.path', []) as $key => $value) {
444+
$uri = str_ireplace('{' . $key . '}', Arr::get($arguments[0], $value, null), $uri);
444445
}
445446

446447
return $uri;
447448
}
448449

449450
/**
451+
450452
* @param $arguments
451453
* @param $api
452454
* @param $uri
@@ -456,9 +458,9 @@ protected function processPathMappings($arguments, $api, $uri): string
456458
protected function processQueryMappings($arguments, $api, $uri): string
457459
{
458460
$query = [];
459-
foreach (array_get($api, 'mappings.query', []) as $key => $value) {
460-
if (array_get($arguments[0], $value) !== null) {
461-
$query[$key] = array_get($arguments[0], $value, '');
461+
foreach (Arr::get($api, 'mappings.query', []) as $key => $value) {
462+
if (Arr::get($arguments[0], $value) !== null) {
463+
$query[$key] = Arr::get($arguments[0], $value, '');
462464
}
463465
}
464466

@@ -478,16 +480,16 @@ protected function processQueryMappings($arguments, $api, $uri): string
478480
*/
479481
protected function processJsonMappings($arguments, $api): array
480482
{
481-
$json = json_encode(array_get($api, 'body', []));
483+
$json = json_encode(Arr::get($api, 'body', []));
482484
if ($json === '[]') {
483485
return [];
484486
}
485-
foreach (array_get($api, 'mappings.body', []) as $key => $value) {
487+
foreach (Arr::get($api, 'mappings.body', []) as $key => $value) {
486488

487489
//Remove array key which is nullable, Need to specify "nullable" in api_helper.php config file.
488490
if (stripos($value, 'nullable|') !== false) {
489491
$values = explode('|', $value);
490-
if (array_get($arguments[0], $values[1]) === null || array_get($arguments[0], $values[1]) === '') {
492+
if (Arr::get($arguments[0], $values[1]) === null || Arr::get($arguments[0], $values[1]) === '') {
491493
$stringToArray = json_decode($json, true);
492494
unset($stringToArray[$key]);
493495
$json = json_encode($stringToArray);
@@ -503,11 +505,11 @@ protected function processJsonMappings($arguments, $api): array
503505
if (is_callable($callable)) {
504506
$json = str_ireplace('"{' . $key . '}"', (call_user_func($callable, $arguments[0])), $json);
505507
}
506-
} elseif ($this->checkBool(array_get($arguments[0], $value))) {
508+
} elseif ($this->checkBool(Arr::get($arguments[0], $value))) {
507509
// Check boolean
508-
$json = str_ireplace('"{' . $key . '}"', array_get($arguments[0], $value, null), $json);
510+
$json = str_ireplace('"{' . $key . '}"', Arr::get($arguments[0], $value, null), $json);
509511
} else {
510-
$json = str_ireplace('{' . $key . '}', array_get($arguments[0], $value, null), $json);
512+
$json = str_ireplace('{' . $key . '}', Arr::get($arguments[0], $value, null), $json);
511513
}
512514
}
513515
$mapping = json_decode($json, true);
@@ -530,13 +532,13 @@ protected function processJsonMappings($arguments, $api): array
530532
*/
531533
protected function processFormParamsMappings($arguments, $api): array
532534
{
533-
$json = json_encode(array_get($api, 'form_params', []));
535+
$json = json_encode(Arr::get($api, 'form_params', []));
534536

535-
foreach (array_get($api, 'mappings.form_params', []) as $key => $value) {
537+
foreach (Arr::get($api, 'mappings.form_params', []) as $key => $value) {
536538
//Remove array key which is nullable, Need to specify "nullable" in api_helper.php config file.
537539
if (stripos($value, 'nullable|') !== false) {
538540
$values = explode('|', $value);
539-
if (array_get($arguments[0], $values[1]) === null || array_get($arguments[0], $values[1]) === '') {
541+
if (Arr::get($arguments[0], $values[1]) === null || Arr::get($arguments[0], $values[1]) === '') {
540542
$stringToArray = json_decode($json, true);
541543
unset($stringToArray[$key]);
542544
$json = json_encode($stringToArray);
@@ -551,11 +553,11 @@ protected function processFormParamsMappings($arguments, $api): array
551553
if (is_callable($callable)) {
552554
$json = str_ireplace('"{' . $key . '}"', (call_user_func($callable, $arguments[0])), $json);
553555
}
554-
} elseif ($this->checkBool(array_get($arguments[0], $value))) {
556+
} elseif ($this->checkBool(Arr::get($arguments[0], $value))) {
555557
// Check boolean
556-
$json = str_ireplace('"{' . $key . '}"', array_get($arguments[0], $value, null), $json);
558+
$json = str_ireplace('"{' . $key . '}"', Arr::get($arguments[0], $value, null), $json);
557559
} else {
558-
$json = str_ireplace('{' . $key . '}', array_get($arguments[0], $value, null), $json);
560+
$json = str_ireplace('{' . $key . '}', Arr::get($arguments[0], $value, null), $json);
559561
}
560562
}
561563
return json_decode($json, true);
@@ -571,20 +573,20 @@ protected function processXmlMappings($arguments, $api): string
571573
{
572574
// get xml config
573575
$rootElementName = (!empty($api['xml_config']['root_element_name'])) ? $api['xml_config']['root_element_name'] : ((!empty(config('api_helper.connections.' . $this->connection . '.root'))) ? config('api_helper.connections.' . $this->connection . 'root') : 'request');
574-
$attributes = array_get($api, 'xml_config.attributes');
575-
$useUnderScores = array_get($api, 'xml_config.use_underscores', true);
576-
$encoding = array_get($api, 'xml_config.encoding', true);
576+
$attributes = Arr::get($api, 'xml_config.attributes');
577+
$useUnderScores = Arr::get($api, 'xml_config.use_underscores', true);
578+
$encoding = Arr::get($api, 'xml_config.encoding', true);
577579

578-
$xml = ArrayToXml::convert(array_get($api, 'body', []), [
580+
$xml = ArrayToXml::convert(Arr::get($api, 'body', []), [
579581
'rootElementName' => $rootElementName,
580582
'_attributes' => $attributes,
581583
], $useUnderScores, $encoding);
582584

583-
foreach (array_get($api, 'mappings.body', []) as $key => $value) {
585+
foreach (Arr::get($api, 'mappings.body', []) as $key => $value) {
584586
// TODO: we can add more support like validator
585587
if (stripos($value, 'nullable|') !== false) {
586588
$values = explode('|', $value);
587-
if (array_get($arguments[0], $values[1]) === null || array_get($arguments[0], $values[1]) === '') {
589+
if (Arr::get($arguments[0], $values[1]) === null || Arr::get($arguments[0], $values[1]) === '') {
588590
$xml = str_ireplace('<' . $key . '>{' . $key . '}</' . $key . '>', '', $xml);
589591
continue;
590592
} else {
@@ -597,16 +599,16 @@ protected function processXmlMappings($arguments, $api): string
597599
if (is_callable($callable)) {
598600
$xml = str_ireplace('{' . $key . '}', $this->escapeSpecialCharacters((call_user_func($callable, $arguments[0]))), $xml);
599601
}
600-
} elseif ($this->checkBool(array_get($arguments[0], $value))) {
602+
} elseif ($this->checkBool(Arr::get($arguments[0], $value))) {
601603
// Check boolean
602-
if (!empty(array_get($arguments[0], $value))) {
603-
$xml = str_ireplace('{' . $key . '}', array_get($arguments[0], $value), $xml);
604+
if (!empty(Arr::get($arguments[0], $value))) {
605+
$xml = str_ireplace('{' . $key . '}', Arr::get($arguments[0], $value), $xml);
604606
} else {
605607
$xml = str_ireplace('<' . $key . '>{' . $key . '}</' . $key . '>', '', $xml);
606608
}
607609
} else {
608-
if (!empty(array_get($arguments[0], $value))) {
609-
$xml = str_ireplace('{' . $key . '}', $this->escapeSpecialCharacters(array_get($arguments[0], $value)), $xml);
610+
if (!empty(Arr::get($arguments[0], $value))) {
611+
$xml = str_ireplace('{' . $key . '}', $this->escapeSpecialCharacters(Arr::get($arguments[0], $value)), $xml);
610612
} else {
611613
$xml = str_ireplace('<' . $key . '>{' . $key . '}</' . $key . '>', '', $xml);
612614
}
@@ -665,7 +667,7 @@ protected function maskFieldValues(array &$data, array $paths)
665667

666668
foreach ($paths as $field) {
667669

668-
$string = array_get($data, $field);
670+
$string = Arr::get($data, $field);
669671

670672
if (stripos($string, '@') !== false) {
671673
$obfuscatedString = ObfuscationHelper::obfuscate($string, 4);

src/Helpers/ObfuscationHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static function obfuscate($string, $visibleChars = 4)
1515
public static function obfuscateEmail($email)
1616
{
1717
$em = explode("@", $email);
18-
$name = implode(array_slice($em, 0, (count($em) - 1)), "@");
18+
$name = implode('@', array_slice($em, 0, (count($em) - 1)));
1919
$len = (strlen($name) - 1);
2020
return substr($name, 0, 1) . str_repeat('*', $len) . "@" . end($em);
2121
}

0 commit comments

Comments
 (0)