ConvertNumericStringsToNumbers   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 22
ccs 5
cts 5
cp 1
rs 10
wmc 5

1 Method

Rating   Name   Duplication   Size   Complexity  
A transform() 0 12 5
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\Transformers;
15
16
use Algolia\ScoutExtended\Contracts\TransformerContract;
17
use function is_string;
18
19
final class ConvertNumericStringsToNumbers implements TransformerContract
20
{
21
    /**
22
     * Converts the given array numeric strings to numbers.
23
     *
24
     * @param object $searchable
25
     * @param array $array
26
     *
27
     * @return array
28
     */
29 15
    public function transform($searchable, array $array): array
30
    {
31 15
        foreach ($array as $key => $value) {
32
            /*
33
             * Casts numeric strings to integers/floats.
34
             */
35 15
            if (is_string($value) && is_numeric($value)) {
36 15
                $array[$key] = ctype_digit($value) ? (int) $value : (float) $value;
37
            }
38
        }
39
40 15
        return $array;
41
    }
42
}
43