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

Carbon::serializeUsing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
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