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

ConvertNumericStringsToNumbers::__invoke()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
eloc 4
nc 4
nop 2
dl 0
loc 12
ccs 5
cts 5
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\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