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

ConvertDatesToTimestamps::__invoke()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 3
rs 10
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
final class ConvertDatesToTimestamps
17
{
18
    /**
19
     * Converts the given array numeric strings to numbers.
20
     *
21
     * @param object $searchable
22
     * @param array $array
23
     *
24
     * @return array
25
     */
26 9
    public function __invoke($searchable, array $array): array
27
    {
28 9
        foreach ($array as $key => $value) {
29 9
            $attributeValue = $searchable->getModel()->getAttribute($key);
30
31
            /*
32
             * Casts carbon instances to timestamp.
33
             */
34 9
            if ($attributeValue instanceof \Illuminate\Support\Carbon) {
35 9
                $array[$key] = $attributeValue->getTimestamp();
36
            }
37
        }
38
39 9
        return $array;
40
    }
41
}
42