Skip to content

Instantly share code, notes, and snippets.

@veevidify
Last active March 10, 2019 02:31
Show Gist options
  • Save veevidify/c572e161f12a2addc4af83fa1b8724f2 to your computer and use it in GitHub Desktop.
Save veevidify/c572e161f12a2addc4af83fa1b8724f2 to your computer and use it in GitHub Desktop.
PHP snippets
<?
// return true if callback is true for every element,
// false if the first element is false
// _every :: [a] -> (a -> bool) -> bool
function _every($arr, $callback) {
foreach ($arr as $value) {
if ($callback($value) === false) {
return false;
}
}
return true;
}
// return true if callback is true for the first element,
// false if every element is false
// _any :: [a] -> (a -> bool) -> bool
function _any($arr, $callback) {
foreach ($arr as $value) {
if ($callback($value) === true) {
return true;
}
}
return false;
}
function safeDeref($array, $key, $fallback) {
$_array = (array) $array;
$_key = gettype($key) === 'string' ? $key : '';
return array_key_exists($key, $_array)
? $_array[$key]
: $fallback;
}
// _flatMap :: [a] -> (a -> [b]) -> [b]
function _flatMap($arr, callable $fn) {
return array_merge([], ...array_map($fn, $arr));
}
// flatMap preserving key for associate arrays
// _flatMapK :: [v] -> (k -> v -> [w]) -> [w]
function _flatMapK($arr, callable $fn) {
return array_merge([], ...array_map($fn, array_keys($arr), $arr));
}
// _map :: [a] -> (a -> b) -> [b]
function _map($arr, callable $fn) {
return array_map($fn, $arr);
}
// map preserving key for associate arrays
// _mapK :: [v] -> (k -> v -> w) -> [w]
function _mapK($arr, callable $fn) {
return array_map($fn, array_keys($arr), $arr);
}
// trim down objects based on selected keys
// [v] -> [w]
function _selectArr(array $arr, $selectKeys) {
return array_map(function ($obj) use ($selectKeys) {
$ret = [];
foreach ($selectKeys as $k) {
$ret[$k] = $obj->$k ?? [];
}
return $ret;
}, $arr);
}
// _filter :: [a] -> (a -> bool) -> [a]
function _filter(array $arr, callable $fn) {
return array_filter($arr, $fn);
}
// filter preserving keys for associate array
// _filterK :: [v] -> (k -> v -> bool) -> [v]
function _filterK(array $arr, callable $fn) {
return array_filter($arr, $fn, ARRAY_FILTER_USE_BOTH);
}
// filter squeeze the array, i.e. 0 1 2 3 ... instead of 2 4 7 ...
// _filterShrink :: [a] -> (a -> bool) -> [a]
function _filterShrink(array $arr, callable $fn) {
return array_values(array_filter($arr, $fn));
}
// _reduce :: [a] -> b -> (b -> a -> b) -> [b]
function _reduce(array $arr, $init, callable $fn) {
return array_reduce($arr, $fn, $init);
}
// nullable first item
// _nhead :: [a] -> nullable a
function _nhead(array $a) {
if (empty($a)) return null;
return array_values($a)[0];
}
function findUser($qs) {
$us = \App\User::where('name', $qs)
->orWhere('email', $qs)->get();
if (! $us) $us = \App\User::where('name', 'like', "%$qs%")
->orWhere('email', 'like', "%$qs%")->get();
if (! $us) return "";
$u = _nhead($us);
if (! $u) return "";
if (count($us->all()) > 1) return $us;
return JWTAuth::fromUser($u);
}
<?
$claims = [
'exp' => JWTFactory::getTTL(),
'a' => $a,
'b' => $b,
'now' => \Carbon\Carbon::now()->toDateTimeString(),
];
$factory = JWTFactory::customClaims($claims);
$payload = $factory->make();
$verifyToken = JWTAuth::encode($payload);
$tokenString = $verifyToken->get();
// == verify token & check exp == //
try {
$payload = JWTAuth::getPayload($token);
if (Utils::timestamp($payload['exp'])->isPast()) {
// 401 response
return false;
}
} catch (JWTException $e) {
// 401 response;
return false;
}
// safely use $payload
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment