Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
Operator
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
11
100.00% covered (success)
100.00%
1 / 1
 parse
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
1 / 1
11
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Db\Sql\Parser;
16
17/**
18 * Operator parser class
19 *
20 * @category   Pop
21 * @package    Pop\Db
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    7.0.0
26 */
27class Operator
28{
29
30    /**
31     * Method to get the operator from the shorthand column name
32     *
33     * @param  string $column
34     * @return array
35     */
36    public static function parse(string $column): array
37    {
38        $operator = '=';
39
40        // LIKE/NOT LIKE shorthand
41        if (str_starts_with($column, '-%')) {
42            $column   = substr($column, 2);
43            $operator = 'NOT LIKE';
44        } else if (str_starts_with($column, '%')) {
45            $column   = substr($column, 1);
46            $operator = 'LIKE';
47        }
48        if (str_ends_with($column, '%-')) {
49            $column   = substr($column, 0, -2);
50            $operator = 'NOT LIKE';
51        } else if (str_ends_with($column, '%')) {
52            $column   = substr($column, 0, -1);
53            $operator = 'LIKE';
54        }
55
56        // NOT NULL/IN/BETWEEN shorthand
57        if (str_ends_with($column, '-')) {
58            $column   = trim(substr($column, 0, -1));
59            $operator = 'NOT';
60        }
61
62        // Basic comparison shorthand
63        if (str_ends_with($column, '>=')) {
64            $column   = trim(substr($column, 0, -2));
65            $operator = '>=';
66        } else if (str_ends_with($column, '<=')) {
67            $column   = trim(substr($column, 0, -2));
68            $operator = '<=';
69        } else if (str_ends_with($column, '!=')) {
70            $column   = trim(substr($column, 0, -2));
71            $operator = '!=';
72        } else if (str_ends_with($column, '>')) {
73            $column   = trim(substr($column, 0, -1));
74            $operator = '>';
75        } else if (str_ends_with($column, '<')) {
76            $column   = trim(substr($column, 0, -1));
77            $operator = '<';
78        }
79
80        return ['column' => $column, 'operator' => $operator];
81    }
82
83}