PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/h5p/h5plib/v124/joubel/core/h5p-metadata.class.php

https://github.com/mackensen/moodle
PHP | 154 lines | 104 code | 19 blank | 31 comment | 10 complexity | d380441dc130f4a56669d6675213a7c2 MD5 | raw file
  1. <?php
  2. namespace Moodle;
  3. /**
  4. * Utility class for handling metadata
  5. */
  6. abstract class H5PMetadata {
  7. private static $fields = array(
  8. 'title' => array(
  9. 'type' => 'text',
  10. 'maxLength' => 255
  11. ),
  12. 'authors' => array(
  13. 'type' => 'json'
  14. ),
  15. 'changes' => array(
  16. 'type' => 'json'
  17. ),
  18. 'source' => array(
  19. 'type' => 'text',
  20. 'maxLength' => 255
  21. ),
  22. 'license' => array(
  23. 'type' => 'text',
  24. 'maxLength' => 32
  25. ),
  26. 'licenseVersion' => array(
  27. 'type' => 'text',
  28. 'maxLength' => 10
  29. ),
  30. 'licenseExtras' => array(
  31. 'type' => 'text',
  32. 'maxLength' => 5000
  33. ),
  34. 'authorComments' => array(
  35. 'type' => 'text',
  36. 'maxLength' => 5000
  37. ),
  38. 'yearFrom' => array(
  39. 'type' => 'int'
  40. ),
  41. 'yearTo' => array(
  42. 'type' => 'int'
  43. ),
  44. 'defaultLanguage' => array(
  45. 'type' => 'text',
  46. 'maxLength' => 32,
  47. )
  48. );
  49. /**
  50. * JSON encode metadata
  51. *
  52. * @param object $content
  53. * @return string
  54. */
  55. public static function toJSON($content) {
  56. // Note: deliberatly creating JSON string "manually" to improve performance
  57. return
  58. '{"title":' . (isset($content->title) ? json_encode($content->title) : 'null') .
  59. ',"authors":' . (isset($content->authors) ? $content->authors : 'null') .
  60. ',"source":' . (isset($content->source) ? '"' . $content->source . '"' : 'null') .
  61. ',"license":' . (isset($content->license) ? '"' . $content->license . '"' : 'null') .
  62. ',"licenseVersion":' . (isset($content->license_version) ? '"' . $content->license_version . '"' : 'null') .
  63. ',"licenseExtras":' . (isset($content->license_extras) ? json_encode($content->license_extras) : 'null') .
  64. ',"yearFrom":' . (isset($content->year_from) ? $content->year_from : 'null') .
  65. ',"yearTo":' . (isset($content->year_to) ? $content->year_to : 'null') .
  66. ',"changes":' . (isset($content->changes) ? $content->changes : 'null') .
  67. ',"defaultLanguage":' . (isset($content->default_language) ? '"' . $content->default_language . '"' : 'null') .
  68. ',"authorComments":' . (isset($content->author_comments) ? json_encode($content->author_comments) : 'null') . '}';
  69. }
  70. /**
  71. * Make the metadata into an associative array keyed by the property names
  72. * @param mixed $metadata Array or object containing metadata
  73. * @param bool $include_title
  74. * @param bool $include_missing For metadata fields not being set, skip 'em.
  75. * Relevant for content upgrade
  76. * @param array $types
  77. * @return array
  78. */
  79. public static function toDBArray($metadata, $include_title = true, $include_missing = true, &$types = array()) {
  80. $fields = array();
  81. if (!is_array($metadata)) {
  82. $metadata = (array) $metadata;
  83. }
  84. foreach (self::$fields as $key => $config) {
  85. // Ignore title?
  86. if ($key === 'title' && !$include_title) {
  87. continue;
  88. }
  89. $exists = array_key_exists($key, $metadata);
  90. // Don't include missing fields
  91. if (!$include_missing && !$exists) {
  92. continue;
  93. }
  94. $value = $exists ? $metadata[$key] : null;
  95. // lowerCamelCase to snake_case
  96. $db_field_name = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $key));
  97. switch ($config['type']) {
  98. case 'text':
  99. if ($value !== null && strlen($value) > $config['maxLength']) {
  100. $value = mb_substr($value, 0, $config['maxLength']);
  101. }
  102. $types[] = '%s';
  103. break;
  104. case 'int':
  105. $value = ($value !== null) ? intval($value) : null;
  106. $types[] = '%d';
  107. break;
  108. case 'json':
  109. $value = ($value !== null) ? json_encode($value) : null;
  110. $types[] = '%s';
  111. break;
  112. }
  113. $fields[$db_field_name] = $value;
  114. }
  115. return $fields;
  116. }
  117. /**
  118. * The metadataSettings field in libraryJson uses 1 for true and 0 for false.
  119. * Here we are converting these to booleans, and also doing JSON encoding.
  120. * This is invoked before the library data is beeing inserted/updated to DB.
  121. *
  122. * @param array $metadataSettings
  123. * @return string
  124. */
  125. public static function boolifyAndEncodeSettings($metadataSettings) {
  126. // Convert metadataSettings values to boolean
  127. if (isset($metadataSettings['disable'])) {
  128. $metadataSettings['disable'] = $metadataSettings['disable'] === 1;
  129. }
  130. if (isset($metadataSettings['disableExtraTitleField'])) {
  131. $metadataSettings['disableExtraTitleField'] = $metadataSettings['disableExtraTitleField'] === 1;
  132. }
  133. return json_encode($metadataSettings);
  134. }
  135. }