Repository navigation
Expand file tree
/
Copy pathImportColumn.php
More file actions
147 lines (116 loc) · 3.28 KB
/
Copy pathImportColumn.php
File metadata and controls
147 lines (116 loc) · 3.28 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
declare(strict_types=1);
namespace NyonCode\WireTable\Import;
use Closure;
use NyonCode\WireCore\Foundation\Support\NameLabel;
/**
* Maps one column of an imported file to a model attribute.
*
* A column matches a file header by its label, its name, or any {@see guess()}
* alias (case-insensitive, trimmed). The raw cell value can be transformed with
* {@see castStateUsing()} and gated with per-cell validation {@see rules()}.
* {@see requiredMapping()} marks a header the file must contain.
*/
class ImportColumn
{
protected string|Closure|null $label = null;
protected bool $requiredMapping = false;
/** @var array<int, mixed> */
protected array $rules = [];
protected ?Closure $castCallback = null;
/** @var array<int, string> */
protected array $guesses = [];
final public function __construct(protected string $name) {}
public static function make(string $name): static
{
return new static($name);
}
public function label(string|Closure|null $label): static
{
$this->label = $label;
return $this;
}
public function requiredMapping(bool $condition = true): static
{
$this->requiredMapping = $condition;
return $this;
}
/**
* @param array<int, mixed> $rules
*/
public function rules(array $rules): static
{
$this->rules = $rules;
return $this;
}
public function castStateUsing(Closure $callback): static
{
$this->castCallback = $callback;
return $this;
}
/**
* Alternative header names this column also matches.
*
* @param array<int, string> $names
*/
public function guess(array $names): static
{
$this->guesses = $names;
return $this;
}
public function getName(): string
{
return $this->name;
}
public function getLabel(): string
{
$label = $this->label instanceof Closure ? ($this->label)() : $this->label;
return $label ?? NameLabel::for($this->name);
}
public function isRequiredMapping(): bool
{
return $this->requiredMapping;
}
/**
* @return array<int, mixed>
*/
public function getRules(): array
{
return $this->rules;
}
/**
* @return array<int, string>
*/
public function getGuesses(): array
{
return $this->guesses;
}
/**
* Locate the file header this column maps to, or null when absent.
*
* @param array<int, string> $headers Header names as they appear in the file.
*/
public function resolveHeader(array $headers): ?string
{
$candidates = array_map(
static fn (string $value): string => mb_strtolower(trim($value)),
[$this->getLabel(), $this->name, ...$this->guesses],
);
foreach ($headers as $header) {
if (in_array(mb_strtolower(trim($header)), $candidates, true)) {
return $header;
}
}
return null;
}
/**
* Transform a raw cell value into the value stored on the model.
*/
public function castState(mixed $value): mixed
{
if ($this->castCallback !== null) {
return ($this->castCallback)($value);
}
return $value;
}
}