Overview

Namespaces

  • Menu
    • Items
      • Contents
    • Traits

Classes

  • Menu\Items\Contents\Link
  • Menu\Items\Contents\Raw
  • Menu\Items\Item
  • Menu\Items\ItemList
  • Menu\Menu
  • Menu\MenuHandler
  • Menu\MenuServiceProvider
  • Menu\Traits\Content
  • Menu\Traits\MenuObject
  • Overview
  • Namespace
  • Class
  1: <?php
  2: namespace Menu;
  3: 
  4: use Menu\Items\ItemList;
  5: use Illuminate\Support\Str;
  6: use Exception;
  7: 
  8: /**
  9:  * Handles various instances of ItemList at once
 10:  */
 11: class MenuHandler
 12: {
 13: 
 14:   /**
 15:    * The ItemList or Item instances this handler acts on
 16:    *
 17:    * @var array
 18:    */
 19:   protected $menuObjects = array();
 20: 
 21:   public static $override = array(
 22:     'add',
 23:     'set',
 24:     'wrap'
 25:   );
 26: 
 27:   public static $responses = array(
 28:     'getHandlerFromResults' => array(
 29:       'getAllItems',
 30:       'getItemsAtDepth',
 31:       'getItemsAtDepthRange',
 32:       'getItemsByContentType',
 33:       'getAllItemLists',
 34:       'getSubmenu',
 35:       'getItemListsAtDepth',
 36:       'getItemListsAtDepthRange',
 37:       'onItem',
 38:       'getContent',
 39:       'stop',
 40:       'filter'
 41:     ),
 42:     'getMatchFromResults' => array(
 43:       'findItemListByName',
 44:       'findActiveItem',
 45:       'findByName',
 46:       'findItemByUrl',
 47:       'find'
 48:     ),
 49:     'getCombinedResults' => array(
 50:       'lists'
 51:     ),
 52:     'getCombindedResultsByKey' => array(
 53:       'getItemsWithDepth',
 54:       'getItemListsWithDepth'
 55:     ),
 56:     'getImplodedResults' => array(
 57:       'render'
 58:     )
 59:   );
 60: 
 61:   /**
 62:    * Set the menuobjects on which this menu should act
 63:    *
 64:    * @param array $menuObjects The menuobjects
 65:    *
 66:    * @return void
 67:    */
 68:   public function __construct($menuObjects = array())
 69:   {
 70:     $this->menuObjects = $menuObjects;
 71:   }
 72: 
 73:   ////////////////////////////////////////////////////////////////////
 74:   ////////////////////////// PUBLIC INTERFACE ////////////////////////
 75:   ////////////////////////////////////////////////////////////////////
 76: 
 77:   public function setMenuObjects($menuObjects)
 78:   {
 79:     $this->menuObjects = $menuObjects;
 80: 
 81:     return $this;
 82:   }
 83: 
 84:   public function getMenuObjects()
 85:   {
 86:     return $this->menuObjects;
 87:   }
 88: 
 89:   public function addMenuObject($menuObject)
 90:   {
 91:     $this->menuObjects[] = $menuObject;
 92: 
 93:     return $this;
 94:   }
 95: 
 96:   public function map($callback)
 97:   {
 98:     array_map($callback, $this->getMenuObjects());
 99:   }
100: 
101:   public function breadcrumbs($choosePath = null)
102:   {
103:     if(is_null($choosePath)) {
104:       $choosePath = function($itemLists) {
105:         return $itemLists[0];
106:       };
107:     }
108: 
109:     $menuObjects = array();
110:     foreach (Menu::allHandlers()->getMenuObjects() as $itemList) {
111:       $breadcrumbs = $itemList->breadcrumbs();
112:       if($breadcrumbs->hasChildren()) {
113:         $menuObjects[] = $breadcrumbs;
114:       }
115:     }
116: 
117:     if(count($menuObjects) > 1)
118:     {
119:       return $choosePath($menuObjects);
120:     }
121: 
122:     return isset($menuObjects[0]) ? $menuObjects[0] : new MenuHandler;
123:   }
124: 
125:   ////////////////////////////////////////////////////////////////////
126:   //////////////////////////// RESPONDERS ////////////////////////////
127:   ////////////////////////////////////////////////////////////////////
128: 
129:   protected function getHandlerFromResults($menuHandlers)
130:   {
131:     if(is_array($menuHandlers) && count($menuHandlers) > 0 && ! $menuHandlers[0] instanceof MenuHandler) {
132:       foreach ($menuHandlers as &$menuHandler) {
133:         $menuHandler = new MenuHandler(array($menuHandler));
134:       }
135:     }
136: 
137:     $menuObjects = $this->getMenuObjectsFromHandlers($menuHandlers);
138: 
139:     return new MenuHandler($menuObjects);
140:   }
141: 
142:   protected function getMatchFromResults($results)
143:   {
144:     foreach($results as $result)
145:     {
146:       if($result !== false)
147:       {
148:         return $result;
149:       }
150:     }
151: 
152:     return false;
153:   }
154: 
155:   protected function getCombinedResults($results)
156:   {
157:     return call_user_func_array('array_merge', $results);
158:   }
159: 
160:   protected function getCombindedResultsByKey($results)
161:   {
162:     $combinedResults = array();
163:     foreach ($results as $result)
164:     {
165:       foreach($result as $key => $items)
166:       {
167:         foreach($items as $item)
168:         {
169:           $combinedResults[$key][] = $item;
170:         }
171:       }
172:     }
173: 
174:     return $combinedResults;
175:   }
176: 
177:   protected function getImplodedResults($results)
178:   {
179:     return implode('', $results);
180:   }
181: 
182:   ////////////////////////////////////////////////////////////////////
183:   ///////////////////////// RESULT EXTRACTORS ////////////////////////
184:   ////////////////////////////////////////////////////////////////////
185: 
186:   protected function getMenuObjectsFromHandlers($menuHandlers)
187:   {
188:     $results = array();
189:     foreach($menuHandlers as $menuHandler)
190:     {
191:       foreach($menuHandler->getMenuObjects() as $item)
192:       {
193:         $results[] = $item;
194:       }
195:     }
196: 
197:     return $results;
198:   }
199: 
200:   ////////////////////////////////////////////////////////////////////
201:   /////////////////////////// MAGIC METHODS //////////////////////////
202:   ////////////////////////////////////////////////////////////////////
203: 
204:   /**
205:    * Magic method that will pass the incoming calls to
206:    * all of the ItemLists this handler acts on
207:    *
208:    * @param string  $method
209:    * @param array   $parameters
210:    *
211:    * @return MenuHandler
212:    */
213:   public function __call($method, $parameters = array())
214:   {
215:     $results = array();
216:     foreach ($this->menuObjects as &$menuObject) {
217:       $result = call_user_func_array(array($menuObject, $method), $parameters);
218: 
219:       if (Str::startsWith($method, static::$override)) {
220:         $menuObject = $result;
221:       }
222: 
223:       $results[] = $result;
224:     }
225: 
226:     foreach (static::$responses as $responseMethod => $methods) {
227:       if(in_array($method, $methods)) {
228:         return $this->$responseMethod($results);
229:       }
230:     }
231: 
232:     return $this;
233:   }
234: 
235:   /**
236:    * Render the MenuHandler
237:    *
238:    * @return string
239:    */
240:   public function __toString()
241:   {
242:     return $this->render();
243:   }
244: 
245: }
246: 
API documentation generated by ApiGen