Hallo allen,
Ik wil d.m.v. de __call() methode een shortcut schrijven voor mijn HTML helper alleen krijg ik de volgende fout:
Object of class Closure could not be converted to string in ...
Alleen ik kan niet plaatsen wat ik nu fout doe gezien ik te weinig met Closures werk en ik er na 2,5 uur nog steeds niet uitkom. Zover mijn kennis loopt zou hij dit moeten doen maar schijn bedriegt... p.s. ik weet dat ik in de __call() methoden nog argumenten moet verwerken etc.
Alvast bedankt,
Michael
Code
<?php
/**
* Magic shortcut to the custom HTML macros.
*
* @param string $name
* @param array $arguments
* @return string
*/
public function __call($name, array $arguments) {
if (!isset(static::$tags[$name])) {
throw new BadMethodCallException(vsprintf("Call to undefined method %s::%s().", [__CLASS__, $name]));
}
// Returnt een closure
return static::$tags[$name];
}
?>
Toon Meer
PHP: Testbestand
<?php
$html = new HTML();
// Registreer een nieuwe html tag
HTML::registerTag('foo', function($html, $content = null, $attributes = []) {
return $html->tag('foo', $attributes, $content);
});
var_dump(HTML::$tags, $html->foo());
?>
Toon Meer
PHP: Hele HTML Helper
<?php
namespace Sparky\Util;
use Closure;
use BadMethodCallException;
/**
* HTML Helper.
*
* @package Sparky
* @author Sparky Dev Team
* @copyright Copyright (c) 2015 - 2015, Michael Beers | Online Media en Design (http://michaelbeers.nl)
* @license http://opensource.org/licenses/Apache-2.0 Apache 2.0
* @link http://michaelbeers.nl
* @sice Version 1.0.0
*
* @TODO Add a shortcut to call tags?
*/
class HTML {
/**
* Should we return XHTML?
*
* @var boolean
*/
protected $xhtml;
/**
* Custom tags.
*
* @var array
*/
public static $tags = [];
/**
* Constructor.
*
* @param boolean $xhtml
*/
public function __construct($xhtml = false) {
$this->xhtml = $xhtml;
}
/**
* Magic shortcut to the custom HTML macros.
*
* @param string $name
* @param array $arguments
* @return string
*/
public function __call($name, array $arguments) {
if (!isset(static::$tags[$name])) {
throw new BadMethodCallException(vsprintf("Call to undefined method %s::%s().", [__CLASS__, $name]));
}
return static::$tags[$name];
}
/**
* Registers a new HTML tag.
*
* @param string $name
* @param \Closure $tag
*/
public static function registerTag($name, Closure $tag) {
static::$tags[$name] = $tag;
}
/**
* Takes an array of attributes and turns it into a string.
*
* @param array $attributes
* @return string
*/
protected function attributes($attributes) {
$attr = '';
foreach ($attributes as $attribute => $value) {
if (is_int($attribute)) {
$attribute = $value;
}
$attr .= ' ' . $attribute . '="' . $value . '"';
}
return $attr;
}
/**
* Creates a HTML5 tag.
*
* @param string $name
* @param array $attributes
* @param string $content
* @returnstring
*/
public function tag($name, array $attributes = [], $content = null) {
return '<' . $name . $this->attributes($attributes) . (($content === null) ? ($this->xhtml ? ' />' : '>') : '>' . $content . '</' . $name . '>');
}
/**
* Helper method for building media tags.
*
* @param string $type
* @param mixed $files
* @param array $attributes
*/
protected function buildMedia($type, $files, $attributes) {
$sources = '';
foreach ((array) $files as $file) {
$sources .= $this->tag('source', ['src' => $file]);
}
return $this->tag($type, $attributes, $sources);
}
/**
* Creates audio tag with support for multiple sources.
*
* @param mixed $files
* @param array $attributes
*/
public function audio($files, array $attributes = []) {
return $this->buildMedia('audio', $files, $attributes);
}
/**
* Creates video tag with support for multiple sources.
*
* @param mixed $files
* @param array $attributes
*/
public function video($files, array $attributes = []) {
return $this->buildMedia('video', $files, $attributes);
}
/**
* Helper method for building list tags.
*
* @param string $type
* @param mixed $items
* @param array $attributes
*/
protected function buildList($type, $items, $attributes) {
$list = '';
foreach ($items as $item) {
if (is_array($item)) {
$list .= $this->tag('li', [], $this->buildList($type, $item, []));
} else {
$list .= $this->tag('li', [], $item);
}
}
return $this->tag($type, $attributes, $list);
}
/**
* Builds an un-ordered list.
*
* @param array $items
* @param array $attributes
* @return string
*/
public function ul(array $items, array $attributes = []) {
return $this->buildList('ul', $items, $attributes);
}
/**
* Builds am ordered list.
*
* @param array $items
* @param array $attributes
* @return string
*/
public function ol(array $items, array $attributes = []) {
return $this->buildList('ol', $items, $attributes);
}
}
Toon Meer