Skip to content

Commit

Permalink
complete refactoring of api
Browse files Browse the repository at this point in the history
  • Loading branch information
dermatthes committed Nov 28, 2018
1 parent 4068e67 commit 6ede1e4
Show file tree
Hide file tree
Showing 11 changed files with 182 additions and 100 deletions.
2 changes: 1 addition & 1 deletion .kick.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ command:
- "composer update"

test:
- "phpunit /opt/tests"
- "phpunit --testdox /opt/tests"
42 changes: 29 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,40 @@
# Phore HTML Toolkit

- Fluent API for creating HTML Elements
- Create, alter, render

## Basic example

```php
// Create a div element:
$elem = fhtml("div @id=main @class=website");

// Append a <div id=content> to the
$elem[] = [
"div @id=content" => "Some Content"
];

// Append paragraph to content div:
$elem["?#content"][] = ["p" => "Some escaped content"];

// Render full page including html-header
echo $elem->renderPage();
```
$elem = fhtml("div @class=someClass @id=abc");
$elem->elem("a @href=:url", ["http://some.url"])->content("Click me");
$elem->render(); // Render partial
$elem->renderPage(); // Render full page including html-header
will output:

```html
<div id="main" class="website">
<div id="content">
Some Content
<p>Some escaped content</p>
</div>
</div>
```

## Altering Elements

## Creating html structures



