Cast metadata tags values according to type

This commit is contained in:
Mathieu Darse
2015-11-10 11:51:57 +01:00
parent e953998ca7
commit 54cc0132b7

View File

@@ -76,23 +76,7 @@ SQL;
case 'caption':
// Sanitize fields
$value = StringHelper::crlfNormalize($value);
switch ($this->structure->typeOf($key)) {
case Mapping::TYPE_DATE:
$value = $this->helper->sanitizeDate($value);
break;
case Mapping::TYPE_FLOAT:
case Mapping::TYPE_DOUBLE:
$value = (float) $value;
break;
case Mapping::TYPE_INTEGER:
case Mapping::TYPE_LONG:
case Mapping::TYPE_SHORT:
case Mapping::TYPE_BYTE:
$value = (int) $value;
break;
}
$value = $this->sanitizeValue($value, $this->structure->typeOf($key));
// Private caption fields are kept apart
$type = $metadata['private'] ? 'private_caption' : 'caption';
// Caption are multi-valued
@@ -110,6 +94,10 @@ SQL;
case 'exif':
// EXIF data is single-valued
$tag = $this->structure->getMetadataTagByName($key);
if ($tag) {
$value = $this->sanitizeValue($value, $tag->getType());
}
$record['exif'][$key] = $value;
break;
@@ -119,4 +107,28 @@ SQL;
}
}
}
private function sanitizeValue($value, $type)
{
switch ($type) {
case Mapping::TYPE_DATE:
return $this->helper->sanitizeDate($value);
case Mapping::TYPE_FLOAT:
case Mapping::TYPE_DOUBLE:
return (float) $value;
case Mapping::TYPE_INTEGER:
case Mapping::TYPE_LONG:
case Mapping::TYPE_SHORT:
case Mapping::TYPE_BYTE:
return (int) $value;
case Mapping::TYPE_BOOLEAN:
return (bool) $value;
default:
return $value;
}
}
}