NOBUG: Fixed SVG browser compatibility
[moodle.git] / h5p / h5plib / v124 / joubel / core / h5p-metadata.class.php
blob3c98aedb3db359b35775c842c5ac62d06a6fd73d
1 <?php
3 namespace Moodle;
5 /**
6 * Utility class for handling metadata
7 */
8 abstract class H5PMetadata {
10 private static $fields = array(
11 'title' => array(
12 'type' => 'text',
13 'maxLength' => 255
15 'a11yTitle' => array(
16 'type' => 'text',
17 'maxLength' => 255,
19 'authors' => array(
20 'type' => 'json'
22 'changes' => array(
23 'type' => 'json'
25 'source' => array(
26 'type' => 'text',
27 'maxLength' => 255
29 'license' => array(
30 'type' => 'text',
31 'maxLength' => 32
33 'licenseVersion' => array(
34 'type' => 'text',
35 'maxLength' => 10
37 'licenseExtras' => array(
38 'type' => 'text',
39 'maxLength' => 5000
41 'authorComments' => array(
42 'type' => 'text',
43 'maxLength' => 5000
45 'yearFrom' => array(
46 'type' => 'int'
48 'yearTo' => array(
49 'type' => 'int'
51 'defaultLanguage' => array(
52 'type' => 'text',
53 'maxLength' => 32,
57 /**
58 * JSON encode metadata
60 * @param object $content
61 * @return string
63 public static function toJSON($content) {
64 // Note: deliberatly creating JSON string "manually" to improve performance
65 return
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') . '}';
80 /**
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
86 * @param array $types
87 * @return array
89 public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
90 $fields = array();
92 if (!is_array($metadata)) {
93 $metadata = (array) $metadata;
96 foreach (self::$fields as $key => $config) {
98 // Ignore title?
99 if ($key === 'title' && !$include_title) {
100 continue;
103 $exists = array_key_exists($key, $metadata);
105 // Don't include missing fields
106 if (!$include_missing && !$exists) {
107 continue;
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']) {
116 case 'text':
117 if ($value !== null && strlen($value) > $config['maxLength']) {
118 $value = mb_substr($value, 0, $config['maxLength']);
120 $types[] = '%s';
121 break;
123 case 'int':
124 $value = ($value !== null) ? intval($value) : null;
125 $types[] = '%d';
126 break;
128 case 'json':
129 $value = ($value !== null) ? json_encode($value) : null;
130 $types[] = '%s';
131 break;
134 $fields[$db_field_name] = $value;
137 return $fields;
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
146 * @return string
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);