-
-
Notifications
You must be signed in to change notification settings - Fork 625
Expand file tree
/
Copy pathFormSubmissionsStore.php
More file actions
76 lines (60 loc) · 1.93 KB
/
FormSubmissionsStore.php
File metadata and controls
76 lines (60 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Statamic\Stache\Stores;
use Illuminate\Support\Facades\Log;
use Statamic\Facades\Form;
use Statamic\Facades\FormSubmission;
use Statamic\Facades\Path;
use Statamic\Facades\YAML;
use Statamic\Stache\Indexes\Value;
use Statamic\Support\Str;
use Statamic\Yaml\ParseException;
use Symfony\Component\Finder\SplFileInfo;
class FormSubmissionsStore extends ChildStore
{
protected $valueIndex = Value::class;
protected $storeIndexes = [
'form',
'date',
];
public function getItemKey($item)
{
return $item->id();
}
public function getItemFilter(SplFileInfo $file)
{
$dir = Str::finish($this->directory(), '/');
$relative = Str::after(Path::tidy($file->getPathname()), $dir);
return $file->getExtension() === 'yaml' && substr_count($relative, '/') === 0;
}
public function makeItemFromFile($path, $contents)
{
$handle = pathinfo($path, PATHINFO_FILENAME);
try {
$data = YAML::file($path)->parse($contents);
} catch (ParseException $e) {
$data = [];
Log::warning('Could not parse form submission file: '.$path);
}
$data = $this->sanitizeData($data);
$form = pathinfo($path, PATHINFO_DIRNAME);
$form = Str::after($form, $this->parent->directory());
$form = Form::find($form);
$submission = FormSubmission::make()
->id($handle)
->form($form)
->data($data);
return $submission;
}
private function sanitizeData(array $data): array
{
return collect($data)->map(function ($value) {
if (is_array($value)) {
return $this->sanitizeData($value);
}
if (is_string($value) && ! mb_check_encoding($value, 'UTF-8')) {
return mb_convert_encoding($value, 'UTF-8', 'UTF-8');
}
return $value;
})->all();
}
}