v1.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace zin;
  3. class content extends wg
  4. {
  5. /**
  6. * @var mixed[]
  7. */
  8. protected static $defineProps = array
  9. (
  10. 'control?: string|array', // 内容类型,值可以为:html, text 以及其他部件的类型,也可以指定为包含 `control` 键值的控件属性数组。
  11. 'content?: string|array', // 内容数据。
  12. 'render?: callable|Closure' // 自定义构建函数。
  13. );
  14. /**
  15. * @var mixed[]
  16. */
  17. protected static $controlMap = array
  18. (
  19. 'list' => 'simpleList',
  20. 'status' => 'statusLabel',
  21. 'pri' => 'priLabel',
  22. 'severity' => 'severityLabel',
  23. );
  24. protected function buildText()
  25. {
  26. return div
  27. (
  28. setClass('text'),
  29. set($this->getRestProps()),
  30. $this->prop('text'),
  31. $this->prop('content'),
  32. $this->children()
  33. );
  34. }
  35. protected function buildLink()
  36. {
  37. return a
  38. (
  39. set::href($this->prop('url')),
  40. set($this->getRestProps()),
  41. $this->prop('text'),
  42. $this->children()
  43. );
  44. }
  45. protected function buildHtml()
  46. {
  47. return div
  48. (
  49. setClass('article'),
  50. set($this->getRestProps()),
  51. html($this->prop('content')),
  52. $this->children()
  53. );
  54. }
  55. protected function buildDivider()
  56. {
  57. return hr(setClass('divider'));
  58. }
  59. protected function build()
  60. {
  61. $render = $this->prop('render');
  62. if($render instanceof \Closure) return $render($this->props->skip('render'), $this->children());
  63. elseif(is_callable($render)) return call_user_func($render, $this->props->skip('render'), $this->children());
  64. $control = $this->prop('control');
  65. if($control)
  66. {
  67. $controlProps = array();
  68. $controlName = '';
  69. if(is_string($control))
  70. {
  71. $controlName = $control;
  72. }
  73. elseif(is_array($control))
  74. {
  75. $controlName = $control['control'];
  76. unset($control['control']);
  77. $controlProps = $control;
  78. }
  79. $methodName = "build{$controlName}";
  80. if(method_exists($this, $methodName)) return $this->$methodName();
  81. if(isset(static::$controlMap[$controlName])) $controlName = static::$controlMap[$controlName];
  82. $wgName = "\\zin\\$controlName";
  83. if(class_exists($wgName)) return new $wgName(set($this->props->skip('control')), $controlProps ? set($controlProps) : null, $this->prop('children'), $this->children());
  84. return createWg($controlName, array(set($this->props->skip('control')), $controlProps ? set($controlProps) : null, $this->prop('children'), $this->children()), 'div');
  85. }
  86. if($this->hasProp('children')) return $this->prop('children');
  87. return parent::build();
  88. }
  89. }