• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Astrotomic / stancy / 58 / 1

Source File

100.0
/src/Models/PageData.php
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
    public static function make(array $data): self
15
    {
16
        return new static($data);
15✔
17
    }
18

19
    public function toFeedItem(): FeedItem
20
    {
21
        throw new Exception(sprintf('You have to define the transformation to a valid %s yourself if you want to use a feed.', FeedItem::class));
1✔
22
    }
23

24
    // https://github.com/spatie/data-transfer-object/issues/64
25
    protected function parseArray(array $array): array
26
    {
27
        foreach ($array as $key => $value) {
7✔
28
            if ($this->isDateTime($value)) {
7✔
29
                $array[$key] = $value->format(DATE_RFC3339);
1✔
30

31
                continue;
1✔
32
            }
33

34
            if ($this->isArrayable($value)) {
7✔
35
                $array[$key] = $value->toArray();
2✔
36

37
                continue;
2✔
38
            }
39

40
            if ($this->isStringable($value)) {
7✔
41
                $array[$key] = $value->__toString();
7✔
42

43
                continue;
7✔
44
            }
45

46
            if (is_array($value)) {
7✔
47
                $array[$key] = $this->parseArray($value);
2✔
48
            }
49
        }
50

51
        return $array;
7✔
52
    }
53

54
    protected function isDateTime($value): bool
55
    {
56
        return $value instanceof DateTime;
7✔
57
    }
58

59
    protected function isArrayable($value): bool
60
    {
61
        return $this->isObjectWithMethod($value, 'toArray');
7✔
62
    }
63

64
    protected function isStringable($value): bool
65
    {
66
        return $this->isObjectWithMethod($value, '__toString');
7✔
67
    }
68

69
    protected function isObjectWithMethod($value, string $method): bool
70
    {
71
        return is_object($value) && method_exists($value, $method) && is_callable([$value, $method]);
7✔
72
    }
73
}
  • Back to Build 53
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2024 Coveralls, Inc