Completed
Push — master ( a51ab2...247c5c )
by Antonio Carlos
03:22
created

Carbon::jsonSerialize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 0
dl 0
loc 12
ccs 7
cts 7
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace IlluminateAgnostic\Str\Support;
4
5
use JsonSerializable;
6
use Carbon\Carbon as BaseCarbon;
7
use IlluminateAgnostic\Str\Support\Traits\Macroable;
8
9
class Carbon extends BaseCarbon implements JsonSerializable
10
{
11
    use Macroable;
12
13
    /**
14
     * The custom Carbon JSON serializer.
15
     *
16
     * @var callable|null
17
     */
18
    protected static $serializer;
19
20
    /**
21
     * Prepare the object for JSON serialization.
22
     *
23
     * @return array|string
24
     */
25 2
    public function jsonSerialize()
26
    {
27 2
        if (static::$serializer) {
28 1
            return call_user_func(static::$serializer, $this);
29
        }
30
31 1
        $carbon = $this;
32
33 1
        return call_user_func(function () use ($carbon) {
34 1
            return get_object_vars($carbon);
35 1
        });
36
    }
37
38
    /**
39
     * JSON serialize all Carbon instances using the given callback.
40
     *
41
     * @param  callable  $callback
42
     * @return void
43
     */
44 9
    public static function serializeUsing($callback)
45
    {
46 9
        static::$serializer = $callback;
47 9
    }
48
}
49