| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace PhpSchool\CliMenu\Builder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpSchool\CliMenu\CliMenu; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PhpSchool\CliMenu\MenuItem\CheckboxItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use PhpSchool\CliMenu\MenuItem\LineBreakItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use PhpSchool\CliMenu\MenuItem\MenuItemInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use PhpSchool\CliMenu\MenuItem\MenuMenuItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use PhpSchool\CliMenu\MenuItem\RadioItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use PhpSchool\CliMenu\MenuItem\SelectableItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use PhpSchool\CliMenu\MenuItem\SplitItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use PhpSchool\CliMenu\MenuItem\StaticItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use PhpSchool\CliMenu\Style\ItemStyle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use function \PhpSchool\CliMenu\Util\each; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @author Aydin Hassan <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | class SplitItemBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var CliMenu | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $menu; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var SplitItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $splitItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * Whether or not to auto create keyboard shortcuts for items | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * when they contain square brackets. Eg: [M]y item | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     private $autoShortcuts = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Regex to auto match for shortcuts defaults to looking | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * for a single character encased in square brackets | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     private $autoShortcutsRegex = '/\[(.)\]/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     private $extraItemStyles = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     public function __construct(CliMenu $menu) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $this->menu = $menu; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         $this->splitItem = new SplitItem(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function addItem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         string $text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         callable $itemCallable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         bool $showItemExtra = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         bool $disabled = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     ) : self { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $this->splitItem->addItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     public function addCheckboxItem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         string $text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |         callable $itemCallable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         bool $showItemExtra = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         bool $disabled = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     ) : self { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         $this->splitItem->addItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     public function addRadioItem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         string $text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         callable $itemCallable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         bool $showItemExtra = false, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         bool $disabled = false | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     ) : self { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->splitItem->addItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 91 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |     public function addStaticItem(string $text) : self | 
            
                                                                        
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 94 |  |  |         $this->splitItem->addItem(new StaticItem($text)); | 
            
                                                                        
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 96 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |     public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->splitItem->addItem(new LineBreakItem($breakChar, $lines)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function addSubMenu(string $text, \Closure $callback) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $builder = CliMenuBuilder::newSubMenu($this->menu->getTerminal()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         if ($this->autoShortcuts) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $builder->enableAutoShortcuts($this->autoShortcutsRegex); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         each($this->extraItemStyles, function (int $i, array $extraItemStyle) use ($builder) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $builder->registerItemStyle($extraItemStyle['class'], $extraItemStyle['style']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         $callback($builder); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $menu = $builder->build(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $menu->setParent($this->menu); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         $this->splitItem->addItem(new MenuMenuItem( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $text, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             $menu, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             $builder->isMenuDisabled() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     public function addMenuItem(MenuItemInterface $item) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         $this->splitItem->addItem($item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     public function setGutter(int $gutter) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $this->splitItem->setGutter($gutter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     public function enableAutoShortcuts(string $regex = null) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         $this->autoShortcuts = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         if (null !== $regex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             $this->autoShortcutsRegex = $regex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     public function registerItemStyle(string $itemClass, ItemStyle $itemStyle) : self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |         $this->extraItemStyles[] = ['class' => $itemClass, 'style' => $itemStyle]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     public function build() : SplitItem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         return $this->splitItem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 168 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 169 |  |  |  |