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

heimrichhannot / contao-utils-bundle / 8111841604

01 Mar 2024 01:05PM UTC coverage: 72.668% (+0.09%) from 72.575%
8111841604

push

github

koertho
fix test

779 of 1072 relevant lines covered (72.67%)

3.5 hits per line

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

25.93
/src/EntityFinder/EntityFinderHelper.php
1
<?php
2

3
/*
4
 * Copyright (c) 2022 Heimrich & Hannot GmbH
5
 *
6
 * @license LGPL-3.0-or-later
7
 */
8

9
namespace HeimrichHannot\UtilsBundle\EntityFinder;
10

11
use Contao\CoreBundle\Framework\ContaoFramework;
12
use Contao\Database;
13
use Contao\Model\Collection;
14
use Contao\ModuleModel;
15
use Contao\Validator;
16
use HeimrichHannot\UtilsBundle\Util\Utils;
17

18
class EntityFinderHelper
19
{
20
    public function __construct(
21
        private Utils $utils,
22
        private ContaoFramework $framework,
23
    )
24
    {
25
    }
1✔
26

27
    /**
28
     * Search within serialized array fields of the model entity.
29
     *
30
     * @param string $type   Module type
31
     * @param string $field  Field with serialized data
32
     * @param array  $values Values to search for in serialized data field
33
     *
34
     * @throws \Exception
35
     */
36
    public function findModulesByTypeAndSerializedValue(string $type, string $field, array $values): ?Collection
37
    {
38
        $blobQuery = $this->utils->database()->createWhereForSerializedBlob(ModuleModel::getTable().'.'.$field, $values);
1✔
39
        $columns = [$blobQuery->createOrWhere()];
1✔
40
        $values = $blobQuery->values;
1✔
41

42
        $columns[] = ModuleModel::getTable().'.type=?';
1✔
43
        $values[] = $type;
1✔
44

45
        return $this->framework->getAdapter(ModuleModel::class)->findBy($columns, $values);
1✔
46
    }
47

48
        /**
49
     * Find frontend modules by insert inserttags like insert_module oder insert_article.
50
     *
51
     * @param string $type The module type
52
     * @param string $field The tl_module field
53
     * @param string $inserttag The inserttag to search for, for example insert_module
54
     * @param int $id The element id to search for, for example the module id (as used in {{insert_module::1}}, would be 1 in this case)
55
     * @return array The found module ids
56
     * @throws \Exception
57
     */
58
    public function findModulesByInserttag(string $type, string $field, string $inserttag, int $id): array
59
    {
60
        if (!Validator::isAlias($field)) {
×
61
            throw new \Exception('Invalid field name '.$field.'given.');
×
62
        }
63
        if (!Validator::isAlias($inserttag)) {
×
64
            throw new \Exception('Invalid inserttag '.$inserttag.'given.');
×
65
        }
66
        $result = Database::getInstance()
×
67
            ->prepare("SELECT id FROM tl_module
×
68
                        WHERE type=?
69
                        AND (
70
                            $field LIKE '%{{".$inserttag."::".$id."}}%'
×
71
                            OR $field LIKE '%{{".$inserttag."::".$id."::%')")
×
72
            ->execute($type);
×
73

74
        return $result->fetchEach('id');
×
75
    }
76

77

78
    /**
79
     * Find content elements by insert inserttags like insert_module oder insert_article.
80
     *
81
     * @param string $type The element type
82
     * @param string $field The tl_content field
83
     * @param string $inserttag The inserttag to search for, for example insert_module
84
     * @param int $id The element id to search for, for example the module id (as used in {{insert_module::1}}, would be 1 in this case)
85
     * @return array The found content element ids
86
     * @throws \Exception
87
     */
88
    public function findContentElementByInserttag(string $type, string $field, string $inserttag, int $id): array
89
    {
90
        if (!Validator::isAlias($field)) {
×
91
            throw new \Exception('Invalid field name '.$field.'given.');
×
92
        }
93
        if (!Validator::isAlias($inserttag)) {
×
94
            throw new \Exception('Invalid inserttag '.$inserttag.'given.');
×
95
        }
96
        $result = Database::getInstance()
×
97
            ->prepare("SELECT id FROM tl_content
×
98
                        WHERE type=?
99
                        AND (
100
                            $field LIKE '%{{".$inserttag."::".$id."}}%'
×
101
                            OR $field LIKE '%{{".$inserttag."::".$id."::%')")
×
102
            ->execute($type);
×
103

104
        return $result->fetchEach('id');
×
105
    }
106
}
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