Skip to content

Commit

Permalink
Merge pull request #2 from Icinga/code-quality-and-conventions
Browse files Browse the repository at this point in the history
Code quality and conventions
  • Loading branch information
lippserd authored Mar 12, 2020
2 parents 34058c6 + e4f4699 commit 8bed1a8
Show file tree
Hide file tree
Showing 18 changed files with 88 additions and 89 deletions.
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
# Exclude all hidden files
.*

# Except those related to Git and GitHub
# Except those related to Git (and GitHub)
!.git*
!.github*
!.phpcs.xml

# Exclude files from composer install
vendor/
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Icinga PHP Library - Web Components

[![PHP Support](https://img.shields.io/badge/php-%3E%3D%205.6-777BB4?logo=PHP)](https://php.net/)
![Build Status](https://github.com/Icinga/ipl-web/workflows/PHP%20Tests/badge.svg?branch=master)

`ipl/validator` provides common web components.

## Installation

The recommended way to install this library is via [Composer](https://getcomposer.org):

```
composer require ipl/web
```
19 changes: 4 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "Icinga PHP Library - Web Components",
"keywords": ["html"],
"homepage": "https://github.com/Icinga/ipl-web",
"license": "MIT",
"autoload": {
"psr-4": {
"ipl\\Web\\": "src"
Expand All @@ -14,21 +15,9 @@
"ipl\\Tests\\Web\\": "tests"
}
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/Icinga/ipl-stdlib",
"no-api": true
},
{
"type": "vcs",
"url": "https://github.com/Icinga/ipl-html",
"no-api": true
}
],
"require": {
"php": ">=5.4.0",
"ipl/html": ">=0.2.2",
"ipl/stdlib": ">=0.3.0"
"php": ">=5.6.0",
"ipl/html": ">=0.3.0",
"ipl/stdlib": ">=0.5.0"
}
}
File renamed without changes.
29 changes: 13 additions & 16 deletions src/Compat/CompatController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@

class CompatController extends Controller
{
/** @var HtmlDocument */
protected $document;
/** @var Content */
protected $content;

/** @var Controls */
protected $controls;

/** @var Content */
protected $content;
/** @var HtmlDocument */
protected $document;

/** @var Footer */
protected $footer;
Expand Down Expand Up @@ -76,29 +76,29 @@ public function getTabs()
}

/**
* Add a control
* Add content
*
* @param ValidHtml $control
* @param ValidHtml $content
*
* @return $this
*/
protected function addControl(ValidHtml $control)
protected function addContent(ValidHtml $content)
{
$this->controls->add($control);
$this->content->add($content);

return $this;
}

/**
* Add content
* Add a control
*
* @param ValidHtml $content
* @param ValidHtml $control
*
* @return $this
*/
protected function addContent(ValidHtml $content)
protected function addControl(ValidHtml $control)
{
$this->content->add($content);
$this->controls->add($control);

return $this;
}
Expand Down Expand Up @@ -127,11 +127,8 @@ protected function addFooter(ValidHtml $footer)
*
* @throws InvalidArgumentException
*/
protected function setTitle($title)
protected function setTitle($title, ...$args)
{
$args = func_get_args();
array_shift($args);

if (! empty($args)) {
$title = vsprintf($title, $args);
}
Expand Down
23 changes: 12 additions & 11 deletions src/Compat/CompatDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,31 @@

use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\FormDecorator\DecoratorInterface;
use ipl\Html\FormElement\BaseFormElement;
use ipl\Html\FormElement\SubmitElement;
use ipl\Html\Contract\FormElement;
use ipl\Html\Contract\FormElementDecorator;
use ipl\Html\Contract\FormSubmitElement;
use ipl\Html\Html;

/**
* Compat form element decorator based on div elements
*/
class CompatDecorator extends BaseHtmlElement implements DecoratorInterface
class CompatDecorator extends BaseHtmlElement implements FormElementDecorator
{
/** @var BaseFormElement The decorated form element */
/** @var FormElement The decorated form element */
protected $formElement;

/** @var bool Whether the form element has been added already */
protected $formElementAdded = false;

protected $tag = 'div';

public function decorate(BaseFormElement $formElement)
public function decorate(FormElement $formElement)
{
$decorator = clone $this;

$decorator->formElement = $formElement;

// TODO(el): Replace with SubmitElementInterface once introduced
if ($formElement instanceof SubmitElement) {
if ($formElement instanceof FormSubmitElement) {
$class = 'control-group form-controls';

$formElement->getAttributes()->add(['class' => 'btn-primary']);
Expand Down Expand Up @@ -80,8 +79,10 @@ protected function assembleLabel()
if ($label !== null) {
$attributes = null;

if ($this->formElement->getAttributes()->has('id')) {
$attributes = new Attributes(['for' => $this->formElement->getAttributes()->get('id')]);
$elementAttributes = $this->formElement->getAttributes();

if (isset($elementAttributes['id'])) {
$attributes = new Attributes(['for' => $elementAttributes['id']]);
}

return Html::tag('div', ['class' => 'control-label-group'], Html::tag('label', $attributes, $label));
Expand All @@ -108,7 +109,7 @@ public function add($content)

protected function assemble()
{
if ($this->formElement->hasBeenValidatedAndIsNotValid()) {
if ($this->formElement->hasBeenValidated() && ! $this->formElement->isValid()) {
$this->getAttributes()->add('class', 'has-error');
}

Expand Down
8 changes: 5 additions & 3 deletions src/Compat/CompatForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ class CompatForm extends Form

public function hasDefaultElementDecorator()
{
if ($this->defaultElementDecorator === null) {
$this->defaultElementDecorator = new CompatDecorator();
if (parent::hasDefaultElementDecorator()) {
return true;
}

return parent::hasDefaultElementDecorator();
$this->setDefaultElementDecorator(new CompatDecorator());

return true;
}
}
10 changes: 5 additions & 5 deletions src/Compat/ViewRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ public static function inject()
Zf1HelperBroker::addHelper($inject);
}

public function getName()
{
return 'ViewRenderer';
}

/**
* Render the view w/o using a view script
*
Expand All @@ -52,9 +57,4 @@ public function render($action = null, $name = null, $noController = null)

$this->setNoRender();
}

public function getName()
{
return 'ViewRenderer';
}
}
9 changes: 4 additions & 5 deletions src/Control/LimitControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class LimitControl extends CompatForm
'500' => '500'
];

/** @var Url */
protected $url;

/** @var string Name of the URL parameter which stores the limit */
protected $limitParam = 'limit';

/** @var Url */
protected $url;

protected $method = 'GET';

public function __construct(Url $url)
Expand Down Expand Up @@ -70,8 +70,7 @@ public function getLimit()

protected function assemble()
{
// TODO(el): Remove 'method' => 'GET' once ipl-html supports this out of the box
$this->addAttributes(['class' => 'limit-control inline', 'method' => 'GET']);
$this->addAttributes(['class' => 'limit-control inline']);

$limit = $this->getLimit();
$limits = static::$limits;
Expand Down
20 changes: 10 additions & 10 deletions src/Control/PaginationControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use ipl\Html\Attributes;
use ipl\Html\BaseHtmlElement;
use ipl\Html\Html;
use ipl\Stdlib\Contract\PaginationInterface;
use ipl\Stdlib\Contract\Paginatable;
use ipl\Web\Url;
use ipl\Web\Widget\Icon;

Expand All @@ -17,12 +17,6 @@
*/
class PaginationControl extends BaseHtmlElement
{
/** @var PaginationInterface The pagination adapter which handles the underlying data source */
protected $paginatable;

/** @var Url The URL to base off pagination URLs */
protected $url;

/** @var int Default maximum number of items which should be shown per page */
protected $defaultPageSize = 25;

Expand All @@ -35,6 +29,12 @@ class PaginationControl extends BaseHtmlElement
/** @var string */
protected $pageSpacer = '';

/** @var Paginatable The pagination adapter which handles the underlying data source */
protected $paginatable;

/** @var Url The URL to base off pagination URLs */
protected $url;

/** @var int Cache for the total number of items */
private $totalCount;

Expand All @@ -48,10 +48,10 @@ class PaginationControl extends BaseHtmlElement
/**
* Create a pagination control
*
* @param PaginationInterface $paginatable The paginatable
* @param Url $url The URL to base off paging URLs
* @param Paginatable $paginatable The paginatable
* @param Url $url The URL to base off paging URLs
*/
public function __construct(PaginationInterface $paginatable, Url $url)
public function __construct(Paginatable $paginatable, Url $url)
{
$this->paginatable = $paginatable;
$this->url = $url;
Expand Down
9 changes: 4 additions & 5 deletions src/Control/SortControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
*/
class SortControl extends CompatForm
{
/** @var Url Request URL */
protected $url;

/** @var string Name of the URL parameter which stores the sort column */
protected $sortParam = 'sort';

/** @var Url Request URL */
protected $url;

/** @var array Possible sort columns as sort string-value pairs */
private $columns;

Expand Down Expand Up @@ -140,8 +140,7 @@ public function getSort()

protected function assemble()
{
// TODO(el): Remove 'method' => 'GET' once ipl-html supports this out of the box
$this->addAttributes(['class' => 'sort-control inline', 'method' => 'GET']);
$this->addAttributes(['class' => 'sort-control inline']);

$columns = $this->getColumns();
$sort = $this->getSort();
Expand Down
4 changes: 2 additions & 2 deletions src/Layout/Content.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/
class Content extends BaseHtmlElement
{
protected $tag = 'div';

protected $contentSeparator = "\n";

protected $defaultAttributes = ['class' => 'content'];

protected $tag = 'div';
}
6 changes: 3 additions & 3 deletions src/Layout/Controls.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
*/
class Controls extends BaseHtmlElement
{
protected $tag = 'div';
/** @var Tabs */
protected $tabs;

protected $contentSeparator = "\n";

protected $defaultAttributes = ['class' => 'controls'];

/** @var Tabs */
protected $tabs;
protected $tag = 'div';

/**
* Get the tabs
Expand Down
4 changes: 2 additions & 2 deletions src/Layout/Footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
*/
class Footer extends BaseHtmlElement
{
protected $tag = 'div';

protected $contentSeparator = "\n";

protected $defaultAttributes = ['class' => 'footer'];

protected $tag = 'div';
}
4 changes: 2 additions & 2 deletions src/Widget/ActionBar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ class ActionBar extends BaseHtmlElement

protected $contentSeparator = ' ';

protected $tag = 'div';

protected $defaultAttributes = [
'class' => 'action-bar',
'data-base-target' => '_self'
];

protected $tag = 'div';

/**
* Create a action bar
*
Expand Down
Loading

0 comments on commit 8bed1a8

Please sign in to comment.