From 15d77080ab53b1114fa84eef9f2332952ddfead5 Mon Sep 17 00:00:00 2001 From: Adrien Leloup Date: Thu, 29 Oct 2020 19:04:04 +0100 Subject: [PATCH] Added ability to add modifiers programmatically --- src/BemComponent.php | 43 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/BemComponent.php b/src/BemComponent.php index fe4e3ff..e08ecfb 100644 --- a/src/BemComponent.php +++ b/src/BemComponent.php @@ -63,12 +63,16 @@ protected function processClasses($classes) */ protected function getModifiers() { - if(is_null($this->modifiers)) { + if(! is_null($this->attributes)) { $modifiers = $this->attributes->get('modifiers'); - $this->modifiers = $this->processModifiers($modifiers); + + $this->modifiers = array_merge( + $this->processModifiers($modifiers), + $this->modifiers ?? [] + ); } - return $this->modifiers; + return $this->modifiers ?? []; } /** @@ -104,4 +108,37 @@ public function hasClass($classname) return in_array($classname, $this->getClasses()); } + /** + * Set a modifier programmatically + * + * @param string $modifier + * @return void + */ + public function modifier(string $modifier) + { + if (is_null($this->modifiers)) { + $this->modifiers = []; + } + + $this->modifiers[] = $modifier; + } + + /** + * Set multiple modifiers programmatically + * + * @param array $modifiers + * @param boolean $overwrite + * @return void + */ + public function modifiers(array $modifiers, $overwrite = false) + { + if ($overwrite) { + $this->modifiers = $modifiers; + + return; + } + + $this->modifiers = array_merge($this->getModifiers(), $modifiers); + } + }