• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemsdk / docker-symfony-api / #74

pending completion
#74

push

DKravtsov
Php 8.2, symfony 6.2, updated RabbitMQ, updated composer dependencies, refactoring.

51 of 51 new or added lines in 44 files covered. (100.0%)

1479 of 2668 relevant lines covered (55.43%)

23.59 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/General/Infrastructure/Rest/SearchTerm.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace App\General\Infrastructure\Rest;
6

7
use App\General\Infrastructure\Rest\Interfaces\SearchTermInterface;
8
use Closure;
9

10
use function array_filter;
11
use function array_map;
12
use function array_merge;
13
use function array_unique;
14
use function array_values;
15
use function explode;
16
use function is_array;
17
use function is_string;
18
use function preg_match_all;
19
use function preg_replace;
20
use function str_contains;
21
use function str_replace;
22
use function trim;
23

24
/**
25
 * Class SearchTerm
26
 *
27
 * @package App\General
28
 */
29
final class SearchTerm implements SearchTermInterface
30
{
31
    /**
32
     * {@inheritdoc}
33
     */
34
    public static function getCriteria(
35
        array | string | null $column,
36
        array | string | null $search,
37
        ?string $operand = null,
38
        ?int $mode = null,
39
    ): ?array {
40
        $operand ??= self::OPERAND_OR;
×
41
        $mode ??= self::MODE_FULL;
×
42
        $columns = self::getColumns($column);
×
43
        $searchTerms = self::getSearchTerms($search);
×
44

45
        // Fallback to OR operand if not supported one given
46
        if ($operand !== self::OPERAND_AND && $operand !== self::OPERAND_OR) {
×
47
            $operand = self::OPERAND_OR;
×
48
        }
49

50
        return self::createCriteria($columns, $searchTerms, $operand, $mode);
×
51
    }
52

53
    /**
54
     * Helper method to create used criteria array with given columns and search terms.
55
     *
56
     * @param array<int, string> $columns
57
     * @param array<int, string> $searchTerms
58
     *
59
     * @return array<string, array<string, array<string, mixed>>>|null
60
     */
61
    private static function createCriteria(array $columns, array $searchTerms, string $operand, int $mode): ?array
62
    {
63
        $iteratorTerm = self::getTermIterator($columns, $mode);
×
64
        /**
65
         * Get criteria
66
         *
67
         * @var array<string, array<string, array<int, string>>> $criteria
68
         */
69
        $criteria = array_filter(array_map($iteratorTerm, $searchTerms));
×
70
        // Initialize output
71
        $output = null;
×
72

73
        // We have some generated criteria
74
        if ($criteria !== []) {
×
75
            // Create used criteria array
76
            $output = [
×
77
                'and' => [
×
78
                    $operand => array_merge(...array_values($criteria)),
×
79
                ],
×
80
            ];
×
81
        }
82

83
        return $output;
×
84
    }
85

86
    /**
87
     * Method to get term iterator closure.
88
     *
89
     * @param array<int, string> $columns
90
     */
91
    private static function getTermIterator(array $columns, int $mode): Closure
92
    {
93
        return static fn (string $term): ?array => $columns === []
×
94
            ? null
×
95
            : array_map(self::getColumnIterator($term, $mode), $columns);
×
96
    }
97

98
    /**
99
     * Method to get column iterator closure.
100
     */
101
    private static function getColumnIterator(string $term, int $mode): Closure
102
    {
103
        /*
104
         * Lambda function to create actual criteria for specified column + term + mode combo.
105
         *
106
         * @param string $column
107
         *
108
         * @return array<int, string>
109
         */
110
        return static fn (string $column): array => [
×
111
            str_contains($column, '.') ? $column : 'entity.' . $column, 'like', self::getTerm($mode, $term),
×
112
        ];
×
113
    }
114

115
    /**
116
     * Method to get search term clause for 'LIKE' query for specified mode.
117
     */
118
    private static function getTerm(int $mode, string $term): string
119
    {
120
        return match ($mode) {
×
121
            self::MODE_STARTS_WITH => $term . '%',
×
122
            self::MODE_ENDS_WITH => '%' . $term,
×
123
            default => '%' . $term . '%', // self::MODE_FULL
×
124
        };
×
125
    }
126

127
    /**
128
     * @param string|array<int, string|null>|null $column search column(s), could be a string or an array of strings
129
     *
130
     * @return array<int, string>
131
     */
132
    private static function getColumns(array | string | null $column): array
133
    {
134
        // Normalize columns
135
        return $column === null
×
136
            ? []
×
137
            : array_filter(
×
138
                array_map(
×
139
                    'trim',
×
140
                    (is_array($column)
×
141
                        ? array_filter($column, static fn (string | null $column): bool => is_string($column))
×
142
                        : (array)$column),
×
143
                ),
×
144
                static fn (string $value): bool => trim($value) !== '',
×
145
            );
×
146
    }
147

148
    /**
149
     * Method to get search terms.
150
     *
151
     * @param string|array<int, string|null>|null $search search term(s), could be a string or an array of strings
152
     *
153
     * @return array<int, string>
154
     */
155
    private static function getSearchTerms(array | string | null $search): array
156
    {
157
        if (is_string($search)) {
×
158
            preg_match_all('#([^\"]\S*|\".+?\")\s*#', trim($search), $matches);
×
159

160
            if ($matches[1]) {
×
161
                $search = array_map(
×
162
                    static fn (string $term): string => trim(str_replace('"', '', $term)),
×
163
                    $matches[1],
×
164
                );
×
165
            }
166
        }
167

168
        return $search === null
×
169
            ? []
×
170
            : array_unique(
×
171
                array_filter(
×
172
                    array_map(
×
173
                        static fn (string $term): string => (string)preg_replace('#\s+#', ' ', $term),
×
174
                        array_map(
×
175
                            'trim',
×
176
                            (is_array($search)
×
177
                                ? array_filter($search, static fn (string | null $term): bool => is_string($term))
×
178
                                : explode(' ', $search)),
×
179
                        ),
×
180
                    ),
×
181
                    static fn (string $value): bool => trim($value) !== ''
×
182
                )
×
183
            );
×
184
    }
185
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc