|
4 | 4 |
|
5 | 5 | use Illuminate\Contracts\Validation\Rule;
|
6 | 6 | use Biigle\Services\MetadataParsing\VolumeMetadata;
|
| 7 | +use Biigle\Services\MetadataParsing\FileMetadata; |
7 | 8 |
|
8 | 9 | class ImageMetadata implements Rule
|
9 | 10 | {
|
@@ -47,86 +48,95 @@ public function passes($attribute, $value): bool
|
47 | 48 | throw new \Exception('No value of type '.VolumeMetadata::class.' given.');
|
48 | 49 | }
|
49 | 50 |
|
50 |
| - $fileMetadata = $value->getFiles(); |
51 | 51 | // This checks if any information is given at all.
|
52 |
| - if ($fileMetadata->isEmpty()) { |
| 52 | + if ($value->isEmpty()) { |
53 | 53 | $this->message = 'The metadata information is empty.';
|
54 | 54 |
|
55 | 55 | return false;
|
56 | 56 | }
|
57 | 57 |
|
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(); |
62 | 59 |
|
| 60 | + foreach ($fileMetadata as $file) { |
| 61 | + if (!$this->fileMetadataPasses($file)) { |
63 | 62 | return false;
|
64 | 63 | }
|
| 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 | + } |
65 | 78 |
|
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}."; |
69 | 83 |
|
70 |
| - return false; |
71 |
| - } |
| 84 | + return false; |
| 85 | + } |
72 | 86 |
|
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}."; |
75 | 90 |
|
76 |
| - return false; |
77 |
| - } |
| 91 | + return false; |
78 | 92 | }
|
79 | 93 |
|
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}."; |
83 | 96 |
|
84 |
| - return false; |
85 |
| - } |
| 97 | + return false; |
| 98 | + } |
| 99 | + } |
86 | 100 |
|
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}."; |
89 | 104 |
|
90 |
| - return false; |
91 |
| - } |
| 105 | + return false; |
92 | 106 | }
|
93 | 107 |
|
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}."; |
98 | 110 |
|
99 |
| - return false; |
100 |
| - } |
| 111 | + return false; |
101 | 112 | }
|
| 113 | + } |
102 | 114 |
|
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}."; |
106 | 119 |
|
107 |
| - return false; |
108 |
| - } |
| 120 | + return false; |
109 | 121 | }
|
| 122 | + } |
110 | 123 |
|
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}."; |
114 | 127 |
|
115 |
| - return false; |
116 |
| - } |
| 128 | + return false; |
117 | 129 | }
|
118 | 130 | }
|
119 | 131 |
|
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}."; |
122 | 135 |
|
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; |
131 | 141 | }
|
132 | 142 | }
|
0 commit comments