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