From 349a1e998d20100f45dac9a26f6c2c5d963ea2ee Mon Sep 17 00:00:00 2001 From: Lito Date: Sat, 5 May 2018 14:11:07 +0200 Subject: [PATCH] Added unset option to remove previous values --- README.md | 8 ++++++++ src/Eusonlito/LaravelMeta/Meta.php | 24 ++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/README.md b/README.md index c6d5f97..e2bdaef 100644 --- a/README.md +++ b/README.md @@ -68,6 +68,9 @@ abstract class Controller extends BaseController # Default robots Meta::set('robots', 'index,follow'); + + # Default image + Meta::set('image', asset('images/logo.png')); } } ``` @@ -96,6 +99,11 @@ class HomeController extends Controller # Section description Meta::set('title', 'This is a detail page'); Meta::set('description', 'All about this detail page'); + + # Remove previous images + Meta::unset('image'); + + # Add only this last image Meta::set('image', asset('images/detail-logo.png')); return view('detail'); diff --git a/src/Eusonlito/LaravelMeta/Meta.php b/src/Eusonlito/LaravelMeta/Meta.php index 30f6b74..bbcdb86 100644 --- a/src/Eusonlito/LaravelMeta/Meta.php +++ b/src/Eusonlito/LaravelMeta/Meta.php @@ -117,6 +117,22 @@ public function set($key, $value) return $this->metas[$key] = self::cut($value, $key); } + /** + * @param string $key + * + * @return void + */ + public function unset($key) + { + $method = 'unset'.$key; + + if (method_exists($this, $method)) { + $this->$method(); + } else { + unset($this->metas[$key]); + } + } + /** * @param string $value * @@ -152,6 +168,14 @@ private function setImage($value) return $value; } + /** + * @return void + */ + private function unsetImage() + { + $this->metas['image'] = []; + } + /** * @param string $key * @param string|array $default