6 * Utility class for handling metadata
8 abstract class H5PMetadata
{
10 private static $fields = array(
33 'licenseVersion' => array(
37 'licenseExtras' => array(
41 'authorComments' => array(
51 'defaultLanguage' => array(
58 * JSON encode metadata
60 * @param object $content
63 public static function toJSON($content) {
64 // Note: deliberatly creating JSON string "manually" to improve performance
66 '{"title":' . (isset($content->title
) ?
json_encode($content->title
) : 'null') .
67 ',"a11yTitle":' . (isset($content->a11y_title
) ?
$content->a11y_title
: 'null') .
68 ',"authors":' . (isset($content->authors
) ?
$content->authors
: 'null') .
69 ',"source":' . (isset($content->source
) ?
'"' . $content->source
. '"' : 'null') .
70 ',"license":' . (isset($content->license
) ?
'"' . $content->license
. '"' : 'null') .
71 ',"licenseVersion":' . (isset($content->license_version
) ?
'"' . $content->license_version
. '"' : 'null') .
72 ',"licenseExtras":' . (isset($content->license_extras
) ?
json_encode($content->license_extras
) : 'null') .
73 ',"yearFrom":' . (isset($content->year_from
) ?
$content->year_from
: 'null') .
74 ',"yearTo":' . (isset($content->year_to
) ?
$content->year_to
: 'null') .
75 ',"changes":' . (isset($content->changes
) ?
$content->changes
: 'null') .
76 ',"defaultLanguage":' . (isset($content->default_language
) ?
'"' . $content->default_language
. '"' : 'null') .
77 ',"authorComments":' . (isset($content->author_comments
) ?
json_encode($content->author_comments
) : 'null') . '}';
81 * Make the metadata into an associative array keyed by the property names
82 * @param mixed $metadata Array or object containing metadata
83 * @param bool $include_title
84 * @param bool $include_missing For metadata fields not being set, skip 'em.
85 * Relevant for content upgrade
89 public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
92 if (!is_array($metadata)) {
93 $metadata = (array) $metadata;
96 foreach (self
::$fields as $key => $config) {
99 if ($key === 'title' && !$include_title) {
103 $exists = array_key_exists($key, $metadata);
105 // Don't include missing fields
106 if (!$include_missing && !$exists) {
110 $value = $exists ?
$metadata[$key] : null;
112 // lowerCamelCase to snake_case
113 $db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
115 switch ($config['type']) {
117 if ($value !== null && strlen($value) > $config['maxLength']) {
118 $value = mb_substr($value, 0, $config['maxLength']);
124 $value = ($value !== null) ?
intval($value) : null;
129 $value = ($value !== null) ?
json_encode($value) : null;
134 $fields[$db_field_name] = $value;
141 * The metadataSettings field in libraryJson uses 1 for true and 0 for false.
142 * Here we are converting these to booleans, and also doing JSON encoding.
143 * This is invoked before the library data is beeing inserted/updated to DB.
145 * @param array $metadataSettings
148 public static function boolifyAndEncodeSettings($metadataSettings) {
149 // Convert metadataSettings values to boolean
150 if (isset($metadataSettings['disable'])) {
151 $metadataSettings['disable'] = $metadataSettings['disable'] === 1;
153 if (isset($metadataSettings['disableExtraTitleField'])) {
154 $metadataSettings['disableExtraTitleField'] = $metadataSettings['disableExtraTitleField'] === 1;
157 return json_encode($metadataSettings);