Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Patch new selectFields method #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 11 additions & 20 deletions internal/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,20 @@ function paginate($app, $cursor)

function selectFields($allowed, $request, $default = null)
{
$paramFields = $request->params("fields");
if (!isset($paramFields)) {
if (!is_null($default)) {
return $default;
} else {
// enforce default or default down to allowed
if (is_null($request->params("fields"))) {
if (is_null($default)) {
return $allowed;
}
} else {
$paramFields = preg_split("/\\,/i", $paramFields);
}
$fields = $allowed;
foreach ($allowed as $field) {
if ("_id" === $field) {
continue;
} else {
if (!in_array($field, $paramFields)) {
if (($key = array_search($field, $fields)) !== false) {
unset($fields[$key]);
}
}
}
return $default;
}
return $fields;
// split by comma, remove all whitespace, cast to lower case
$fields = array_map(function($input){
return trim(strtolower($input));
}, explode(',', $request->params("fields")));

// return all keys which intersect with the allowed list
return array_intersect($allowed, $fields);
}


Expand Down