1<?php
2
3/*
4 * This file is part of the TYPO3 CMS project.
5 *
6 * It is free software; you can redistribute it and/or modify it under
7 * the terms of the GNU General Public License, either version 2
8 * of the License, or any later version.
9 *
10 * For the full copyright and license information, please read the
11 * LICENSE.txt file that was distributed with this source code.
12 *
13 * The TYPO3 project - inspiring people to share!
14 */
15
16namespace TYPO3\CMS\IndexedSearch\Utility;
17
18use TYPO3\CMS\Core\Database\ConnectionPool;
19use TYPO3\CMS\Core\Type\Enumeration;
20use TYPO3\CMS\Core\Utility\GeneralUtility;
21
22/**
23 * Enumeration object for LikeWildcard
24 * @internal
25 */
26final class LikeWildcard extends Enumeration
27{
28    const __default = self::BOTH;
29
30    /** @var int Do not use any wildcard */
31    const NONE = 0;
32
33    /** @var int Use wildcard on left side */
34    const LEFT = 1;
35
36    /** @var int Use wildcard on right side */
37    const RIGHT = 2;
38
39    /** @var int Use wildcard on both sides */
40    const BOTH = 3;
41
42    /**
43     * Returns a LIKE clause for sql queries.
44     *
45     * @param string $tableName The name of the table to query.
46     * @param string $fieldName The name of the field to query with LIKE.
47     * @param string $likeValue The value for the LIKE clause operation.
48     * @return string
49     */
50    public function getLikeQueryPart($tableName, $fieldName, $likeValue)
51    {
52        $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
53            ->getQueryBuilderForTable($tableName);
54
55        $string = ($this->value & self::LEFT ? '%' : '')
56            . $queryBuilder->escapeLikeWildcards($likeValue)
57            . ($this->value & self::RIGHT ? '%' : '');
58
59        return $queryBuilder->expr()->like($fieldName, $queryBuilder->quote($string));
60    }
61}
62