Passed
Pull Request — master (#51)
by
unknown
01:48
created

Model::convertNonAscii()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 43
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 1
dl 0
loc 43
rs 9.568
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
            $string = $this->convertNonAscii($string);
63
            return htmlentities($string);
64
        }
65
    }
66
67
    /**
68
     * Make Sure Uri is a Psr\Http\Message\UriInterface instance
69
     *
70
     * @param \Psr\Http\Message\UriInterface|string|null $uri
71
     * @return string
72
     */
73
    protected function filterUriInstance($uri)
74
    {
75
        if ($uri instanceof UriInterface) {
76
            return (string) $uri;
77
        }
78
79
        if (is_string($uri)) {
80
            return (string) \One\createUriFromString($uri);
81
        }
82
83
        return (string) \One\createuriFromServer();
84
    }
85
86
    /**
87
     * Make Sure Date in string with correct format state
88
     *
89
     * @param \DateTimeInterface|string|int|null $date
90
     * @return string
91
     */
92
    protected function filterDateInstance($date)
93
    {
94
        if (empty($date)) {
95
            $date = new \DateTime("now", new \DateTimeZone("Asia/Jakarta"));
96
        }
97
98
        if (is_string($date) || is_int($date)) {
99
            $date = new \DateTime($date, new \DateTimeZone("Asia/Jakarta"));
100
        }
101
102
        return $this->formatDate($date);
103
    }
104
105
    /**
106
     * format date into required format
107
     *
108
     * @param \DateTimeInterface $date
109
     * @return string
110
     */
111
    protected function formatDate($date)
112
    {
113
        return $date->format("Y-m-d H:i:s");
114
    }
115
116
    /**
117
     * create lead/synopsis from body content if not available
118
     *
119
     * @param string $body
120
     * @return string
121
     */
122
    protected function createLeadFromBody($body)
123
    {
124
        $body = strip_tags($body);
125
        $sentences = array_filter(
126
            explode(".", $body),
127
            function ($item) {
128
                return trim($item);
129
            }
130
        );
131
132
        return implode(
133
            '. ',
134
            array_slice(
135
                $sentences,
136
                0,
137
                self::LEAD_SENTENCES_LIMIT
138
            )
139
        );
140
    }
141
142
    /**
143
     * proxy method to chain it to Collection class
144
     *
145
     * @param string $name
146
     * @param array $arguments
147
     * @throws \Exception
148
     */
149
    public function __call($name, $arguments)
150
    {
151
        if (method_exists($this->getCollection(), $name)) {
152
            return call_user_func_array(array($this->getCollection(), $name), $arguments);
153
        }
154
155
        throw new \Exception("method $name not exist");
156
    }
157
    
158
    /**
159
     * Clean non ASCII char from string
160
     *
161
     * @param $string
162
     * @return string
163
     */
164
    private function convertNonAscii($string)
165
    {
166
        $search = $replace = array();
167
168
        // Replace Single Curly Quotes
169
        $search[]  = chr(226).chr(128).chr(152);
170
        $replace[] = "'";
171
        $search[]  = chr(226).chr(128).chr(153);
172
        $replace[] = "'";
173
174
        // Replace Smart Double Curly Quotes
175
        $search[]  = chr(226).chr(128).chr(156);
176
        $replace[] = '"';
177
        $search[]  = chr(226).chr(128).chr(157);
178
        $replace[] = '"';
179
180
        // Replace En Dash
181
        $search[]  = chr(226).chr(128).chr(147);
182
        $replace[] = '--';
183
184
        // Replace Em Dash
185
        $search[]  = chr(226).chr(128).chr(148);
186
        $replace[] = '---';
187
188
        // Replace Bullet
189
        $search[]  = chr(226).chr(128).chr(162);
190
        $replace[] = '*';
191
192
        // Replace Middle Dot
193
        $search[]  = chr(194).chr(183);
194
        $replace[] = '*';
195
196
        // Replace Ellipsis with three consecutive dots
197
        $search[]  = chr(226).chr(128).chr(166);
198
        $replace[] = '...';
199
200
        // Apply Replacements
201
        $string = str_replace($search, $replace, $string);
202
203
        // Remove any non-ASCII Characters
204
        $string = preg_replace("/[^\x01-\x7F]/", "", $string);
205
206
        return $string;
207
    }
208
}
209