Skip to content

Commit 2437e98

Browse files
committed
Fix VideoMetadata validation rule for new metadata objects
1 parent 480cec7 commit 2437e98

File tree

4 files changed

+234
-105
lines changed

4 files changed

+234
-105
lines changed

app/Rules/ImageMetadata.php

Lines changed: 60 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Contracts\Validation\Rule;
66
use Biigle\Services\MetadataParsing\VolumeMetadata;
7+
use Biigle\Services\MetadataParsing\FileMetadata;
78

89
class ImageMetadata implements Rule
910
{
@@ -47,86 +48,95 @@ public function passes($attribute, $value): bool
4748
throw new \Exception('No value of type '.VolumeMetadata::class.' given.');
4849
}
4950

50-
$fileMetadata = $value->getFiles();
5151
// This checks if any information is given at all.
52-
if ($fileMetadata->isEmpty()) {
52+
if ($value->isEmpty()) {
5353
$this->message = 'The metadata information is empty.';
5454

5555
return false;
5656
}
5757

58-
foreach ($fileMetadata as $file) {
59-
60-
if (!in_array($file->name, $this->files)) {
61-
$this->message = "There is no file with filename {$file->name}.";
58+
$fileMetadata = $value->getFiles();
6259

60+
foreach ($fileMetadata as $file) {
61+
if (!$this->fileMetadataPasses($file)) {
6362
return false;
6463
}
64+
}
65+
66+
return true;
67+
}
68+
69+
/**
70+
* Get the validation error message.
71+
*
72+
* @return string
73+
*/
74+
public function message()
75+
{
76+
return $this->message;
77+
}
6578

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}.";
79+
protected function fileMetadataPasses(FileMetadata $file)
80+
{
81+
if (!in_array($file->name, $this->files)) {
82+
$this->message = "There is no file with filename {$file->name}.";
6983

70-
return false;
71-
}
84+
return false;
85+
}
7286

73-
if (is_null($file->lat)) {
74-
$this->message = "Missing latitude for file {$file->name}.";
87+
if (!is_null($file->lng)) {
88+
if (!is_numeric($file->lng) || abs($file->lng) > 180) {
89+
$this->message = "'{$file->lng}' is no valid longitude for file {$file->name}.";
7590

76-
return false;
77-
}
91+
return false;
7892
}
7993

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}.";
94+
if (is_null($file->lat)) {
95+
$this->message = "Missing latitude for file {$file->name}.";
8396

84-
return false;
85-
}
97+
return false;
98+
}
99+
}
86100

87-
if (is_null($file->lng)) {
88-
$this->message = "Missing longitude for file {$file->name}.";
101+
if (!is_null($file->lat)) {
102+
if (!is_numeric($file->lat) || abs($file->lat) > 90) {
103+
$this->message = "'{$file->lat}' is no valid latitude for file {$file->name}.";
89104

90-
return false;
91-
}
105+
return false;
92106
}
93107

94-
// Catch both a malformed date (false) and the zero date (negative integer).
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}.";
108+
if (is_null($file->lng)) {
109+
$this->message = "Missing longitude for file {$file->name}.";
98110

99-
return false;
100-
}
111+
return false;
101112
}
113+
}
102114

103-
foreach (self::NUMERIC_FIELDS as $key => $text) {
104-
if (!is_null($file->$key) && !is_numeric($file->$key)) {
105-
$this->message = "'{$file->$key}' is no valid {$text} for file {$file->name}.";
115+
// Catch both a malformed date (false) and the zero date (negative integer).
116+
if (!is_null($file->takenAt)) {
117+
if (!(strtotime($file->takenAt) > 0)) {
118+
$this->message = "'{$file->takenAt}' is no valid date for file {$file->name}.";
106119

107-
return false;
108-
}
120+
return false;
109121
}
122+
}
110123

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}.";
124+
foreach (self::NUMERIC_FIELDS as $key => $text) {
125+
if (!is_null($file->$key) && !is_numeric($file->$key)) {
126+
$this->message = "'{$file->$key}' is no valid {$text} for file {$file->name}.";
114127

115-
return false;
116-
}
128+
return false;
117129
}
118130
}
119131

120-
return true;
121-
}
132+
if (!is_null($file->yaw)) {
133+
if ($file->yaw < 0 || $file->yaw > 360) {
134+
$this->message = "'{$file->yaw}' is no valid yaw for file {$file->name}.";
122135

123-
/**
124-
* Get the validation error message.
125-
*
126-
* @return string
127-
*/
128-
public function message()
129-
{
130-
return $this->message;
136+
return false;
137+
}
138+
}
139+
140+
return true;
131141
}
132142
}

app/Rules/VideoMetadata.php

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,13 @@ public function passes($attribute, $value): bool
1515
return false;
1616
}
1717

18-
$columns = array_shift($value);
19-
20-
$filenames = [];
21-
foreach ($value as $index => $row) {
22-
$combined = array_combine($columns, $row);
23-
$combined = array_filter($combined);
24-
$filename = $combined['filename'];
25-
if (array_key_exists($filename, $filenames)) {
26-
// If this exists, it was already checked if it is a valid date by the
27-
// parent method.
28-
if (!array_key_exists('taken_at', $combined)) {
29-
// +1 since index starts at 0.
30-
// +1 since column description row was removed above.
31-
$line = $index + 2;
32-
33-
$this->message = "File {$filename} has multiple entries but no 'taken_at' at line {$line}.";
18+
$fileMetadata = $value->getFiles();
3419

20+
foreach ($fileMetadata as $file) {
21+
foreach ($file->getFrames() as $frame) {
22+
if (!$this->fileMetadataPasses($frame)) {
3523
return false;
3624
}
37-
} else {
38-
$filenames[$filename] = true;
3925
}
4026
}
4127

tests/php/Rules/ImageMetadataTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,15 @@ public function testEmptyFilename()
9999
{
100100
$validator = new ImageMetadataRule(['abc.jpg']);
101101
$metadata = new VolumeMetadata;
102-
$metadata->addFile(new ImageMetadata(
103-
name: 'abc.jpg'
104-
));
105-
$metadata->addFile(new ImageMetadata(
106-
name: ''
107-
));
102+
$metadata->addFile(new ImageMetadata(name: ''));
103+
$this->assertFalse($validator->passes(null, $metadata));
104+
}
105+
106+
public function testEmpty()
107+
{
108+
$validator = new ImageMetadataRule(['abc.jpg']);
109+
$metadata = new VolumeMetadata;
110+
$metadata->addFile(new ImageMetadata(name: 'abc.jpg'));
108111
$this->assertFalse($validator->passes(null, $metadata));
109112
}
110113
}

0 commit comments

Comments
 (0)