Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Doctrine\DBAL\Driver\API\SQLite;
4
 
5
use Doctrine\DBAL\Platforms\AbstractPlatform;
6
use Doctrine\DBAL\Platforms\SqlitePlatform;
7
use Doctrine\Deprecations\Deprecation;
8
 
9
use function array_merge;
10
use function strpos;
11
 
12
/**
13
 * User-defined SQLite functions.
14
 *
15
 * @internal
16
 */
17
final class UserDefinedFunctions
18
{
19
    private const DEFAULT_FUNCTIONS = [
20
        'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
21
        'mod'  => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
22
        'locate'  => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
23
    ];
24
 
25
    /**
26
     * @param callable(string, callable, int): bool                  $callback
27
     * @param array<string, array{callback: callable, numArgs: int}> $additionalFunctions
28
     */
29
    public static function register(callable $callback, array $additionalFunctions = []): void
30
    {
31
        $userDefinedFunctions = array_merge(self::DEFAULT_FUNCTIONS, $additionalFunctions);
32
 
33
        foreach ($userDefinedFunctions as $function => $data) {
34
            $callback($function, $data['callback'], $data['numArgs']);
35
        }
36
    }
37
 
38
    /**
39
     * User-defined function that implements MOD().
40
     *
41
     * @param int $a
42
     * @param int $b
43
     */
44
    public static function mod($a, $b): int
45
    {
46
        return $a % $b;
47
    }
48
 
49
    /**
50
     * User-defined function that implements LOCATE().
51
     *
52
     * @param string $str
53
     * @param string $substr
54
     * @param int    $offset
55
     */
56
    public static function locate($str, $substr, $offset = 0): int
57
    {
58
        Deprecation::trigger(
59
            'doctrine/dbal',
60
            'https://github.com/doctrine/dbal/pull/5749',
61
            'Relying on DBAL\'s emulated LOCATE() function is deprecated. '
62
                . 'Use INSTR() or %s::getLocateExpression() instead.',
63
            AbstractPlatform::class,
64
        );
65
 
66
        // SQL's LOCATE function works on 1-based positions, while PHP's strpos works on 0-based positions.
67
        // So we have to make them compatible if an offset is given.
68
        if ($offset > 0) {
69
            $offset -= 1;
70
        }
71
 
72
        $pos = strpos($str, $substr, $offset);
73
 
74
        if ($pos !== false) {
75
            return $pos + 1;
76
        }
77
 
78
        return 0;
79
    }
80
}