aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Helper/ContentProxy.php
diff options
context:
space:
mode:
Diffstat (limited to 'src/Wallabag/CoreBundle/Helper/ContentProxy.php')
-rw-r--r--src/Wallabag/CoreBundle/Helper/ContentProxy.php140
1 files changed, 70 insertions, 70 deletions
diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
index 5622cc83..656ac6ee 100644
--- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php
+++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php
@@ -67,6 +67,76 @@ class ContentProxy
67 } 67 }
68 68
69 /** 69 /**
70 * Use a Symfony validator to ensure the language is well formatted.
71 *
72 * @param Entry $entry
73 * @param string $value Language to validate and save
74 */
75 public function updateLanguage(Entry $entry, $value)
76 {
77 // some lang are defined as fr-FR, es-ES.
78 // replacing - by _ might increase language support
79 $value = str_replace('-', '_', $value);
80
81 $errors = $this->validator->validate(
82 $value,
83 (new LocaleConstraint())
84 );
85
86 if (0 === count($errors)) {
87 $entry->setLanguage($value);
88
89 return;
90 }
91
92 $this->logger->warning('Language validation failed. ' . (string) $errors);
93 }
94
95 /**
96 * Use a Symfony validator to ensure the preview picture is a real url.
97 *
98 * @param Entry $entry
99 * @param string $value URL to validate and save
100 */
101 public function updatePreviewPicture(Entry $entry, $value)
102 {
103 $errors = $this->validator->validate(
104 $value,
105 (new UrlConstraint())
106 );
107
108 if (0 === count($errors)) {
109 $entry->setPreviewPicture($value);
110
111 return;
112 }
113
114 $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors);
115 }
116
117 /**
118 * Update date.
119 *
120 * @param Entry $entry
121 * @param string $value Date to validate and save
122 */
123 public function updatePublishedAt(Entry $entry, $value)
124 {
125 $date = $value;
126
127 // is it a timestamp?
128 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
129 $date = '@' . $value;
130 }
131
132 try {
133 $entry->setPublishedAt(new \DateTime($date));
134 } catch (\Exception $e) {
135 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]);
136 }
137 }
138
139 /**
70 * Stock entry with fetched or imported content. 140 * Stock entry with fetched or imported content.
71 * Will fall back to OpenGraph data if available. 141 * Will fall back to OpenGraph data if available.
72 * 142 *
@@ -155,74 +225,4 @@ class ContentProxy
155 { 225 {
156 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']); 226 return !empty($content['title']) && !empty($content['html']) && !empty($content['url']);
157 } 227 }
158
159 /**
160 * Use a Symfony validator to ensure the language is well formatted.
161 *
162 * @param Entry $entry
163 * @param string $value Language to validate and save
164 */
165 public function updateLanguage(Entry $entry, $value)
166 {
167 // some lang are defined as fr-FR, es-ES.
168 // replacing - by _ might increase language support
169 $value = str_replace('-', '_', $value);
170
171 $errors = $this->validator->validate(
172 $value,
173 (new LocaleConstraint())
174 );
175
176 if (0 === count($errors)) {
177 $entry->setLanguage($value);
178
179 return;
180 }
181
182 $this->logger->warning('Language validation failed. ' . (string) $errors);
183 }
184
185 /**
186 * Use a Symfony validator to ensure the preview picture is a real url.
187 *
188 * @param Entry $entry
189 * @param string $value URL to validate and save
190 */
191 public function updatePreviewPicture(Entry $entry, $value)
192 {
193 $errors = $this->validator->validate(
194 $value,
195 (new UrlConstraint())
196 );
197
198 if (0 === count($errors)) {
199 $entry->setPreviewPicture($value);
200
201 return;
202 }
203
204 $this->logger->warning('PreviewPicture validation failed. ' . (string) $errors);
205 }
206
207 /**
208 * Update date.
209 *
210 * @param Entry $entry
211 * @param string $value Date to validate and save
212 */
213 public function updatePublishedAt(Entry $entry, $value)
214 {
215 $date = $value;
216
217 // is it a timestamp?
218 if (filter_var($date, FILTER_VALIDATE_INT) !== false) {
219 $date = '@'.$value;
220 }
221
222 try {
223 $entry->setPublishedAt(new \DateTime($date));
224 } catch (\Exception $e) {
225 $this->logger->warning('Error while defining date', ['e' => $e, 'url' => $entry->getUrl(), 'date' => $value]);
226 }
227 }
228} 228}