18
18
use Hyde \Framework \Features \Publications \PublicationFieldTypes ;
19
19
use Hyde \Framework \Features \Publications \PublicationService ;
20
20
use Illuminate \Support \Collection ;
21
+ use Illuminate \Support \Str ;
21
22
use function implode ;
22
23
use function in_array ;
23
24
use InvalidArgumentException ;
@@ -42,15 +43,19 @@ class MakePublicationCommand extends ValidatingCommand
42
43
43
44
protected PublicationType $ publicationType ;
44
45
46
+ /** @var \Illuminate\Support\Collection<string, PublicationType> */
47
+ protected Collection $ fieldData ;
48
+
45
49
public function safeHandle (): int
46
50
{
47
51
$ this ->title ('Creating a new publication! ' );
48
52
49
53
$ this ->publicationType = $ this ->getPublicationTypeSelection ();
54
+ $ this ->fieldData = new Collection ();
50
55
51
- $ fieldData = $ this ->collectFieldData ();
56
+ $ this ->collectFieldData ();
52
57
53
- $ creator = new CreatesNewPublicationPage ($ this ->publicationType , $ fieldData , (bool ) $ this ->option ('force ' ));
58
+ $ creator = new CreatesNewPublicationPage ($ this ->publicationType , $ this -> fieldData , (bool ) $ this ->option ('force ' ));
54
59
if ($ creator ->hasFileConflict ()) {
55
60
$ this ->error ('Error: A publication already exists with the same canonical field value ' );
56
61
if ($ this ->confirm ('Do you wish to overwrite the existing file? ' )) {
@@ -63,20 +68,18 @@ public function safeHandle(): int
63
68
}
64
69
$ creator ->create ();
65
70
66
- $ this ->info ( " Created file { $ creator ->getOutputPath ()}" );
71
+ $ this ->infoComment ( ' All done! Created file' , $ creator ->getOutputPath ());
67
72
68
73
return Command::SUCCESS ;
69
74
}
70
75
71
76
protected function getPublicationTypeSelection (): PublicationType
72
77
{
73
78
$ publicationTypes = $ this ->getPublicationTypes ();
74
-
75
- $ publicationTypeSelection = $ this ->argument ('publicationType ' ) ?? $ publicationTypes ->keys ()->get (
76
- (int ) $ this ->choice (
77
- 'Which publication type would you like to create a publication item for? ' ,
78
- $ publicationTypes ->keys ()->toArray ()
79
- )
79
+ $ publicationTypeSelection = $ this ->argument ('publicationType ' ) ??
80
+ $ this ->choice (
81
+ 'Which publication type would you like to create a publication item for? ' ,
82
+ $ publicationTypes ->keys ()->toArray ()
80
83
);
81
84
82
85
if ($ publicationTypes ->has ($ publicationTypeSelection )) {
@@ -88,7 +91,6 @@ protected function getPublicationTypeSelection(): PublicationType
88
91
throw new InvalidArgumentException ("Unable to locate publication type [ $ publicationTypeSelection] " );
89
92
}
90
93
91
- /** @return \Illuminate\Support\Collection<string, PublicationType> */
92
94
protected function getPublicationTypes (): Collection
93
95
{
94
96
$ publicationTypes = PublicationService::getPublicationTypes ();
@@ -99,57 +101,50 @@ protected function getPublicationTypes(): Collection
99
101
return $ publicationTypes ;
100
102
}
101
103
102
- /** @return \Illuminate\Support\Collection<string, string|array|null> */
103
- protected function collectFieldData (): Collection
104
+ protected function collectFieldData (): void
104
105
{
105
106
$ this ->newLine ();
106
107
$ this ->info ('Now please enter the field data: ' );
107
- $ data = new Collection ();
108
108
109
109
/** @var PublicationField $field */
110
110
foreach ($ this ->publicationType ->getFields () as $ field ) {
111
111
if (str_starts_with ($ field ->name , '__ ' )) {
112
112
continue ;
113
113
}
114
+
114
115
$ this ->newLine ();
115
116
$ fieldInput = $ this ->captureFieldInput ($ field );
116
- if ($ fieldInput !== null ) {
117
- $ data ->put ($ field ->name , $ fieldInput );
117
+ if (empty ($ fieldInput )) {
118
+ $ this ->line ("<fg=gray> > Skipping field $ field ->name </> " );
119
+ } else {
120
+ $ this ->fieldData ->put ($ field ->name , $ fieldInput );
118
121
}
119
122
}
120
123
121
- return $ data ;
124
+ $ this -> newLine () ;
122
125
}
123
126
124
127
protected function captureFieldInput (PublicationField $ field ): ?PublicationFieldValue
125
128
{
126
- $ selection = match ($ field ->type ) {
129
+ return match ($ field ->type ) {
127
130
PublicationFieldTypes::Text => $ this ->captureTextFieldInput ($ field ),
128
131
PublicationFieldTypes::Array => $ this ->captureArrayFieldInput ($ field ),
129
132
PublicationFieldTypes::Image => $ this ->captureImageFieldInput ($ field ),
130
133
PublicationFieldTypes::Tag => $ this ->captureTagFieldInput ($ field ),
131
- default => new ( $ field -> type -> fieldClass ())( $ this ->askWithValidation ($ field-> name , " Enter data for field </>[<comment> $ field -> name </comment>] " , $ field -> getValidationRules ()-> toArray ()) ),
134
+ default => $ this ->captureOtherFieldInput ($ field ),
132
135
};
133
-
134
- if (empty ($ selection )) {
135
- $ this ->line ("<fg=gray> > Skipping field $ field ->name </> " );
136
-
137
- return null ;
138
- }
139
-
140
- return $ selection ;
141
136
}
142
137
143
138
protected function captureTextFieldInput (PublicationField $ field ): TextField
144
139
{
145
- $ this ->line (InputStreamHandler:: formatMessage ( $ field ->name , 'lines ' ) );
140
+ $ this ->infoComment ( ' Enter lines for field ' , $ field ->name , '</>(end with an empty line) ' );
146
141
147
142
return new TextField (implode ("\n" , InputStreamHandler::call ()));
148
143
}
149
144
150
145
protected function captureArrayFieldInput (PublicationField $ field ): ArrayField
151
146
{
152
- $ this ->line (InputStreamHandler:: formatMessage ( $ field ->name ) );
147
+ $ this ->infoComment ( ' Enter values for field ' , $ field ->name , ' </>(end with an empty line) ' );
153
148
154
149
return new ArrayField (InputStreamHandler::call ());
155
150
}
@@ -163,10 +158,7 @@ protected function captureImageFieldInput(PublicationField $field): ?ImageField
163
158
return $ this ->handleEmptyOptionsCollection ($ field , 'media file ' , "No media files found in directory _media/ {$ this ->publicationType ->getIdentifier ()}/ " );
164
159
}
165
160
166
- $ filesArray = $ mediaFiles ->toArray ();
167
- $ selection = (int ) $ this ->choice ('Which file would you like to use? ' , $ filesArray );
168
-
169
- return new ImageField ($ filesArray [$ selection ]);
161
+ return new ImageField ($ this ->choice ('Which file would you like to use? ' , $ mediaFiles ->toArray ()));
170
162
}
171
163
172
164
protected function captureTagFieldInput (PublicationField $ field ): ?TagField
@@ -189,6 +181,24 @@ protected function captureTagFieldInput(PublicationField $field): ?TagField
189
181
return new TagField ($ choice );
190
182
}
191
183
184
+ protected function captureOtherFieldInput (PublicationField $ field ): ?PublicationFieldValue
185
+ {
186
+ $ selection = $ this ->askForFieldData ($ field ->name , $ field ->getValidationRules ()->toArray ());
187
+ if (empty ($ selection )) {
188
+ return null ;
189
+ }
190
+
191
+ $ namespace = Str::beforeLast (PublicationFieldValue::class, '\\' );
192
+ $ className = "$ namespace \\{$ field ->type ->name }Field " ;
193
+
194
+ return new $ className ($ selection );
195
+ }
196
+
197
+ protected function askForFieldData (string $ name , array $ rules ): string
198
+ {
199
+ return $ this ->askWithValidation ($ name , "Enter data for field </>[<comment> $ name</comment>] " , $ rules );
200
+ }
201
+
192
202
/** @return null */
193
203
protected function handleEmptyOptionsCollection (PublicationField $ field , string $ type , string $ message )
194
204
{
@@ -197,7 +207,7 @@ protected function handleEmptyOptionsCollection(PublicationField $field, string
197
207
}
198
208
199
209
$ this ->newLine ();
200
- $ this ->warn (" <fg=red>Warning:</> $ message " );
210
+ $ this ->warn ("<fg=red>Warning:</> $ message " );
201
211
if ($ this ->confirm ('Would you like to skip this field? ' , true )) {
202
212
return null ;
203
213
} else {
0 commit comments