Passed
Push — master ( da45e8...9bb787 )
by Tom
03:18
created

PageData::toFeedItem()   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 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Astrotomic\Stancy\Models;
4
5
use DateTime;
6
use Exception;
7
use Illuminate\Contracts\Support\Arrayable;
8
use Spatie\DataTransferObject\DataTransferObject;
9
use Spatie\Feed\Feedable;
10
use Spatie\Feed\FeedItem;
11
12
abstract class PageData extends DataTransferObject implements Arrayable, Feedable
13
{
14 12
    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...
15
    {
16 12
        return new static($data);
17
    }
18
19 1
    public function toFeedItem(): FeedItem
20
    {
21 1
        throw new Exception(sprintf('You have to define the transformation to a valid %s yourself if you want to use a feed.', FeedItem::class));
22
    }
23
24
    // https://github.com/spatie/data-transfer-object/issues/64
25 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...
26
    {
27 5
        foreach ($array as $key => $value) {
28 5
            if ($this->isDateTime($value)) {
29 1
                $array[$key] = $value->format(DATE_RFC3339);
30
31 1
                continue;
32
            }
33
34 5
            if ($this->isArrayable($value)) {
35 1
                $array[$key] = $value->toArray();
36
37 1
                continue;
38
            }
39
40 5
            if ($this->isStringable($value)) {
41 5
                $array[$key] = $value->__toString();
42
43 5
                continue;
44
            }
45
46 5
            if (is_array($value)) {
47 5
                $array[$key] = $this->parseArray($value);
48
            }
49
        }
50
51 5
        return $array;
52
    }
53
54 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...
55
    {
56 5
        return $value instanceof DateTime;
57
    }
58
59 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...
60
    {
61 5
        return $this->isObjectWithMethod($value, 'toArray');
62
    }
63
64 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...
65
    {
66 5
        return $this->isObjectWithMethod($value, '__toString');
67
    }
68
69 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...
70
    {
71 5
        return is_object($value) && method_exists($value, $method) && is_callable([$value, $method]);
72
    }
73
}
74