From 182234825aceca18a4d2a5f1adc97d92a7297908 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 5 Apr 2017 13:29:49 -0500 Subject: [PATCH 1/3] Added setIndentChars Added the ability to override the default indent characters without having to pass all params to listsFlattened. This allows for all default behavior listsFlattened with the added ability. Example Usage: Model::orderBy('parent_id')->get()->nest()->setIndentChars('- ')->listsFlattened('name'); --- src/NestableCollection.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/NestableCollection.php b/src/NestableCollection.php index 58b6b5c..88e1de1 100644 --- a/src/NestableCollection.php +++ b/src/NestableCollection.php @@ -18,6 +18,7 @@ class NestableCollection extends Collection private $total; private $parentColumn; private $removeItemsWithMissingAncestor = true; + private $indentChars = '    '; public function __construct($items = []) { @@ -87,9 +88,10 @@ public function nest() * * @return array */ - public function listsFlattened($column = 'title', BaseCollection $collection = null, $level = 0, array &$flattened = [], $indentChars = '    ') + public function listsFlattened($column = 'title', BaseCollection $collection = null, $level = 0, array &$flattened = [], $indentChars = null) { $collection = $collection ?: $this; + $indentChars = $indentChars ?: $this->indentChars; foreach ($collection as $item) { $flattened[$item->id] = str_repeat($indentChars, $level).$item->$column; if ($item->items) { @@ -100,6 +102,17 @@ public function listsFlattened($column = 'title', BaseCollection $collection = n return $flattened; } + /** + * Change the default indent characters when flattening lists + * + * @param string $indentChars + * @return $this + */ + public function setIndentChars(string $indentChars) { + $this->indentChars = $indentChars; + return $this; + } + /** * Force keeping items that have a missing ancestor. * From e9e2624e043560eb2eb978c1624db329babe6576 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 5 Apr 2017 13:35:03 -0500 Subject: [PATCH 2/3] Updated docs with setIndent() method --- README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.md b/README.md index 9e5c7c8..092afe9 100644 --- a/README.md +++ b/README.md @@ -59,6 +59,23 @@ By default it will look for a `title` column. You can send a custom column name Model::orderBy('parent_id')->get()->nest()->listsFlattened('name'); ``` +Four spaces are used to indent by default, to use your own use the `setIndent()` method, followed by the `listsFlattened()` method: + +```php +Model::orderBy('parent_id')->get()->nest()->setIndent('> ')->listsFlattened(); +``` + +Results: + +```php +[ + '22' => 'Item 1 Title', + '10' => '> Child 1 Title', + '17' => '> Child 2 Title', + '14' => 'Item 2 Title', +] +``` + ## Nesting a subtree This package remove items that have missing ancestor, this doesn’t allow you to nest a branch of a tree. From ee8a47c38e0f51c87b65c328ead458a85cc1ecd7 Mon Sep 17 00:00:00 2001 From: Tony Date: Wed, 5 Apr 2017 13:37:17 -0500 Subject: [PATCH 3/3] Changed setIndentChars() to the friendlier setIndent() --- src/NestableCollection.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NestableCollection.php b/src/NestableCollection.php index 88e1de1..c850200 100644 --- a/src/NestableCollection.php +++ b/src/NestableCollection.php @@ -108,7 +108,7 @@ public function listsFlattened($column = 'title', BaseCollection $collection = n * @param string $indentChars * @return $this */ - public function setIndentChars(string $indentChars) { + public function setIndent(string $indentChars) { $this->indentChars = $indentChars; return $this; }