Skip to content
This repository has been archived by the owner on Feb 17, 2020. It is now read-only.

Commit

Permalink
- Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
akadlec committed Jan 14, 2018
1 parent e526447 commit 983e55d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 13 deletions.
29 changes: 24 additions & 5 deletions src/IPub/Widgets/Components/Control.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
use IPub\Widgets\Exceptions;
use IPub\Widgets\Managers;
use IPub\Widgets\Widgets;
use Tracy\Debugger;

/**
* Widgets container control definition
Expand Down Expand Up @@ -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']);
}
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/IPub/Widgets/Managers/WidgetsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

use IPub;
use IPub\Widgets\Widgets;
use Tracy\Debugger;

/**
* Registered widgets manager
Expand Down
14 changes: 6 additions & 8 deletions src/IPub/Widgets/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand All @@ -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
Expand Down

0 comments on commit 983e55d

Please sign in to comment.