-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from genius257/2.x
2.x
- Loading branch information
Showing
16 changed files
with
636 additions
and
322 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?php | ||
|
||
namespace Genius257\View; | ||
|
||
use Genius257\View\Dom\Node\RootNode; | ||
use PHPHtmlParser\Dom\Node\HtmlNode; | ||
|
||
abstract class ComponentWithChildren extends Component | ||
{ | ||
/** @var array<int, HtmlNode|Component|Stringable|string> */ | ||
public array $children = []; | ||
|
||
/** | ||
* @param HtmlNode|Component|Stringable|string $child | ||
*/ | ||
public static function childToString($child): string | ||
{ | ||
if ($child instanceof Stringable) { | ||
return $child->__toString(); | ||
} | ||
|
||
if ($child instanceof HtmlNode) { | ||
return $child->__toString(); | ||
} | ||
|
||
if ($child instanceof Component) { | ||
return View::renderComponent($child); | ||
} | ||
|
||
return strval($child); | ||
} | ||
|
||
/** | ||
* Renders each child and returns the concatenated strings. | ||
* | ||
* @return string | ||
*/ | ||
public function renderChildren(): string | ||
{ | ||
return implode( | ||
'', | ||
array_map( | ||
function ($child) { | ||
return strval($child); | ||
}, | ||
$this->children | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Get *REAL* HTMLNode children. | ||
* | ||
* *REAL*, meaning that child components would produce a root HTMLNode, | ||
* making changes to the child data appear as not working in some cases. | ||
* | ||
* @return HtmlNode[] | ||
*/ | ||
public function getHTMLNodeChildren() | ||
{ | ||
$HTMLNodes = []; | ||
foreach ($this->children as $child) { | ||
if ($child instanceof RootNode) { | ||
foreach ($child->getChildren() as $child) { | ||
if ($child instanceof HtmlNode) { | ||
$HTMLNodes[] = $child; | ||
} | ||
} | ||
} elseif ($child instanceof HtmlNode) { | ||
$HTMLNodes[] = $child; | ||
} | ||
} | ||
|
||
return $HTMLNodes; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.