Completed
Push — develop ( 1f78f5...10fbf8 )
by Alejandro
16s queued 11s
created

getOptionalBoolFromInputFilter()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 2
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shlinkio\Shlink\Core;
6
7
use Cake\Chronos\Chronos;
8
use DateTimeInterface;
9
use Fig\Http\Message\StatusCodeInterface;
10
use Laminas\InputFilter\InputFilter;
11
use PUGX\Shortid\Factory as ShortIdFactory;
12
13
use function sprintf;
14
15
const DEFAULT_DELETE_SHORT_URL_THRESHOLD = 15;
16
const DEFAULT_SHORT_CODES_LENGTH = 5;
17
const MIN_SHORT_CODES_LENGTH = 4;
18
const DEFAULT_REDIRECT_STATUS_CODE = StatusCodeInterface::STATUS_FOUND;
19
const DEFAULT_REDIRECT_CACHE_LIFETIME = 30;
20
const LOCAL_LOCK_FACTORY = 'Shlinkio\Shlink\LocalLockFactory';
21
const CUSTOM_SLUGS_REGEXP = '/[^A-Za-z0-9._~]+/';
22
23
function generateRandomShortCode(int $length): string
24
{
25
    static $shortIdFactory;
26
    if ($shortIdFactory === null) {
27
        $shortIdFactory = new ShortIdFactory();
28
    }
29
30
    $alphabet = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
31
    return $shortIdFactory->generate($length, $alphabet)->serialize();
32
}
33
34
function parseDateFromQuery(array $query, string $dateName): ?Chronos
35
{
36
    return ! isset($query[$dateName]) || empty($query[$dateName]) ? null : Chronos::parse($query[$dateName]);
37
}
38
39
/**
40
 * @param string|DateTimeInterface|Chronos|null $date
41
 */
42
function parseDateField($date): ?Chronos
43
{
44
    if ($date === null || $date instanceof Chronos) {
45
        return $date;
46
    }
47
48
    if ($date instanceof DateTimeInterface) {
49
        return Chronos::instance($date);
50
    }
51
52
    return Chronos::parse($date);
53
}
54
55
function determineTableName(string $tableName, array $emConfig = []): string
56
{
57
    $schema = $emConfig['connection']['schema'] ?? null;
58
//    $tablePrefix = $emConfig['connection']['table_prefix'] ?? null; // TODO
59
60
    if ($schema === null) {
61
        return $tableName;
62
    }
63
64
    return sprintf('%s.%s', $schema, $tableName);
65
}
66
67
function getOptionalIntFromInputFilter(InputFilter $inputFilter, string $fieldName): ?int
68
{
69
    $value = $inputFilter->getValue($fieldName);
70
    return $value !== null ? (int) $value : null;
71
}
72
73
function getOptionalBoolFromInputFilter(InputFilter $inputFilter, string $fieldName): ?bool
74
{
75
    $value = $inputFilter->getValue($fieldName);
76
    return $value !== null ? (bool) $value : null;
77
}
78