Skip to content

Commit

Permalink
Moved the field check out of the config (#76)
Browse files Browse the repository at this point in the history
Replaced the key return with getAttribute
  • Loading branch information
petermein authored and johannesschobel committed Feb 11, 2018
1 parent e7d6e7d commit 66a8a8a
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions Traits/HashIdTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

use App\Ship\Exceptions\IncorrectIdException;
use Illuminate\Support\Facades\Config;
use function is_null;
use Route;
use function strtolower;
use Vinkla\Hashids\Facades\Hashids;
use function is_null;
use function strtolower;

/**
* Class HashIdTrait.
Expand Down Expand Up @@ -36,20 +36,19 @@ trait HashIdTrait
*/
public function getHashedKey($field = null)
{
// if no key is set, use the default key name (i.e., id)
if ($field === null) {
$field = $this->getKeyName();
}

// hash the ID only if hash-id enabled in the config
if (Config::get('apiato.hash-id')) {

// if no key is set, use the default key name (i.e., id)
if ($field === null) {
$field = $this->getKeyName();
}

// we need to get the VALUE for this KEY (model field)
$value = $this->getAttribute($field);
return $this->encoder($value);
}

return $this->getKey();
return $this->getAttribute($field);
}

/**
Expand Down Expand Up @@ -102,7 +101,7 @@ private function locateAndDecodeIds($requestData, $key)
private function processField($data, $keysTodo)
{
// check if there are no more fields to be processed
if(empty($keysTodo)) {
if (empty($keysTodo)) {
// there are no more keys left - so basically we need to decode this entry
$decodedId = $this->decode($data);
return $decodedId;
Expand All @@ -112,21 +111,20 @@ private function processField($data, $keysTodo)
$field = array_shift($keysTodo);

// is the current field an array?! we need to process it like crazy
if($field == '*') {
if ($field == '*') {
//make sure field value is an array
$data = is_array($data) ? $data : [$data];

// process each field of the array (and go down one level!)
$fields = $data;
foreach($fields as $key => $value) {
foreach ($fields as $key => $value) {
$data[$key] = $this->processField($value, $keysTodo);
}
return $data;

}
else {
} else {
// check if the key we are looking for does, in fact, really exist
if(! array_key_exists($field, $data)) {
if (!array_key_exists($field, $data)) {
return $data;
}

Expand Down Expand Up @@ -187,7 +185,7 @@ public function decodeArray(array $ids)
public function decode($id, $parameter = null)
{
// check if passed as null, (could be an optional decodable variable)
if(is_null($id) || strtolower($id) == 'null'){
if (is_null($id) || strtolower($id) == 'null') {
return $id;
}

Expand Down

0 comments on commit 66a8a8a

Please sign in to comment.