Completed
Pull Request — master (#83)
by Nuno
42:15 queued 37:30
created

ConvertNumericStringsToNumbers   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

1 Method

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