Skip to content

Commit

Permalink
Correção e inclusão do elemento Lists (listas)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanifce committed Aug 31, 2018
1 parent 9868d1e commit 37fba0d
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 35 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
},

"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": "*",
"squizlabs/php_codesniffer": "^3.3"
},

"autoload": {
Expand All @@ -31,4 +32,4 @@
"Tests\\": "tests"
}
}
}
}
14 changes: 9 additions & 5 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@
use HTMLBuilder\Page;
use HTMLBuilder\ElementFactory;
use HTMLBuilder\Elements\Table;
use HTMLBuilder\Elements\Paragraph;
use HTMLBuilder\Elements\Paragraph as P;
use HTMLBuilder\Elements\Lists;

$page = new Page('Minha Página!');
$page->add_in_head(ElementFactory::make('base')->attr('href', ['http://html.dev/htmlBuilder']));
$page->add_in_head(ElementFactory::make('base')->attr('href', ['http://htmlbuilder.local/']));
$page->add_in_head(ElementFactory::make('meta')->attr('charset', ['utf-8']));
$page->add_in_body(ElementFactory::make('h1')->value('Minha Página!'));
$page->add_in_body(ElementFactory::make('hr'));
$page->add_in_body(ElementFactory::make('p')->value('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum'));
$page->add_in_body(new P('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum'));

#########################
# Usando a classe Table #
Expand All @@ -44,5 +45,8 @@

$page->render();

$paragraph = new Paragraph('Esse é meu paragrafo!');
$paragraph->render();
$paragraph = new P('Esse é meu paragrafo!');
$paragraph->attr('teste')->show();

$lists = new Lists(['item 1', 'item 2']);
$lists->attr('style', [''])->addItems('item 3', ['teste'=>false])->show();
29 changes: 23 additions & 6 deletions src/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,20 @@ class Element implements InterfaceElements
* @method __construct
* @param string
* @param mix
* @param array
* @param mix
*/
public function __construct(string $name, $value = null, array $attr = [])
public function __construct(string $name, $value = null, $attr = null)
{
$this->name = strtolower($name);
$this->setName($name);
$this->value($value);
$this->attr = $attr;
$this->attr($attr);
}

public function setName(string $name)
{
$this->name = strtolower($name);

return $this;
}

/**
Expand All @@ -62,6 +69,10 @@ public function __construct(string $name, $value = null, array $attr = [])
*/
public function value($value)
{
if (is_null($value)) {
return $this;
}

$this->value[] = $value;

return $this;
Expand All @@ -75,7 +86,11 @@ public function value($value)
*/
public function attr($attr, $value = null)
{
if (is_array($attr)) {
if (null === $attr) {
return $this;
}

if (is_array($attr) && count($attr) > 0) {
$this->attr = array_merge($attr, $this->attr);

return $this;
Expand All @@ -101,6 +116,8 @@ public function render()
$this->tag .= $key . '="';
if (is_array($value)) {
$this->tag .= implode(' ', $value);
} else {
$this->tag .= $value != false? $value: 0;
}
$this->tag .= '" ';
}
Expand All @@ -116,7 +133,7 @@ public function render()

public function __tostring()
{
$this->render();
return $this->render();
}

/**
Expand Down
64 changes: 64 additions & 0 deletions src/Elements/Lists.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace HTMLBuilder\Elements;

use HTMLBuilder\Element;

class Lists extends Element
{
private $items;

public function __construct(array $items = null, string $type = 'unordered')
{
$this->type($type);
$this->item($items);
}

private function type(string $type)
{
$type = strtolower($type);
switch($type) {
case 'unordered':
$this->setName('ul');
break;
case 'ordered':
$this->setName('ol');
break;
default:
$this->setName('ul');
}
}

private function item($item)
{
if (is_array($item)) {
foreach($item as $chave => $value) {
if (is_array($value)) {
$this->addItems($key, $value);
} else {
$this->addItems($value);
}
}

return $this;
}

$this->addItems($item);

return $this;
}

public function addItems($value, $attr = null)
{
$this->items[] = new Element('li', $value, $attr);

return $this;
}

public function show()
{
$this->value($this->items);

echo $this->render();
}
}
32 changes: 10 additions & 22 deletions src/Elements/Paragraph.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,34 @@
* @dependence Factory
*/
use HTMLBuilder\Interfaces\InterfaceElements;
use HTMLBuilder\ElementFactory;
use HTMLBuilder\Element;

/**
* @class Table
*
* Implementa uma ferramenta de criação de tabelas em HTML
*
* @author Alan Freire <alan_freire@msn.com>
* @version 1.0.0
* @version 1.2.0
* @copyright MIT 2017
*/
class Paragraph implements InterfaceElements
class Paragraph extends Element
{
public function __construct($content = null, array $attributes = array())
{
$this->paragraph = ElementFactory::make("p");
$this->setName('p');

$this->paragraph->value($content);
if (!is_null($content)) {
$this->value($content);
}

if (count($attributes) > 0) {
$this->paragraph->attr($attributes);
$this->attr($attributes);
}
}

public function value($content)
{
$this->paragraph->value($content);

return $this;
}

public function attr($attributes, $value = null)
{
$this->paragraph->attr($attributes, $value);

return $this;
}

public function render()
public function show()
{
echo $this->paragraph->render();
print $this->render();
}
}

0 comments on commit 37fba0d

Please sign in to comment.