Added functional-php library

This commit is contained in:
Alejandro Celaya 2018-11-02 10:54:42 +01:00
parent 6986d03c53
commit 521f6f2b18
8 changed files with 17 additions and 21 deletions

View file

@ -25,6 +25,7 @@
"endroid/qr-code": "^1.7",
"firebase/php-jwt": "^4.0",
"guzzlehttp/guzzle": "^6.2",
"lstrojny/functional-php": "^1.8",
"mikehaertl/phpwkhtmltopdf": "^2.2",
"monolog/monolog": "^1.21",
"roave/security-advisories": "dev-master",

View file

@ -6,8 +6,8 @@ namespace Shlinkio\Shlink\Common;
use const ARRAY_FILTER_USE_KEY;
use const JSON_ERROR_NONE;
use function array_filter;
use function Functional\contains;
use function getenv;
use function in_array;
use function json_decode as spl_json_decode;
use function json_last_error;
use function json_last_error_msg;
@ -49,11 +49,6 @@ function env($key, $default = null)
return trim($value);
}
function contains($needle, array $haystack): bool
{
return in_array($needle, $haystack, true);
}
/**
* Returns only the keys in keysToPick from provided array
*
@ -63,7 +58,7 @@ function contains($needle, array $haystack): bool
function pick(array $array, array $keysToPick): array
{
return array_filter($array, function (string $key) use ($keysToPick) {
return contains($key, $keysToPick);
return contains($keysToPick, $key);
}, ARRAY_FILTER_USE_KEY);
}

View file

@ -11,7 +11,7 @@ use Shlinkio\Shlink\Core\Options\AppOptions;
use Zend\ServiceManager\Exception\ServiceNotCreatedException;
use Zend\ServiceManager\Exception\ServiceNotFoundException;
use Zend\ServiceManager\Factory\FactoryInterface;
use function Shlinkio\Shlink\Common\contains;
use function Functional\contains;
use function Shlinkio\Shlink\Common\env;
class CacheFactory implements FactoryInterface
@ -53,7 +53,7 @@ class CacheFactory implements FactoryInterface
{
// Try to get the adapter from config
$config = $container->get('config');
if (isset($config['cache']['adapter']) && contains($config['cache']['adapter'], self::VALID_CACHE_ADAPTERS)) {
if (isset($config['cache']['adapter']) && contains(self::VALID_CACHE_ADAPTERS, $config['cache']['adapter'])) {
return $this->resolveCacheAdapter($config['cache']);
}

View file

@ -9,9 +9,9 @@ use Doctrine\ORM\QueryBuilder;
use Shlinkio\Shlink\Core\Entity\ShortUrl;
use function array_column;
use function array_key_exists;
use function Functional\contains;
use function is_array;
use function key;
use function Shlinkio\Shlink\Common\contains;
class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryInterface
{
@ -63,7 +63,7 @@ class ShortUrlRepository extends EntityRepository implements ShortUrlRepositoryI
$fieldName = is_array($orderBy) ? key($orderBy) : $orderBy;
$order = is_array($orderBy) ? $orderBy[$fieldName] : 'ASC';
if (contains($fieldName, ['visits', 'visitsCount', 'visitCount'])) {
if (contains(['visits', 'visitsCount', 'visitCount'], $fieldName)) {
$qb->addSelect('COUNT(DISTINCT v) AS totalVisits')
->leftJoin('s.visits', 'v')
->groupBy('s')

View file

@ -11,7 +11,7 @@ use Zend\Diactoros\Response;
use Zend\Expressive\Template\TemplateRendererInterface;
use function array_shift;
use function explode;
use function Shlinkio\Shlink\Common\contains;
use function Functional\contains;
class NotFoundHandler implements RequestHandlerInterface
{
@ -47,7 +47,7 @@ class NotFoundHandler implements RequestHandlerInterface
$status = StatusCodeInterface::STATUS_NOT_FOUND;
// If the first accepted type is json, return a json response
if (contains($accept, ['application/json', 'text/json', 'application/x-json'])) {
if (contains(['application/json', 'text/json', 'application/x-json'], $accept)) {
return new Response\JsonResponse([
'error' => 'NOT_FOUND',
'message' => 'Not found',

View file

@ -10,7 +10,7 @@ use Symfony\Component\Filesystem\Exception\IOException;
use Symfony\Component\Filesystem\Filesystem;
use function array_diff;
use function array_keys;
use function Shlinkio\Shlink\Common\contains;
use function Functional\contains;
class DatabaseConfigCustomizer implements ConfigCustomizerInterface
{
@ -68,7 +68,7 @@ class DatabaseConfigCustomizer implements ConfigCustomizerInterface
}
// If the driver is one of the params to ask for, ask for it first
if (contains(self::DRIVER, $keysToAskFor)) {
if (contains($keysToAskFor, self::DRIVER)) {
$io->title('DATABASE');
$titlePrinted = true;
$db[self::DRIVER] = $this->ask($io, self::DRIVER);

View file

@ -20,8 +20,8 @@ use Shlinkio\Shlink\Rest\Util\RestUtils;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Router\RouteResult;
use Zend\I18n\Translator\TranslatorInterface;
use function Functional\contains;
use function implode;
use function Shlinkio\Shlink\Common\contains;
use function sprintf;
class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface, RequestMethodInterface
@ -72,7 +72,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
if ($routeResult === null
|| $routeResult->isFailure()
|| $request->getMethod() === self::METHOD_OPTIONS
|| contains($routeResult->getMatchedRouteName(), $this->routesWhitelist)
|| contains($this->routesWhitelist, $routeResult->getMatchedRouteName())
) {
return $handler->handle($request);
}

View file

@ -10,8 +10,8 @@ use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use function array_shift;
use function explode;
use function Functional\contains;
use function parse_str;
use function Shlinkio\Shlink\Common\contains;
use function Shlinkio\Shlink\Common\json_decode;
use function trim;
@ -32,17 +32,17 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
$currentParams = $request->getParsedBody();
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
if (! empty($currentParams) || contains($method, [
if (! empty($currentParams) || contains([
self::METHOD_GET,
self::METHOD_HEAD,
self::METHOD_OPTIONS,
])) {
], $method)) {
return $handler->handle($request);
}
// If the accepted content is JSON, try to parse the body from JSON
$contentType = $this->getRequestContentType($request);
if (contains($contentType, ['application/json', 'text/json', 'application/x-json'])) {
if (contains(['application/json', 'text/json', 'application/x-json'], $contentType)) {
return $handler->handle($this->parseFromJson($request));
}