Passed
Push — master ( d32d3d...0995a0 )
by Andrea Marco
02:44 queued 12s
created

TurnsIntoString::toJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
/**
6
 * Trait to turn a DTO into a string.
7
 *
8
 */
9
trait TurnsIntoString
10
{
11
    /**
12
     * Retrieve the DTO as a JSON string
13
     *
14
     * @param int $options
15
     * @return string|false
16
     */
17 6
    public function toJson($options = 0)
18
    {
19 6
        return json_encode($this->toArray(), $options);
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

19
        return json_encode($this->/** @scrutinizer ignore-call */ toArray(), $options);
Loading history...
20
    }
21
22
    /**
23
     * Retrieve the JSON serialized DTO
24
     *
25
     * @return array
26
     */
27 3
    public function jsonSerialize(): array
28
    {
29 3
        return $this->toArray();
30
    }
31
32
    /**
33
     * Retrieve the string representation of the DTO
34
     *
35
     * @return string
36
     */
37 3
    public function __toString(): string
38
    {
39 3
        return $this->toJson();
40
    }
41
}
42