Node.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. class UI_Node {
  3. public $tagName, $props, $childrens;
  4. function __construct($tagName = null, $props = null, $childrens = null) {
  5. $this->tagName = $tagName;
  6. $this->props = $props;
  7. $this->childrens = $childrens;
  8. }
  9. function __toString() {
  10. return '<' . $this->tagName . $this->attributesToString() . '>' . $this->childrensToString() . '</' . $this->tagName . '>';
  11. }
  12. function toReactNode() { // @return array(3): [ tagName, attributes, childrens ] @see p5UI__buildDom
  13. return [
  14. (string)$this->tagName,
  15. !$this->props ? [] : $this->props,
  16. $this->childrensToReactNodes(),
  17. ];
  18. }
  19. // 'reactNode' => [ 'div', [ 'class' => "container AjaxFrmHorizontalEdit", 'style' => [ "max-width" => "940px" ] ], [
  20. // [ 'h4', [ 'style' => [ "padding-bottom" => "3px", "border-bottom" => "1px solid #ddd" ] ], [
  21. // "Edycja rekordu Nr {$record['ID']}",
  22. // [ 'small', [ 'class' => "pull-right valign-btns-bottom" ], [ $rowFunctionsOut ] ],
  23. // ] ],
  24. // [ 'P5UI__FeatureEditForm', [
  25. // 'class' => "", 'action' => "", 'method' => "post",
  26. // 'id' => "EDIT_FRM_{$this->_htmlID}", // TODO: rm - use React nodes // TODO: $this->_htmlID not exists!
  27. // 'ajaxSaveUrl' => "{$syncUrl}&_task=editSaveAjax", // TODO:? &_hash={$this->_htmlID}
  28. // 'namespace' => $acl->getNamespace(),
  29. // 'idRecord' => $record['ID'],
  30. // 'tableLabelHtml' => $tblLabel,
  31. // ], [
  32. // [ 'fieldset', [ 'style' => [ "padding-bottom" => "100px" ] ], $jsFields ] // fieldset
  33. // ] ] // form
  34. // ] ] // .container
  35. function attributesToString() {
  36. if (!$this->props) return '';
  37. $attrs = '';
  38. foreach ($this->props as $k => $v) {
  39. if (is_array($v)) {
  40. $attrs .= " {$k}=\"" . implode(" ", $v) . "\"";
  41. } else if (true === $v) { // eg. open => true : 'open'
  42. $attrs .= " {$k}";
  43. } else if (false === $v) { // skip if false value
  44. } else if (!empty($v)) {
  45. $attrs .= " {$k}=\"{$v}\"";
  46. }
  47. }
  48. return $attrs;
  49. }
  50. function scalarChildToString($child) {
  51. if (null === $child) return '';
  52. if (is_scalar($child)) {
  53. if (is_float($child) && !$child) return '0.0';
  54. // TODO: is price -> '0.00'
  55. // if (is_null($child)) return 'NULL'; // ??
  56. return (string)$child;
  57. }
  58. }
  59. function childrensToReactNodes() {
  60. if (null === $this->childrens) return '';
  61. if (is_scalar($this->childrens)) return $this->scalarChildToString($this->childrens);
  62. if (is_object($this->childrens) && $this->childrens instanceof UI_Node) return [ $this->childrens->toReactNode() ];
  63. if (!is_array($this->childrens)) throw new Exception("Unsupported children type");
  64. return array_map(
  65. function ($child) {
  66. if (null === $child) return null;
  67. if (is_scalar($child)) return $this->scalarChildToString($child);
  68. if (is_object($child)) return $child->toReactNode(); // TODO: is class UI_Node
  69. return (string)$child; // or throw new Exception('Not implemented UI_Node child type');
  70. },
  71. $this->childrens
  72. );
  73. }
  74. function childrensToString() {
  75. if (null === $this->childrens) return '';
  76. if (is_scalar($this->childrens)) return $this->scalarChildToString($this->childrens);
  77. if (is_object($this->childrens) && $this->childrens instanceof UI_Node) return (string)$this->childrens;
  78. // if (!is_array($this->childrens)) throw new Exception("Unsupported children type");
  79. if (!is_array($this->childrens)) trigger_error("Unsupported children type: " . var_export($this->childrens, true), E_USER_WARNING);
  80. if (!is_array($this->childrens)) return '';
  81. return array_reduce(
  82. $this->childrens,
  83. function ($ret, $child) {
  84. return "{$ret}{$child}"; // $child->__toString()
  85. },
  86. ""
  87. );
  88. }
  89. }