From 983e55df1ba93a516dac84c779b052cc17dc714a Mon Sep 17 00:00:00 2001 From: Adam Kadlec Date: Sun, 14 Jan 2018 19:37:28 +0100 Subject: [PATCH] - Refactoring --- src/IPub/Widgets/Components/Control.php | 29 ++++++++++++++++---- src/IPub/Widgets/Managers/WidgetsManager.php | 1 + src/IPub/Widgets/Widgets/Widget.php | 14 ++++------ 3 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/IPub/Widgets/Components/Control.php b/src/IPub/Widgets/Components/Control.php index 670ed1b..cc193d2 100644 --- a/src/IPub/Widgets/Components/Control.php +++ b/src/IPub/Widgets/Components/Control.php @@ -28,7 +28,6 @@ use IPub\Widgets\Exceptions; use IPub\Widgets\Managers; use IPub\Widgets\Widgets; -use Tracy\Debugger; /** * Widgets container control definition @@ -140,6 +139,14 @@ public function render() // Check if decorator name is provided if (is_string($arg)) { $this->setDecorator($arg); + + } elseif (is_array($arg)) { + if (array_key_exists('group', $arg)) { + $this->setGroup($arg['group']); + + } elseif (array_key_exists('decorator', $arg)) { + $this->setDecorator($arg['decorator']); + } } } @@ -246,15 +253,17 @@ public function getWidgets() /** * Add widget to container * - * @param string $name + * @param string|Widgets\IFactory $name * @param array $data * @param string|NULL $group * @param string|NULL $position * + * @return Widgets\IWidget + * * @throws Exceptions\WidgetNotRegisteredException * @throws Exceptions\InvalidStateException */ - public function addWidget(string $name, array $data = [], string $group = NULL, string $position = NULL) + public function addWidget($name, array $data = [], string $group = NULL, string $position = NULL) : Widgets\IWidget { if ($position === NULL) { $position = $this->position; @@ -267,8 +276,16 @@ public function addWidget(string $name, array $data = [], string $group = NULL, // Prepare widget settings data $data = $this->createData($data); - if (!$factory = $this->widgetsManager->get($name, $group)) { - throw new Exceptions\WidgetNotRegisteredException(sprintf('Widget of type "%s" in group "%s" is not registered.', $name, $group)); + if (is_string($name)) { + if (!$factory = $this->widgetsManager->get($name, $group)) { + throw new Exceptions\WidgetNotRegisteredException(sprintf('Widget of type "%s" in group "%s" is not registered.', $name, $group)); + } + + } elseif (!$name instanceof Widgets\IFactory) { + throw new Exceptions\InvalidArgumentException(sprintf('Provided service is not valid widgets factory service. Instance of IPub\Widgets\Widgets\IFactory expected, instance of %s provided', get_class($name))); + + } else { + $factory = $name; } // Check container exist @@ -292,6 +309,8 @@ public function addWidget(string $name, array $data = [], string $group = NULL, // Add widget component to container/position $positionContainer->addComponent($widget, ($widget->getName() . spl_object_hash($data))); + + return $widget; } /** diff --git a/src/IPub/Widgets/Managers/WidgetsManager.php b/src/IPub/Widgets/Managers/WidgetsManager.php index 9f9984b..b7cdb43 100644 --- a/src/IPub/Widgets/Managers/WidgetsManager.php +++ b/src/IPub/Widgets/Managers/WidgetsManager.php @@ -20,6 +20,7 @@ use IPub; use IPub\Widgets\Widgets; +use Tracy\Debugger; /** * Registered widgets manager diff --git a/src/IPub/Widgets/Widgets/Widget.php b/src/IPub/Widgets/Widgets/Widget.php index ea7e062..acbbaf1 100644 --- a/src/IPub/Widgets/Widgets/Widget.php +++ b/src/IPub/Widgets/Widgets/Widget.php @@ -119,13 +119,13 @@ protected function beforeRender() // If widget name has space... $pos = mb_strpos($name, ' '); if ($pos !== FALSE && mb_strpos($name, '||') === FALSE) { - $name = Utils\Html::el('span') + $title = Utils\Html::el('span') ->addAttributes(['class' => 'color']) ->setText(mb_substr($name, 0, $pos)) ->render(); // Modify widget name - $name = $name . mb_substr($name, $pos); + $name = $title . mb_substr($name, $pos); } // If widget name has subtitle... @@ -147,16 +147,14 @@ protected function beforeRender() // Set badge if exists if ($badge = $this->data->getBadge()) { - $badge = Utils\Html::el('span') - ->addAttributes(['class' => 'badge badge-' . $badge]) - ->render(); + $badge = Utils\Html::el('i') + ->addAttributes(['class' => 'badge badge-' . $badge]); } // Set icon if exists if ($icon = $this->data->getIcon()) { - $icon = Utils\Html::el('span') - ->addAttributes(['class' => 'icon icon-' . $icon]) - ->render(); + $icon = Utils\Html::el('i') + ->addAttributes(['class' => 'ipub-icon ipub-icon-' . $icon]); } // Assign basic widget data to template