diff --git a/app/Http/Controllers/Api/ComponentController.php b/app/Http/Controllers/Api/ComponentController.php index 35c43a8ad13e..6c08d12ccb44 100644 --- a/app/Http/Controllers/Api/ComponentController.php +++ b/app/Http/Controllers/Api/ComponentController.php @@ -24,6 +24,20 @@ class ComponentController extends AbstractApiController { + /** + * Parse a comma-separated tags payload into a clean tag list. + * + * @param string $tags + * + * @return array + */ + protected function parseTags($tags) + { + return array_values(array_filter(array_map('trim', preg_split('/ ?, ?/', (string) $tags)), function ($tag) { + return $tag !== ''; + })); + } + /** * Get all components. * @@ -83,9 +97,9 @@ public function postComponents() throw new BadRequestHttpException(); } - if (Binput::has('tags')) { + if (!is_null(Binput::get('tags', null))) { // The component was added successfully, so now let's deal with the tags. - $tags = preg_split('/ ?, ?/', Binput::get('tags')); + $tags = $this->parseTags(Binput::get('tags')); // For every tag, do we need to create it? $componentTags = array_map(function ($taggable) use ($component) { @@ -124,8 +138,8 @@ public function putComponent(Component $component) throw new BadRequestHttpException(); } - if (Binput::has('tags')) { - $tags = preg_split('/ ?, ?/', Binput::get('tags')); + if (!is_null(Binput::get('tags', null))) { + $tags = $this->parseTags(Binput::get('tags')); // For every tag, do we need to create it? $componentTags = array_map(function ($taggable) use ($component) { diff --git a/app/Http/Controllers/Dashboard/ComponentController.php b/app/Http/Controllers/Dashboard/ComponentController.php index 38a021f453cd..3e1cab141ccc 100644 --- a/app/Http/Controllers/Dashboard/ComponentController.php +++ b/app/Http/Controllers/Dashboard/ComponentController.php @@ -35,6 +35,20 @@ class ComponentController extends Controller */ protected $subMenu = []; + /** + * Parse a comma-separated tags payload into a clean tag list. + * + * @param string $tags + * + * @return array + */ + protected function parseTags($tags) + { + return array_values(array_filter(array_map('trim', preg_split('/ ?, ?/', (string) $tags)), function ($tag) { + return $tag !== ''; + })); + } + /** * Creates a new component controller instance. * @@ -145,7 +159,7 @@ public function updateComponentAction(Component $component) } // The component was added successfully, so now let's deal with the tags. - $tags = preg_split('/ ?, ?/', $tags); + $tags = $this->parseTags($tags); // For every tag, do we need to create it? $componentTags = array_map(function ($taggable) use ($component) { @@ -198,7 +212,7 @@ public function createComponentAction() } // The component was added successfully, so now let's deal with the tags. - $tags = preg_split('/ ?, ?/', $tags); + $tags = $this->parseTags($tags); // For every tag, do we need to create it? $componentTags = array_map(function ($taggable) use ($component) {