Passed
Push — master ( f54aa8...6cc176 )
by Charis
02:58 queued 51s
created

Model::formatDate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace One\Model;
4
5
use One\Collection;
6
use Psr\Http\Message\UriInterface;
7
8
/**
9
 * Model base class
10
 * @method \One\Collection getCollection()
11
 * @method self withCollection(Collection $collection)
12
 * @method mixed|null get(string $key)
13
 * @method self set(string $key, mixed $value)
14
 * @method self add(string $key, mixed $value)
15
 * @method self map(\Closure $callback, array $context)
16
 * @method self filter filter(\Closure $callback)
17
 */
18
class Model
19
{
20
    const LEAD_SENTENCES_LIMIT = 5;
21
22
    /**
23
     * data variable to that work as One\Collection
24
     *
25
     * @var \One\Collection
26
     */
27
    protected $collection = null;
28
29
    /**
30
     * get Data Collection
31
     *
32
     * @return \One\Collection
33
     */
34
    public function getCollection()
35
    {
36
        return $this->collection;
37
    }
38
39
    /**
40
     * immutable, return CLONE of original object with changed collection
41
     *
42
     * @param \One\Collection $collection
43
     * @return self
44
     */
45
    public function withCollection(Collection $collection)
46
    {
47
        $clone = clone $this;
48
        $clone->collection = $collection;
49
50
        return $clone;
51
    }
52
53
    /**
54
     * Clean non parseable char from string
55
     *
56
     * @param $string
57
     * @return string
58
     */
59
    protected function filterStringInstance($string)
60
    {
61
        if (!empty($string)) {
62
            return htmlentities($string);
63
        }
64
    }
65
66
    /**
67
     * Make Sure Uri is a Psr\Http\Message\UriInterface instance
68
     *
69
     * @param \Psr\Http\Message\UriInterface|string|null $uri
70
     * @return string
71
     */
72
    protected function filterUriInstance($uri)
73
    {
74
        if ($uri instanceof UriInterface) {
75
            return (string) $uri;
76
        }
77
78
        if (is_string($uri)) {
79
            return (string) \One\createUriFromString($uri);
80
        }
81
82
        return (string) \One\createuriFromServer();
83
    }
84
85
    /**
86
     * Make Sure Date in string with correct format state
87
     *
88
     * @param \DateTimeInterface|string|int|null $date
89
     * @return string
90
     */
91
    protected function filterDateInstance($date)
92
    {
93
        if (empty($date)) {
94
            $date = new \DateTime("now", new \DateTimeZone("Asia/Jakarta"));
95
        }
96
97
        if (is_string($date) || is_int($date)) {
98
            $date = new \DateTime($date, new \DateTimeZone("Asia/Jakarta"));
99
        }
100
101
        return $this->formatDate($date);
102
    }
103
104
    /**
105
     * format date into required format
106
     *
107
     * @param \DateTimeInterface $date
108
     * @return string
109
     */
110
    protected function formatDate($date)
111
    {
112
        return $date->format("Y-m-d H:i:s");
113
    }
114
115
    /**
116
     * create lead/synopsis from body content if not available
117
     *
118
     * @param string $body
119
     * @return string
120
     */
121
    protected function createLeadFromBody($body)
122
    {
123
        $body = strip_tags($body);
124
        $sentences = array_filter(
125
            explode(".", $body),
126
            function ($item) {
127
                return trim($item);
128
            }
129
        );
130
131
        return implode(
132
            '. ',
133
            array_slice(
134
                $sentences,
135
                0,
136
                self::LEAD_SENTENCES_LIMIT
137
            )
138
        );
139
    }
140
141
    /**
142
     * proxy method to chain it to Collection class
143
     *
144
     * @param string $name
145
     * @param array $arguments
146
     * @throws \Exception
147
     */
148
    public function __call($name, $arguments)
149
    {
150
        if (method_exists($this->getCollection(), $name)) {
151
            return call_user_func_array(array($this->getCollection(), $name), $arguments);
152
        }
153
154
        throw new \Exception("method $name not exist");
155
    }
156
}
157