v1.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace zin;
  3. class severityLabel extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array(
  9. 'level: string|int', // 严重程度等级。
  10. 'text?: string|array', // 标签文本或标签文本映射对象,如果不指定从 $lang->$moduleName->severityList 中获取。
  11. 'isIcon?: bool' // 是否显示为图标。
  12. );
  13. /**
  14. * @param mixed $child
  15. */
  16. protected function onAddChild($child)
  17. {
  18. if(is_string($child) || is_int($child))
  19. {
  20. if(!$this->props->has('level'))
  21. {
  22. $this->props->set('level', $child);
  23. return false;
  24. }
  25. if(!$this->props->has('text') && is_string($child))
  26. {
  27. $this->props->set('text', $child);
  28. return false;
  29. }
  30. }
  31. return $child;
  32. }
  33. protected function build()
  34. {
  35. $level = $this->prop('level', 0);
  36. $text = $this->prop('text');
  37. $isIcon = $this->prop('isIcon');
  38. if(is_null($text))
  39. {
  40. global $app, $lang;
  41. $moduleName = $app->getModuleName();
  42. if(isset($lang->$moduleName->severityList)) $text = $lang->$moduleName->severityList;
  43. }
  44. if(is_array($text)) $text = isset($text[$level]) ? $text[$level] : null;
  45. $level = trim("$level");
  46. if($text === null) $text = ($level === '0' || $level === '') ? '' : $level;
  47. if($isIcon === null) $isIcon = is_numeric($text);
  48. return span
  49. (
  50. set($this->getRestProps()),
  51. setClass($isIcon ? 'severity' : 'severity severity-label'),
  52. set('data-severity', $level),
  53. $isIcon ? '' : $text
  54. );
  55. }
  56. }