FRED™  3.0
FRED™: Framework for Rapid and Easy Development
Builder.php
Go to the documentation of this file.
1 <?php
2 
4 
5 class Builder extends \Rsi\Thing{
6 
7  const RENDER_CAPTION = '~'; //!< ID prefix indicating to render only the caption.
8  const RENDER_WIDGET = '$'; //!< ID prefix indicating to render only the widget.
9  const RENDER_BOTH = '%'; //!< ID prefix indicating to render both caption and widget (explicitly).
10  const RENDER_WILD = '*'; //!< Render all (between previous and next id).
11 
12  protected $_view;
13 
14  public function __construct($view){
15  $this->_view = $view;
16  }
17  /**
18  * Add a row.
19  * @param array $ids Current set of widget ID's.
20  * @param array $row Widget ID's for the row.
21  * @param string $index Position to insert the row (before; empty to add on end).
22  * @return array Widget ID's with the row added (duplicate ID's are removed from the original set).
23  */
24  public static function row($ids,$row,$index = null){
25  $result = [];
26  foreach($ids as $id){
27  if($id == $index){
28  $result[] = $row;
29  $index = false;
30  }
31  if(is_array($id) || !in_array($id,$row)) $result[] = $id;
32  }
33  if($index !== false) $result[] = $row;
34  return $result;
35  }
36 
37  protected function trim($id){
38  return ltrim($id,self::RENDER_CAPTION . self::RENDER_WIDGET . self::RENDER_BOTH);
39  }
40 
41  protected function next($ids){
42  while($id = array_shift($ids)){
43  if(is_array($id)) $id = $this->next($id);
44  if($id && ($id != self::RENDER_WILD)) return $id;
45  }
46  return null;
47  }
48 
49  protected function expand($ids,&$done = []){
50  $result = [];
51  while($id = array_shift($ids))
52  if(is_array($id)) $result[] = $this->expand($id,$done);
53  elseif($id == self::RENDER_WILD){
54  $todo = array_diff($this->_view->notRenderedWidgetIds(),$done);
55  $next = $this->trim($this->next($ids));
56  while(($id = array_shift($todo)) && ($id != $next)) $done[] = $this->trim($result[] = $id);
57  }
58  else $done[] = $this->trim($result[] = $id);
59  return $result;
60  }
61 
62  protected function renderCaption($id){
63  return substr($id,0,1) == self::RENDER_WIDGET ? null : $this->_view->widget($this->trim($id))->renderCaption();
64  }
65 
66  protected function renderWidget($id){
67  return substr($id,0,1) == self::RENDER_CAPTION ? null : $this->_view->renderWidget($this->trim($id));
68  }
69 
70  public function build($ids){
71  $content = '';
72  foreach($this->expand($ids) as $id) $content .= is_array($id)
73  ? $this->html->div($this->build($id),null,'fred-sub ids-' . implode($id))
74  : $this->html->div($this->renderCaption($id) . $this->renderWidget($id),null,'fred-row id-' . $id);
75  return $content;
76  }
77 
78  protected function getHtml(){
79  return $this->_view->component('html');
80  }
81 
82 }
const RENDER_CAPTION
ID prefix indicating to render only the caption.
Definition: Builder.php:7
Basic object.
Definition: Thing.php:13
const RENDER_BOTH
ID prefix indicating to render both caption and widget (explicitly).
Definition: Builder.php:9
const RENDER_WIDGET
ID prefix indicating to render only the widget.
Definition: Builder.php:8
const RENDER_WILD
Render all (between previous and next id).
Definition: Builder.php:10
static row($ids, $row, $index=null)
Add a row.
Definition: Builder.php:24