Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 64 additions & 56 deletions MicrofyClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,42 +326,44 @@ public static function a(string $href, ?string $text = null, string $target = ''
return "<a href=\"$href\"$targetAttr$classAttr>$text</a>";
}

public static function htmlTable(array $array, string $class = '', string $id = ''): string
{
if (empty($array)) return "<p><em>No data.</em></p>";

$idAttr = $id !== '' ? " id='" . htmlspecialchars($id, ENT_QUOTES) . "'" : '';

if ($class !== '') {
$tableTag = "<table{$idAttr} class='" . htmlspecialchars($class, ENT_QUOTES) . "'>";
} else {
$tableTag = "<table{$idAttr} border='1' cellpadding='6' cellspacing='0'>";
}

$html = $tableTag;

// thead
$html .= "<thead><tr>";
foreach (array_keys($array[0]) as $col) {
$html .= "<th>" . htmlspecialchars($col, ENT_QUOTES) . "</th>";
}
$html .= "</tr></thead>";

// tbody
$html .= "<tbody>";
foreach ($array as $row) {
$html .= "<tr>";
foreach ($row as $cell) {
$html .= "<td>" . htmlspecialchars($cell, ENT_QUOTES) . "</td>";
}
$html .= "</tr>";
}
$html .= "</tbody>";

$html .= "</table>";

return $html;
}
public static function htmlTable(array $array, string $class = '', string $id = ''): string
{
if (empty($array)) {
return "<p><em>No data.</em></p>";
}

$idAttr = $id !== '' ? " id='" . htmlspecialchars((string) $id, ENT_QUOTES) . "'" : '';

if ($class !== '') {
$tableTag = "<table{$idAttr} class='" . htmlspecialchars((string) $class, ENT_QUOTES) . "'>";
} else {
$tableTag = "<table{$idAttr} border='1' cellpadding='6' cellspacing='0'>";
}

$html = $tableTag;

// thead
$html .= "<thead><tr>";
foreach (array_keys($array[0]) as $col) {
$html .= "<th>" . htmlspecialchars((string) $col, ENT_QUOTES) . "</th>";
}
$html .= "</tr></thead>";

// tbody
$html .= "<tbody>";
foreach ($array as $row) {
$html .= "<tr>";
foreach ($row as $cell) {
$html .= "<td>" . htmlspecialchars((string) $cell, ENT_QUOTES) . "</td>";
}
$html .= "</tr>";
}
$html .= "</tbody>";

$html .= "</table>";

return $html;
}

// --- MISC OUTPUT ---

Expand Down Expand Up @@ -423,15 +425,18 @@ public static function fail(string $msg = 'Error'): void
self::json(['status' => 'fail', 'msg' => $msg]);
}

public static function jsonArray(array $data): string
{
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

public static function jsonString(string $msg, bool $ok = true): string
{
return self::jsonArray(['status' => $ok ? 'ok' : 'fail', 'msg' => $msg]);
}
public static function jsonArray(array $data): string
{
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
}

public static function jsonString(string $msg, bool $ok = true): string
{
return self::jsonArray([
'status' => $ok ? 'ok' : 'fail',
'msg' => $msg,
]);
}


// --- STRING ---
Expand Down Expand Up @@ -508,22 +513,25 @@ public static function span(string $text = '', string $class = ''): void
echo "<span" . self::classAttr($class) . ">$text</span>";
}

public static function div(string $text, array $attrs = []): string {
$attrStr = self::buildAttr($attrs);
return "<div$attrStr>$text</div>";
}
public static function div(string $text, array $attrs = []): string
{
$attrStr = self::buildAttr($attrs);
return "<div$attrStr>$text</div>";
}
public static function section(string $text = '', string $class = ''): void
{
echo "<section" . self::classAttr($class) . ">$text</section>";
}

public static function buildAttr(array $attrs): string {
$parts = [];
foreach ($attrs as $k => $v) {
$parts[] = "$k=\"" . htmlspecialchars($v) . "\"";
}
return $parts ? ' ' . implode(' ', $parts) : '';
}
public static function buildAttr(array $attrs): string
{
$parts = [];
foreach ($attrs as $k => $v) {
$parts[] = "$k=\"" . htmlspecialchars((string) $v) . "\"";
}

return $parts ? ' ' . implode(' ', $parts) : '';
}

// --- CODE BLOCKS ---

Expand Down
5 changes: 0 additions & 5 deletions microfy_aliases.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,9 +412,4 @@ function jsonString(...$args) {
return Microfy::jsonString(...$args);
}
}
if (!function_exists('jsonString')) {
function jsonString(...$args) {
return Microfy::jsonString(...$args);
}
}