3
3
namespace Biigle \Rules ;
4
4
5
5
use Illuminate \Contracts \Validation \Rule ;
6
+ use Biigle \Services \MetadataParsing \VolumeMetadata ;
6
7
7
8
class ImageMetadata implements Rule
8
9
{
9
- /**
10
- * Allowed columns for the metadata information to change image attributes.
11
- *
12
- * @var array
13
- */
14
- const ALLOWED_ATTRIBUTES = [
15
- 'lat ' ,
16
- 'lng ' ,
17
- 'taken_at ' ,
18
- ];
19
-
20
- /**
21
- * Allowed columns for the metadata information to change image metadata.
22
- *
23
- * @var array
24
- */
25
- const ALLOWED_METADATA = [
26
- 'area ' ,
27
- 'distance_to_ground ' ,
28
- 'gps_altitude ' ,
29
- 'yaw ' ,
30
- ];
31
-
32
10
/**
33
11
* All numeric metadata fields (keys) with description (values).
34
12
*
35
13
* @var array
36
14
*/
37
15
const NUMERIC_FIELDS = [
38
16
'area ' => 'area ' ,
39
- 'distance_to_ground ' => 'distance to ground ' ,
40
- 'gps_altitude ' => 'GPS altitude ' ,
17
+ 'distanceToGround ' => 'distance to ground ' ,
18
+ 'gpsAltitude ' => 'GPS altitude ' ,
41
19
'lat ' => 'latitude ' ,
42
20
'lng ' => 'longitude ' ,
43
21
'yaw ' => 'yaw ' ,
44
22
];
45
23
46
- /**
47
- * Array of volume file names.
48
- *
49
- * @var array
50
- */
51
- protected $ files ;
52
-
53
24
/**
54
25
* The validation error message.
55
26
*
@@ -62,157 +33,84 @@ class ImageMetadata implements Rule
62
33
*
63
34
* @param array $files
64
35
*/
65
- public function __construct ($ files )
36
+ public function __construct (public array $ files )
66
37
{
67
- $ this ->files = $ files ;
68
38
$ this ->message = "The :attribute is invalid. " ;
69
39
}
70
40
71
41
/**
72
42
* Determine if the validation rule passes.
73
- *
74
- * @param string $attribute
75
- * @param array $value
76
- * @return bool
77
43
*/
78
- public function passes ($ attribute , $ value )
44
+ public function passes ($ attribute , $ value ): bool
79
45
{
80
- if (!is_array ($ value )) {
81
- return false ;
46
+ if (!($ value instanceof VolumeMetadata )) {
47
+ throw new \ Exception ( ' No value of type ' .VolumeMetadata::class. ' given. ' ) ;
82
48
}
83
49
50
+ $ fileMetadata = $ value ->getFiles ();
84
51
// This checks if any information is given at all.
85
- if (empty ($ value )) {
86
- $ this ->message = 'The metadata information is empty. ' ;
87
-
88
- return false ;
89
- }
90
-
91
- $ columns = array_shift ($ value );
92
-
93
- // This checks if any information is given beside the column description.
94
- if (empty ($ value )) {
52
+ if ($ fileMetadata ->isEmpty ()) {
95
53
$ this ->message = 'The metadata information is empty. ' ;
96
54
97
55
return false ;
98
56
}
99
57
100
- if (!in_array ('filename ' , $ columns )) {
101
- $ this ->message = 'The filename column is required. ' ;
102
-
103
- return false ;
104
- }
105
-
106
- $ colCount = count ($ columns );
58
+ foreach ($ fileMetadata as $ file ) {
107
59
108
- if ($ colCount === 1 ) {
109
- $ this ->message = 'No metadata columns given. ' ;
110
-
111
- return false ;
112
- }
113
-
114
- if ($ colCount !== count (array_unique ($ columns ))) {
115
- $ this ->message = 'Each column may occur only once. ' ;
116
-
117
- return false ;
118
- }
119
-
120
- $ allowedColumns = array_merge (['filename ' ], self ::ALLOWED_ATTRIBUTES , self ::ALLOWED_METADATA );
121
- $ diff = array_diff ($ columns , $ allowedColumns );
122
-
123
- if (count ($ diff ) > 0 ) {
124
- $ this ->message = 'The columns array may contain only values of: ' .implode (', ' , $ allowedColumns ).'. ' ;
125
-
126
- return false ;
127
- }
128
-
129
- $ lng = in_array ('lng ' , $ columns );
130
- $ lat = in_array ('lat ' , $ columns );
131
- if ($ lng && !$ lat || !$ lng && $ lat ) {
132
- $ this ->message = "If the 'lng' column is present, the 'lat' column must be present, too (and vice versa). " ;
133
-
134
- return false ;
135
- }
136
-
137
- foreach ($ value as $ index => $ row ) {
138
- // +1 since index starts at 0.
139
- // +1 since column description row was removed above.
140
- $ line = $ index + 2 ;
141
-
142
- if (count ($ row ) !== $ colCount ) {
143
- $ this ->message = "Invalid column count in line {$ line }. " ;
144
-
145
- return false ;
146
- }
147
-
148
- $ combined = array_combine ($ columns , $ row );
149
- $ combined = array_filter ($ combined );
150
- if (!array_key_exists ('filename ' , $ combined )) {
151
- $ this ->message = "Filename missing in line {$ line }. " ;
60
+ if (!in_array ($ file ->name , $ this ->files )) {
61
+ $ this ->message = "There is no file with filename {$ file ->name }. " ;
152
62
153
63
return false ;
154
64
}
155
65
156
- $ filename = $ combined ['filename ' ];
157
-
158
- if (!in_array ($ filename , $ this ->files )) {
159
- $ this ->message = "There is no file with filename {$ filename }. " ;
160
-
161
- return false ;
162
- }
163
-
164
- if (array_key_exists ('lng ' , $ combined )) {
165
- $ lng = $ combined ['lng ' ];
166
- if (!is_numeric ($ lng ) || abs ($ lng ) > 180 ) {
167
- $ this ->message = "' {$ lng }' is no valid longitude for file {$ filename }. " ;
66
+ if (!is_null ($ file ->lng )) {
67
+ if (!is_numeric ($ file ->lng ) || abs ($ file ->lng ) > 180 ) {
68
+ $ this ->message = "' {$ file ->lng }' is no valid longitude for file {$ file ->name }. " ;
168
69
169
70
return false ;
170
71
}
171
72
172
-
173
- if (!array_key_exists ('lat ' , $ combined )) {
174
- $ this ->message = "Missing latitude for file {$ filename }. " ;
73
+ if (is_null ($ file ->lat )) {
74
+ $ this ->message = "Missing latitude for file {$ file ->name }. " ;
175
75
176
76
return false ;
177
77
}
178
78
}
179
79
180
- if (array_key_exists ('lat ' , $ combined )) {
181
- $ lat = $ combined ['lat ' ];
182
- if (!is_numeric ($ lat ) || abs ($ lat ) > 90 ) {
183
- $ this ->message = "' {$ lat }' is no valid latitude for file {$ filename }. " ;
80
+ if (!is_null ($ file ->lat )) {
81
+ if (!is_numeric ($ file ->lat ) || abs ($ file ->lat ) > 90 ) {
82
+ $ this ->message = "' {$ file ->lat }' is no valid latitude for file {$ file ->name }. " ;
184
83
185
84
return false ;
186
85
}
187
86
188
- if (! array_key_exists ( ' lng ' , $ combined )) {
189
- $ this ->message = "Missing longitude for file {$ filename }. " ;
87
+ if (is_null ( $ file -> lng )) {
88
+ $ this ->message = "Missing longitude for file {$ file -> name }. " ;
190
89
191
90
return false ;
192
91
}
193
92
}
194
93
195
94
// Catch both a malformed date (false) and the zero date (negative integer).
196
- if (array_key_exists ('taken_at ' , $ combined )) {
197
- $ date = $ combined ['taken_at ' ];
198
- if (!(strtotime ($ date ) > 0 )) {
199
- $ this ->message = "' {$ date }' is no valid date for file {$ filename }. " ;
95
+ if (!is_null ($ file ->takenAt )) {
96
+ if (!(strtotime ($ file ->takenAt ) > 0 )) {
97
+ $ this ->message = "' {$ file ->takenAt }' is no valid date for file {$ file ->name }. " ;
200
98
201
99
return false ;
202
100
}
203
101
}
204
102
205
103
foreach (self ::NUMERIC_FIELDS as $ key => $ text ) {
206
- if (array_key_exists ( $ key , $ combined ) && !is_numeric ($ combined [ $ key] )) {
207
- $ this ->message = "' {$ combined [ $ key] }' is no valid {$ text } for file {$ filename }. " ;
104
+ if (! is_null ( $ file -> $ key ) && !is_numeric ($ file -> $ key )) {
105
+ $ this ->message = "' {$ file -> $ key }' is no valid {$ text } for file {$ file -> name }. " ;
208
106
209
107
return false ;
210
108
}
211
109
}
212
110
213
- if (array_key_exists ( ' yaw ' , $ combined )) {
214
- if ($ combined [ ' yaw ' ] < 0 || $ combined [ ' yaw ' ] > 360 ) {
215
- $ this ->message = "' {$ combined [ ' yaw ' ] }' is no valid yaw for file {$ filename }. " ;
111
+ if (! is_null ( $ file -> yaw )) {
112
+ if ($ file -> yaw < 0 || $ file -> yaw > 360 ) {
113
+ $ this ->message = "' {$ file -> yaw }' is no valid yaw for file {$ file -> name }. " ;
216
114
217
115
return false ;
218
116
}
0 commit comments