From eb766b0081cd75bf5303f6f6bb75481b4778608c Mon Sep 17 00:00:00 2001 From: Paal Joergensen Date: Mon, 17 Sep 2018 11:47:05 +0200 Subject: [PATCH] HFP-2184 Add utility class for handling metadata --- h5p-metadata.class.php | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 h5p-metadata.class.php diff --git a/h5p-metadata.class.php b/h5p-metadata.class.php new file mode 100644 index 0000000..b60158f --- /dev/null +++ b/h5p-metadata.class.php @@ -0,0 +1,84 @@ + array( + 'type' => 'json' + ), + 'changes' => array( + 'type' => 'json' + ), + 'source' => array( + 'type' => 'text', + 'maxLength' => 255 + ), + 'license' => array( + 'type' => 'text', + 'maxLength' => 32 + ), + 'licenseVersion' => array( + 'type' => 'text', + 'maxLength' => 10 + ), + 'licenseExtras' => array( + 'type' => 'text', + 'maxLength' => 5000 + ), + 'authorComments' => array( + 'type' => 'text', + 'maxLength' => 5000 + ), + 'yearFrom' => array( + 'type' => 'int' + ), + 'yearTo' => array( + 'type' => 'int' + ) + ); + + /** + * Make the metadata into an associative array keyed by the property names + * @param mixed $metadata Array or object containing metadata + * @return array + */ + public static function toDBArray($metadata, &$types = array()) { + $fields = array(); + + if (!is_array($metadata)) { + $metadata = (array) $metadata; + } + + foreach (self::FIELDS as $key => $config) { + if (isset($metadata[$key])) { + $value = $metadata[$key]; + $db_field_name = strtolower(preg_replace('/(? $config['maxLength']) { + $value = mb_substr($value, 0, $config['maxLength']); + } + $types[] = '%s'; + break; + + case 'int': + $value = ($value !== null) ? intval($value): null; + $types[] = '%i'; + break; + + case 'json': + $value = json_encode($value); + $types[] = '%s'; + break; + } + + $fields[$db_field_name] = $value; + } + } + + return $fields; + } +}