ConvertNumericStringsToNumbers::transform()   A
last analyzed

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 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