Browse Source

Throws an exception if the index is out of bound in `insertAt()`

Yves P 9 years ago
parent
commit
f565b5b7b0
1 changed files with 6 additions and 1 deletions
  1. 6 1
      src/View/Helper/BreadcrumbsHelper.php

+ 6 - 1
src/View/Helper/BreadcrumbsHelper.php

@@ -114,7 +114,7 @@ class BreadcrumbsHelper extends Helper
      *
      * If the index already exists, the new crumb will be inserted,
      * and the existing element will be shifted one index greater.
-     * If the index is out of bounds, it will be added to the end.
+     * If the index is out of bounds, it will throw an exception.
      *
      * @param int $index The index to insert at.
      * @param string $title Title of the crumb.
@@ -126,9 +126,14 @@ class BreadcrumbsHelper extends Helper
      * the link)
      * - *templateVars*: Specific template vars in case you override the templates provided.
      * @return $this
+     * @throws LogicException In case the index is out of bound
      */
     public function insertAt($index, $title, $url = null, array $options = [])
     {
+        if (!isset($this->crumbs[$index])) {
+            throw new LogicException(sprintf("No crumb could be found at index '%s'", $index));
+        }
+
         array_splice($this->crumbs, $index, 0, [compact('title', 'url', 'options')]);
 
         return $this;