Passed
Pull Request — master (#170)
by
unknown
08:08
created

searchableAttribute::getValue()   A

Complexity

Conditions 5
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 5
nc 2
nop 3
dl 0
loc 9
ccs 6
cts 6
cp 1
crap 5
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of Scout Extended.
7
 *
8
 * (c) Algolia Team <[email protected]>
9
 *
10
 *  For the full copyright and license information, please view the LICENSE
11
 *  file that was distributed with this source code.
12
 */
13
14
namespace Algolia\ScoutExtended\Settings\SettingAttribute;
15
16
use Illuminate\Support\Str;
17
use Algolia\ScoutExtended\Contracts\SettingContract;
18
19
/*
20
 * @internal
21
 */
22
final class searchableAttribute implements SettingContract
23
{
24
    /**
25
     * @var string[]
26
     */
27
    private static $unsearchableAttributesKeys = [
28
        'id',
29
        '*_id',
30
        'id_*',
31
        '*ed_at',
32
        '*_count',
33
        'count_*',
34
        'number_*',
35
        '*_number',
36
        '*image*',
37
        '*url*',
38
        '*link*',
39
        '*password*',
40
        '*token*',
41
        '*hash*',
42
    ];
43
44
    /**
45
     * @var string[]
46
     */
47
    private static $unsearchableAttributesValues = [
48
        'http://*',
49
        'https://*',
50
    ];
51
52
    /**
53
     * Checks if the given key/value is a 'searchableAttributes'.
54
     *
55
     * @param  string $key
56
     * @param  array|null|string $value
57
     * @param  array $searchableAttributes
58
     *
59
     * @return array
60
     */
61 4
    public function getValue(string $key, $value, array $searchableAttributes): array
62
    {
63 4
        if (! is_object($value) && ! is_array($value) &&
64 4
            ! Str::is(self::$unsearchableAttributesKeys, $key) &&
65 4
            ! Str::is(self::$unsearchableAttributesValues, $value)) {
66 4
            $searchableAttributes[] = $key;
67
        }
68
69 4
        return $searchableAttributes;
70
    }
71
}
72