Completed
Push — master ( 3e885b...da45e8 )
by Tom
02:56
created

PageData::isStringable()   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
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Astrotomic\Stancy\Models;
4
5
use DateTime;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Spatie\DataTransferObject\DataTransferObject;
8
9
abstract class PageData extends DataTransferObject implements Arrayable
10
{
11 9
    public static function make(array $data): self
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\PageData::make() does not have @param annotation for its traversable parameter $data.
Loading history...
12
    {
13 9
        return new static($data);
14
    }
15
16
    // https://github.com/spatie/data-transfer-object/issues/64
17 5
    protected function parseArray(array $array): array
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\PageData::parseArray() does not have @param annotation for its traversable parameter $array.
Loading history...
introduced by
Method \Astrotomic\Stancy\Models\PageData::parseArray() does not have @return annotation for its traversable return value.
Loading history...
18
    {
19 5
        foreach ($array as $key => $value) {
20 5
            if ($this->isDateTime($value)) {
21 1
                $array[$key] = $value->format(DATE_RFC3339);
22
23 1
                continue;
24
            }
25
26 5
            if ($this->isArrayable($value)) {
27 1
                $array[$key] = $value->toArray();
28
29 1
                continue;
30
            }
31
32 5
            if ($this->isStringable($value)) {
33 5
                $array[$key] = $value->__toString();
34
35 5
                continue;
36
            }
37
38 5
            if (is_array($value)) {
39 5
                $array[$key] = $this->parseArray($value);
40
            }
41
        }
42
43 5
        return $array;
44
    }
45
46 5
    protected function isDateTime($value): bool
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\PageData::isDateTime() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
47
    {
48 5
        return $value instanceof DateTime;
49
    }
50
51 5
    protected function isArrayable($value): bool
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\PageData::isArrayable() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
52
    {
53 5
        return $this->isObjectWithMethod($value, 'toArray');
54
    }
55
56 5
    protected function isStringable($value): bool
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\PageData::isStringable() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
57
    {
58 5
        return $this->isObjectWithMethod($value, '__toString');
59
    }
60
61 5
    protected function isObjectWithMethod($value, string $method): bool
0 ignored issues
show
introduced by
Method \Astrotomic\Stancy\Models\PageData::isObjectWithMethod() does not have parameter type hint nor @param annotation for its parameter $value.
Loading history...
62
    {
63 5
        return is_object($value) && method_exists($value, $method) && is_callable([$value, $method]);
64
    }
65
}
66