PHP printf-syntax printf printf parser
Parses printf strings เป็นกระแสของ Lexemes
ติดตั้งเวอร์ชันล่าสุดด้วย:
composer require ' donatj/printf-parser ' นี่คือตัวอย่างง่ายๆ:
<?php
require __DIR__ . ' /../vendor/autoload.php ' ;
$ emitter = new donatj Printf LexemeEmitter ;
$ parser = new donatj Printf Parser ( $ emitter );
$ parser -> parseStr ( ' percent of %s: %d%% ' );
$ lexemes = $ emitter -> getLexemes ();
foreach ( $ lexemes as $ lexeme ) {
echo $ lexeme -> getLexItemType () . ' -> ' ;
echo var_export ( $ lexeme -> getVal (), true );
if ( $ lexeme instanceof donatj Printf ArgumentLexeme ) {
echo ' arg type: ' . $ lexeme -> argType ();
}
echo PHP_EOL ;
}เอาท์พุท:
! -> 'percent of '
s -> 's' arg type: string
! -> ': '
d -> 'd' arg type: int
! -> '%'
Parser ใช้ PHP PrintF PrintF Printf String Parser
function __construct( donatj Printf Emitter $ emitter )Parser Constructor
$emitter - emitter ที่กำหนดเพื่อปล่อย Lexemes เป็นแยกวิเคราะห์ function parseStr( string $ string ) : voidวิเคราะห์สตริง printf และปล่อย lexemes ที่แยกวิเคราะห์ไปยังตัวปล่อยที่กำหนดค่า
function getLexemes() : donatj Printf LexemeCollectionคืนคำศัพท์ที่ได้รับจาก emitter เป็น Lexemecollection ที่ไม่เปลี่ยนรูป
Lexemecollection เป็นคอลเล็กชั่น Lexemes ที่ไม่เปลี่ยนรูป
function getInvalid() : ? donatj Printf Lexemeดึง Lexeme หรือ Null ที่ไม่ถูกต้องครั้งแรกหากทั้งหมดถูกต้อง
สิ่งนี้มีประโยชน์สำหรับการตรวจสอบว่าสตริง printf แยกวิเคราะห์โดยไม่มีข้อผิดพลาด
function toArray() : arrayรับ Lexemecollection เป็นอาร์เรย์ที่สั่งซื้อของ Lexemes
function argTypes() : array ArgumentLexeme::ARG_TYPE_MISSING
ArgumentLexeme::ARG_TYPE_INT
ArgumentLexeme::ARG_TYPE_DOUBLE
ArgumentLexeme::ARG_TYPE_STRING
Lexeme แสดงถึงองค์ประกอบ "พื้นฐาน" ของสตริง printf - สายอักขระทั้งตัวอักษร "!" หรือ lexemes ที่ไม่ถูกต้อง
<?php
namespace donatj Printf ;
class Lexeme {
public const T_INVALID = '' ;
public const T_LITERAL_STRING = ' ! ' ;
} function __construct( string $ lexItemType , string $ val , int $ pos )ตัวสร้าง Lexitem
function getLexItemType() : stringประเภทของ printf lexeme
function getVal() : stringข้อความของ Lexeme
function getPos() : intตำแหน่งสตริงของ lexeme ที่กำหนด
<?php
namespace donatj Printf ;
class ArgumentLexeme {
/** @var string the argument is treated as an integer and presented as a binary number. */
public const T_INT_AS_BINARY = ' b ' ;
/** @var string the argument is treated as an integer and presented as the character with that ASCII value. */
public const T_INT_AS_CHARACTER = ' c ' ;
/** @var string the argument is treated as an integer and presented as a (signed) decimal number. */
public const T_INT = ' d ' ;
/** @var string the argument is treated as scientific notation (e.g. 1.2e+2). The precision specifier stands for the
number of digits after the decimal point since PHP 5.2.1. In earlier versions, it was taken as number of
significant digits (one less). */
public const T_DOUBLE_AS_SCI = ' e ' ;
/** @var string like %e but uses uppercase letter (e.g. 1.2E+2). */
public const T_DOUBLE_AS_SCI_CAP = ' E ' ;
/** @var string the argument is treated as a float and presented as a floating-point number (locale aware). */
public const T_FLOAT_LOCALE = ' f ' ;
/** @var string the argument is treated as a float and presented as a floating-point number (non-locale aware).
Available since PHP 5.0.3. */
public const T_FLOAT_NO_LOCALE = ' F ' ;
/** @var string shorter of %e and %f. */
public const T_FLOAT_AUTO_SCI = ' g ' ;
/** @var string shorter of %E and %F. */
public const T_FLOAT_AUTO_SCI_CAP = ' G ' ;
/** @var string the argument is treated as an integer and presented as an octal number. */
public const T_INT_AS_OCTAL = ' o ' ;
/** @var string the argument is treated as and presented as a string. */
public const T_STRING = ' s ' ;
/** @var string the argument is treated as an integer and presented as an unsigned decimal number. */
public const T_INT_UNSIGNED = ' u ' ;
/** @var string the argument is treated as an integer and presented as a hexadecimal number (with lowercase letters). */
public const T_INT_HEX = ' x ' ;
/** @var string the argument is treated as an integer and presented as a hexadecimal number (with uppercase letters). */
public const T_INT_HEX_CAP = ' X ' ;
public const VALID_T_TYPES = [ self :: T_INT_AS_BINARY , self :: T_INT_AS_CHARACTER , self :: T_INT , self :: T_DOUBLE_AS_SCI , self :: T_DOUBLE_AS_SCI_CAP , self :: T_FLOAT_LOCALE , self :: T_FLOAT_NO_LOCALE , self :: T_FLOAT_AUTO_SCI , self :: T_FLOAT_AUTO_SCI_CAP , self :: T_INT_AS_OCTAL , self :: T_STRING , self :: T_INT_UNSIGNED , self :: T_INT_HEX , self :: T_INT_HEX_CAP ];
public const ARG_TYPE_MISSING = '' ;
public const ARG_TYPE_INT = ' int ' ;
public const ARG_TYPE_DOUBLE = ' float ' ;
public const ARG_TYPE_STRING = ' string ' ;
/** @var string[] string s */
public const STRING_TYPES = [ self :: T_STRING ];
/** @var string[] integer d, u, c, o, x, X, b */
public const INTEGER_TYPES = [ self :: T_INT , self :: T_INT_UNSIGNED , self :: T_INT_AS_CHARACTER , self :: T_INT_AS_OCTAL , self :: T_INT_HEX , self :: T_INT_HEX_CAP , self :: T_INT_AS_BINARY ];
/** @var string[] double g, G, e, E, f, F */
public const DOUBLE_TYPES = [ self :: T_FLOAT_AUTO_SCI , self :: T_FLOAT_AUTO_SCI_CAP , self :: T_DOUBLE_AS_SCI , self :: T_DOUBLE_AS_SCI_CAP , self :: T_FLOAT_LOCALE , self :: T_FLOAT_NO_LOCALE ];
public const T_INVALID = '' ;
public const T_LITERAL_STRING = ' ! ' ;
} function __construct( string $ lexItemType , string $ val , int $ pos , ? int $ arg , bool $ showPositive , ? string $ padChar , ? int $ padWidth , bool $ leftJustified , ? int $ precision )ตัวสร้าง AgrigressLexeme
ตัวสร้าง Lexitem
function getArg() : ? int ตัวระบุตำแหน่งเช่น %3$s จะส่งคืน 3 และ %s จะส่งคืนค่าว่าง
function getShowPositive() : boolคือ "คำนำหน้าบวกตัวเลขบวกที่เปิดใช้งานการตั้งค่าสถานะ Plus Sign +"
function getPadChar() : ? stringธงอักขระแผ่นที่ระบุ
function getPadWidth() : ? intความกว้างของแผ่นที่ระบุ
function getLeftJustified() : boolเปิดใช้งานการตั้งค่าสถานะ JUGUTIFICITH ทางซ้ายหรือไม่?
function getPrecision() : ? intความแม่นยำของ Lexeme ระบุ
function argType() : stringส่งคืนตามประเภทของอาร์กิวเมนต์อย่างใดอย่างหนึ่งต่อไปนี้
Argumentlexeme :: arg_type_missing
Argumentlexeme :: arg_type_int
Argumentlexeme :: arg_type_double
ArgumentlexeMe :: arg_type_string
function getLexItemType() : stringประเภทของ printf lexeme
function getVal() : stringข้อความของ Lexeme
function getPos() : intตำแหน่งสตริงของ lexeme ที่กำหนด