```
$doc = fhtml("div @id=name2 @class=bordered");
Expand All @@ -21,14 +44,7 @@ $doc->alter();

## Rendering Templates

```
$doc->tpl([
"div @class=content" => [
["p" => "Hello World"],
["p" => "And hello world 2"]
]
]);
```


## Appending to Templates

Expand Down
2 changes: 1 addition & 1 deletion src/Elements/DocumentNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __addToIndex(HtmlElement $element)
}
}

public function getElementById ($id) : HtmlContainerElement
public function getElementById ($id) : HtmlElement
{
if ( ! isset ($this->indexById[$id]))
throw new \InvalidArgumentException("Query element id '$id': No element found.");
Expand Down
8 changes: 4 additions & 4 deletions src/Elements/HtmlContainerElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ public function add(HtmlElementNode $child)
$this->children[] = $child;
$child->_setParent($this);

if ($child instanceof DocumentNode) {
$this->getRootDocument()->__addToIndex($child);
return;
}
if ($child instanceof HtmlContainerElement)
$this->getRootDocument()->__addToIndex($child->getDocument());

if ($child instanceof HtmlElement) {
$id = $child->getAttrib("id");
if ($id !== null)
$this->getRootDocument()->__addToIndex($child);
}

}

/**
Expand Down
49 changes: 23 additions & 26 deletions src/Fhtml/FHtml.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ class FHtml implements HtmlElementNode, \ArrayAccess

protected $emptyTags = ["meta"=>true, "img"=>true, "br"=>true, "hr"=>true, "input"=>true, "link"=>true];

public function __construct(array $template=null)
public function __construct($elementDef=null, $arguments=[])
{
$this->documentNode = new DocumentNode();
$this->curNode = $this->documentNode;

if ($template !== null)
$this->tpl($template);
if (is_string($elementDef)) {
$elem = $this->elem($elementDef, $arguments);
$this->curNode = $elem->curNode;

}
if (is_array($elementDef))
$this->tpl($elementDef, $arguments);
}


Expand Down Expand Up @@ -125,17 +130,8 @@ protected function _parseElem (string $elemDef, $arrayArgs=[]) : array
* @param array $arrayArgs
* @return FHtml
*/
public function elem(string $elemDef, $arrayArgs=[]) : self
protected function elem(string $elemDef, $arrayArgs=[]) : self
{
if (strpos($elemDef, ">") !== false) {
// Multi-Element
$curElem = $this;
foreach (explode(">", $elemDef) as $curDef) {
$curElem = $curElem->elem(trim ($curDef));
}
return $curElem;
}

[$tagName, $attrs] = $this->_parseElem($elemDef, $arrayArgs);

if (isset ($this->emptyTags[$tagName])) {
Expand All @@ -148,7 +144,7 @@ public function elem(string $elemDef, $arrayArgs=[]) : self
}


public function content($child) : self
protected function content($child) : self
{
if ($child === null)
return $this;
Expand All @@ -163,13 +159,13 @@ public function content($child) : self
return $this;
}

public function text(string $textContent) : self
protected function text(string $textContent) : self
{
$this->curNode->add(new TextNode($textContent));
return $this;
}

public function html__unescaped__(string $rawHtmlContent) : self
protected function html__unescaped__(string $rawHtmlContent) : self
{
$this->curNode->add(new RawHtmlNode($rawHtmlContent));
return $this;
Expand All @@ -183,7 +179,7 @@ private function cloneit ($curNode) : FHtml
return $new;
}

public function end() : self
protected function end() : self
{
if ($this->curNode->getParent() === $this->curNode)
throw new \InvalidArgumentException("end(): Node is document node.");
Expand Down Expand Up @@ -229,7 +225,7 @@ public function __toString()
*
* @return FHtml
*/
public function empty() : self
public function clear() : self
{
if ($this->curNode instanceof HtmlContainerElement)
$this->curNode->clearChildren();
Expand Down Expand Up @@ -307,12 +303,6 @@ public function getNode() : HtmlElement
}


public function macro(FHtmlMacro $macro) : self
{
return $macro->onAttach($this);
}


public function _setParent(HtmlContainerElement $parent)
{
$this->parent = $parent;
Expand Down Expand Up @@ -372,12 +362,19 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
$elem = $this;
if ($offset !== null) {
$elem = $this->elem($offset);
$elem->tpl($value);
}
if (is_array($value)) {
$elem->tpl($value, []);
return;
}
if ($value instanceof FHtml) {
$this->curNode->add($value->getDocument());
return;
}
$this->tpl($value);
$elem->content($value);
}

/**
Expand Down
30 changes: 0 additions & 30 deletions src/Fhtml/FHtmlMacro.php

This file was deleted.

12 changes: 6 additions & 6 deletions src/Fhtml/_FHtmlTemplateTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
trait _FHtmlTemplateTrait
{

private function _addStructRecursive ($node, FHtml $pointer) : void
private function _addStructRecursive ($node, FHtml $pointer, array $arguments) : void
{
if ($node instanceof HtmlElementNode) {
$pointer->curNode->add($node);
return;
}
if (is_callable($node)) {
$ret = $node($pointer);
$pointer->tpl($ret);
$pointer->tpl($ret, $arguments);
return;
}
if (is_string($node)) {
Expand All @@ -45,13 +45,13 @@ private function _addStructRecursive ($node, FHtml $pointer) : void

foreach ($node as $key => $value) {
if (is_string($key)) {
$this->_addStructRecursive($value, $pointer->elem($key));
$this->_addStructRecursive($value, $pointer->elem($key, $arguments), $arguments);
//if ($pointer->curNode !== $pointer->documentNode)
// $pointer->end();
continue;
}
if (is_int($key)) {
$this->_addStructRecursive($value, $pointer);
$this->_addStructRecursive($value, $pointer, $arguments);
continue;
}
if ($value instanceof HtmlElementNode) {
Expand All @@ -64,9 +64,9 @@ private function _addStructRecursive ($node, FHtml $pointer) : void
return;
}

public function tpl($input) : self
protected function tpl($input, array $arguments) : self
{
$this->_addStructRecursive($input, $this);
$this->_addStructRecursive($input, $this, $arguments);
return $this;
}
}
15 changes: 10 additions & 5 deletions src/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@


function fhtml($elem=null, $params=[]) : \Phore\Html\Fhtml\FHtml {
if ($elem === null)
return new \Phore\Html\Fhtml\FHtml();
if (is_array($elem))
return (new \Phore\Html\Fhtml\FHtml($elem));
return (new \Phore\Html\Fhtml\FHtml())->elem($elem, $params);
/*
if (is_string($elem)) {
return (new \Phore\Html\Fhtml\FHtml())->elem($elem, $params);
}
*/
return new \Phore\Html\Fhtml\FHtml($elem, $params);
}

/**
* FHTML Empty Element
*/
define("FE", null);
16 changes: 13 additions & 3 deletions tests/integration/AlterElementsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,25 @@
class AlterElementsTest extends TestCase
{


public function testAlterElements()
{
$doc = fhtml("div");
$doc->elem("p @id=abc @a @b");
$doc[] = fhtml("p @id=abc @a @b");

$doc->query("#abc")->alter("@class=-b +c");
$doc["?#abc"]->alter("@class=-b +c");

$this->assertEquals('<div><p id="abc" class="a c"></p></div>', $doc->render(false));
}


public function testRemoveElements()
{
$doc = fhtml("div");
$doc[] = fhtml(["p @id=abc" => ["p" => "text"]]);

$doc["?#abc"]->clear();

$this->assertEquals('<div><p id="abc"></p></div>', $doc->render(false));
}

}
Loading

0 comments on commit 6ede1e4

Please sign in to comment.