<?php
declare(strict_types=1);
namespace Termwind\Html;
use Termwind\Components\Element;
use Termwind\Termwind;
use Termwind\ValueObjects\Node;
/**
* @internal
*/
final class CodeRenderer
{
public const TOKEN_DEFAULT = 'token_default';
public const TOKEN_COMMENT = 'token_comment';
public const TOKEN_STRING = 'token_string';
public const TOKEN_HTML = 'token_html';
public const TOKEN_KEYWORD = 'token_keyword';
public const ACTUAL_LINE_MARK = 'actual_line_mark';
public const LINE_NUMBER = 'line_number';
private const ARROW_SYMBOL_UTF8 = '➜';
private const DELIMITER_UTF8 = '▕ '; // '▶';
private const LINE_NUMBER_DIVIDER = 'line_divider';
private const MARKED_LINE_NUMBER = 'marked_line';
private const WIDTH = 3;
/**
* Holds the theme.
*
* @var array<string, string>
*/
private const THEME = [
self::TOKEN_STRING => 'text-gray',
self::TOKEN_COMMENT => 'text-gray italic',
self::TOKEN_KEYWORD => 'text-magenta strong',
self::TOKEN_DEFAULT => 'strong',
self::TOKEN_HTML => 'text-blue strong',
self::ACTUAL_LINE_MARK => 'text-red strong',
self::LINE_NUMBER => 'text-gray',
self::MARKED_LINE_NUMBER => 'italic strong',
self::LINE_NUMBER_DIVIDER => 'text-gray',
];
private string $delimiter = self::DELIMITER_UTF8;
private string $arrow = self::ARROW_SYMBOL_UTF8;
private const NO_MARK = ' ';
/**
* Highlights HTML content from a given node and converts to the content element.
*/
public function toElement(Node $node): Element
{
$line = max((int) $node->getAttribute('line'), 0);
$startLine = max((int) $node->getAttribute('start-line'), 1);
$html = $node->getHtml();
$lines = explode("\n", $html);
$extraSpaces = $this->findExtraSpaces($lines);
if ($extraSpaces !== '') {
$lines = array_map(static function (string $line) use ($extraSpaces): string {
return str_starts_with($line, $extraSpaces) ? substr($line, strlen($extraSpaces)) : $line;
}, $lines);
$html = implode("\n", $lines);
}
$tokenLines = $this->getHighlightedLines(trim($html, "\n"), $startLine);
$lines = $this->colorLines($tokenLines);
$lines = $this->lineNumbers($lines, $line);
return Termwind::div(trim($lines, "\n"));
}
/**
* Finds extra spaces which should be removed from HTML.
*
* @param array<int, string> $lines
*/
private function findExtraSpaces(array $lines): string
{
foreach ($lines as $line) {
if ($line === '') {
continue;
}
if (preg_replace('/\s+/', '', $line) === '') {
return $line;
}
}
return '';
}
/**
* Returns content split into lines with numbers.
*
* @return array<int, array<int, array{0: string, 1: non-empty-string}>>
*/
private function getHighlightedLines(string $source, int $startLine): array
{
$source = str_replace(["\r\n", "\r"], "\n", $source);
$tokens = $this->tokenize($source);
return $this->splitToLines($tokens, $startLine - 1);
}
/**
* Splits content into tokens.
*
* @return array<int, array{0: string, 1: string}>
*/
private function tokenize(string $source): array
{
$tokens = token_get_all($source);
$output = [];
$currentType = null;
$newType = self::TOKEN_KEYWORD;
$buffer = '';
foreach ($tokens as $token) {
if (is_array($token)) {
if ($token[0] !== T_WHITESPACE) {
$newType = match ($token[0]) {
T_OPEN_TAG, T_OPEN_TAG_WITH_ECHO, T_CLOSE_TAG, T_STRING, T_VARIABLE,
T_DIR, T_FILE, T_METHOD_C, T_DNUMBER, T_LNUMBER, T_NS_C,
T_LINE, T_CLASS_C, T_FUNC_C, T_TRAIT_C => self::TOKEN_DEFAULT,
T_COMMENT, T_DOC_COMMENT => self::TOKEN_COMMENT,
T_ENCAPSED_AND_WHITESPACE, T_CONSTANT_ENCAPSED_STRING => self::TOKEN_STRING,
T_INLINE_HTML => self::TOKEN_HTML,
default => self::TOKEN_KEYWORD
};
}
} else {
$newType = $token === '"' ? self::TOKEN_STRING : self::TOKEN_KEYWORD;
}
if ($currentType === null) {
$currentType = $newType;
}
if ($currentType !== $newType) {
$output[] = [$currentType, $buffer];
$buffer = '';
$currentType = $newType;
}
$buffer .= is_array($token) ? $token[1] : $token;
}
$output[] = [$newType, $buffer];
return $output;
}
/**
* Splits tokens into lines.
*
* @param array<int, array{0: string, 1: string}> $tokens
* @param int $startLine
* @return array<int, array<int, array{0: string, 1: non-empty-string}>>
*/
private function splitToLines(array $tokens, int $startLine): array
{
$lines = [];
$line = [];
foreach ($tokens as $token) {
foreach (explode("\n", $token[1]) as $count => $tokenLine) {
if ($count > 0) {
$lines[$startLine++] = $line;
$line = [];
}
if ($tokenLine === '') {
continue;
}
$line[] = [$token[0], $tokenLine];
}
}
$lines[$startLine++] = $line;
return $lines;
}
/**
* Applies colors to tokens according to a color schema.
*
* @param array<int, array<int, array{0: string, 1: non-empty-string}>> $tokenLines
* @return array<int, string>
*/
private function colorLines(array $tokenLines): array
{
$lines = [];
foreach ($tokenLines as $lineCount => $tokenLine) {
$line = '';
foreach ($tokenLine as $token) {
[$tokenType, $tokenValue] = $token;
$line .= $this->styleToken($tokenType, $tokenValue);
}
$lines[$lineCount] = $line;
}
return $lines;
}
/**
* Prepends line numbers into lines.
*
* @param array<int, string> $lines
* @param int $markLine
* @return string
*/
private function lineNumbers(array $lines, int $markLine): string
{
$lastLine = (int) array_key_last($lines);
$lineLength = strlen((string) ($lastLine + 1));
$lineLength = $lineLength < self::WIDTH ? self::WIDTH : $lineLength;
$snippet = '';
$mark = ' '.$this->arrow.' ';
foreach ($lines as $i => $line) {
$coloredLineNumber = $this->coloredLineNumber(self::LINE_NUMBER, $i, $lineLength);
if (0 !== $markLine) {
$snippet .= ($markLine === $i + 1
? $this->styleToken(self::ACTUAL_LINE_MARK, $mark)
: self::NO_MARK
);
$coloredLineNumber = ($markLine === $i + 1 ?
$this->coloredLineNumber(self::MARKED_LINE_NUMBER, $i, $lineLength) :
$coloredLineNumber
);
}
$snippet .= $coloredLineNumber;
$snippet .= $this->styleToken(self::LINE_NUMBER_DIVIDER, $this->delimiter);
$snippet .= $line.PHP_EOL;
}
return $snippet;
}
/**
* Formats line number and applies color according to a color schema.
*/
private function coloredLineNumber(string $token, int $lineNumber, int $length): string
{
return $this->styleToken(
$token, str_pad((string) ($lineNumber + 1), $length, ' ', STR_PAD_LEFT)
);
}
/**
* Formats string and applies color according to a color schema.
*/
private function styleToken(string $token, string $string): string
{
return (string) Termwind::span($string, self::THEME[$token]);
}
}
The Kueue Pay Payment Gateway is an innovative technology that facilitates seamless and secure transactions between merchants and their customers. It enables businesses to accept debit and credit card payments both online and in physical stores.
The Kueue Pay Payment Gateway acts as a bridge between a merchant’s website or point-of-sale system and the payment processing network. It securely transmits payment information, authorizes transactions, and provides real-time status updates.
The Kueue Pay Developer API empowers developers and entrepreneurs to integrate the Kueue Pay Payment Gateway directly into their websites or applications. This streamlines the payment process for customers and provides businesses with a customizable and efficient payment solution.
To access the Kueue Pay Developer API, you need to sign up for a developer account on our platform. Once registered, you’ll receive an API key that you can use to authenticate your API requests.
The Kueue Pay Developer API allows you to initiate payments, check the status of payments, and process refunds. You can create a seamless payment experience for your customers while maintaining control over transaction management.
Yes, the Kueue Pay Developer API is designed to accommodate businesses of varying sizes and industries. Whether you’re a small online store or a large enterprise, our API can be tailored to fit your specific payment needs.
The Kueue Pay Developer API is designed with simplicity and ease of use in mind. Our comprehensive documentation, code samples, and developer support resources ensure a smooth integration process for any web platform.
We offer competitive pricing plans for using the Kueue Pay Payment Gateway and Developer API. Details about fees and pricing tiers can be found on our developer portal.
Absolutely, the Kueue Pay Developer API offers customization options that allow you to tailor the payment experience to match your brand and user interface. You can create a seamless and cohesive payment journey for your customers.
We provide dedicated developer support to assist you with any issues or questions you may have during the API integration process. Reach out to our support team at developersupport@NFCPay.com for prompt assistance.
Remember, our goal is to empower your business with a robust and efficient payment solution. If you have any additional questions or concerns, feel free to explore our developer portal or contact our support team.