Kotchasan

InputItem
in package

Input Object

Represents an input item from various sources such as GET, POST, SESSION, or COOKIE.

Tags
see
https://www.kotchasan.com/

Table of Contents

$type  : string|null
$value  : mixed
__construct()  : mixed
Class Constructor
all()  : mixed
Get the raw input value.
color()  : string|array<string|int, mixed>
Get the color value. Returns a string or an array of strings.
create()  : static
Create an InputItem object.
date()  : string|array<string|int, mixed>|null
Get the date and time value.
description()  : string
Get the sanitized and truncated text.
detail()  : string
Remove PHP tags and escape characters for text input from an editor.
exists()  : bool
Check if the input variable exists.
filter()  : string|array<string|int, mixed>
Filter the input value based on a regular expression pattern.
isCookie()  : bool
Check if the input is from a COOKIE variable.
isGet()  : bool
Check if the input is from a GET variable.
isPost()  : bool
Check if the input is from a POST variable.
isSession()  : bool
Check if the input is from a SESSION variable.
keywords()  : string
Get the sanitized and truncated keywords from the input value.
number()  : string|array<string|int, mixed>
Extracts numbers or an array of numbers from the input.
password()  : string|array<string|int, mixed>
Validates and filters the input as a password.
quote()  : string|array<string|int, mixed>
Accepts text input and converts single quotes to HTML entity '&#39;', and trims leading and trailing spaces.
text()  : string|array<string|int, mixed>
Accepts text input and converts special characters '&', '"', "'", '<', '>', and '\' to their corresponding HTML entities, and trims leading and trailing spaces.
textarea()  : string|array<string|int, mixed>
Converts '<', '>', '\', '{', '}', and '\n' to their corresponding HTML entities, converts '\n' to '<br>', and trims leading and trailing spaces.
time()  : string|null
Get the time value.
toArray()  : array<string|int, mixed>
Converts the input value to an array.
toBoolean()  : bool|array<string|int, mixed>
Converts the input value to a boolean or an array of booleans.
toDouble()  : float|array<string|int, mixed>
Converts the input value to a double.
toFloat()  : float|array<string|int, mixed>
Converts the input value to a float or an array of floats.
toInt()  : int|array<string|int, mixed>
Converts the input value to an integer or an array of integers.
toObject()  : object|array<string|int, mixed>
Converts the input value to an object or an array of objects.
topic()  : string|array<string|int, mixed>
Converts tags and removes extra spaces (not exceeding 1 space) without line breaks.
toString()  : string|array<string|int, mixed>|null
Converts the input value to a string, an array of strings, or null.
url()  : string|array<string|int, mixed>
Converts tags without converting &amp; and removes trailing spaces.
username()  : string|array<string|int, mixed>
Accepts email addresses and phone numbers only.
checkValue()  : string
Check the validity of a value.
cut()  : string
Truncate a string to a specified length.

Properties

$type

protected string|null $type

The input type (e.g., GET, POST, SESSION, COOKIE)

$value

protected mixed $value

The input value

Methods

__construct()

Class Constructor

public __construct([mixed $value = null ][, string|null $type = null ]) : mixed
Parameters
$value : mixed = null

The input value (default: null)

$type : string|null = null

The input type (e.g., GET, POST, SESSION, COOKIE) (default: null)

Return values
mixed

all()

Get the raw input value.

public all() : mixed
Return values
mixed

The raw input value

color()

Get the color value. Returns a string or an array of strings.

public color() : string|array<string|int, mixed>
Tags
assert

create('#000')->color() [==] '#000'

assert

create('red')->color() [==] 'red'

Return values
string|array<string|int, mixed>

The color value

create()

Create an InputItem object.

public static create(mixed $value[, string|null $type = null ]) : static
Parameters
$value : mixed

The input value

$type : string|null = null

The input type (e.g., GET, POST, SESSION, COOKIE) (default: null)

Return values
static

The created InputItem object

date()

Get the date and time value.

public date([bool $strict = false ]) : string|array<string|int, mixed>|null

Returns null if the date value is empty or not in the correct format.

Parameters
$strict : bool = false

True to validate the date value strictly, false to skip validation (default: false)

Tags
assert

create('2016-01-01 20:20:20')->date() [==] '2016-01-01 20:20:20'

assert

create('2016-01-01 20:20:20')->date() [==] '2016-01-01 20:20:20'

assert

create('2016-01-01 20:20:20')->date(true) [==] '2016-01-01 20:20:20'

assert

create('20:20:20')->date() [==] '20:20:20'

assert

create('20:20')->date() [==] '20:20'

assert

create('20:20')->date(true) [==] '20:20:00'

assert

create('2016-01-01')->date() [==] '2016-01-01'

assert

create('')->date() [==] null

assert

create(null)->date() [==] null

Return values
string|array<string|int, mixed>|null

The date and time value

description()

Get the sanitized and truncated text.

public description(int $len) : string

Removes tags and unwanted characters from the text.

Parameters
$len : int

The maximum length of the text (default: 0)

Tags
assert

create("ท.ด(ส )อ"บ'\r\n\t")->description() [==] 'ท.ด(ส )อ บ'

assert

create('ทดสอบ<style>body {color: red}</style>')->description() [==] 'ทดสอบ'

assert

create('ทดสอบตัวหนา')->description() [==] 'ทดสอบตัวหนา'

assert

create('ทดสอบ{LNG_Language name}')->description() [==] 'ทดสอบ'

assert

create('ทดสอบ[code]ตัวหนา[/code]')->description() [==] 'ทดสอบ'

assert

create('ทดสอบ[b]ตัวหนา[/b]')->description() [==] 'ทดสอบตัวหนา'

assert

create('2 > 1 < 3 > 2{WIDGET_XXX}')->description() [==] '2 > 1 < 3 > 2'

assert

create('ทดสอบ')->description() [==] 'ทดสอบ'

assert

create('ท&ด"&nbsp;/__ส-อ+บ')->description() [==] 'ท ด \ /__ส-อ+บ'

assert

create('ภาคภูมิ')->description(2) [==] 'ภา'

assert

create('U1.username ทดสอบภาษาไทย')->description(5) [throws] \Kotchasan\InputItemException

Return values
string

The sanitized and truncated text

detail()

Remove PHP tags and escape characters for text input from an editor.

public detail() : string
Tags
assert

create('{ทด/สอบ}')->detail() [==] '{ทด\/สอบ}'

assert

create('U1.username')->detail() [throws] \Kotchasan\InputItemException

Return values
string

The sanitized text

exists()

Check if the input variable exists.

public exists() : bool
Return values
bool

True if the input variable exists, false otherwise

filter()

Filter the input value based on a regular expression pattern.

public filter(string $format[, string $replace = '' ]) : string|array<string|int, mixed>
Parameters
$format : string

The regular expression pattern to filter the input value

$replace : string = ''

The replacement string for filtered characters (default: '')

Tags
assert

create('admin,1234')->filter('0-9a-zA-Z,') [==] 'admin,1234'

assert

create('adminกข,12ฟ34')->filter('0-9a-zA-Z,') [==] 'admin,1234'

assert

create('U1.username')->filter('a-zA-Z0-9.') [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The filtered input value

isCookie()

Check if the input is from a COOKIE variable.

public isCookie() : bool
Return values
bool

True if the input is from a COOKIE variable, false otherwise

isGet()

Check if the input is from a GET variable.

public isGet() : bool
Return values
bool

True if the input is from a GET variable, false otherwise

isPost()

Check if the input is from a POST variable.

public isPost() : bool
Return values
bool

True if the input is from a POST variable, false otherwise

isSession()

Check if the input is from a SESSION variable.

public isSession() : bool
Return values
bool

True if the input is from a SESSION variable, false otherwise

keywords()

Get the sanitized and truncated keywords from the input value.

public keywords(int $len) : string

Removes tags, whitespace, and unwanted characters from the input value.

Parameters
$len : int

The maximum length of the keywords (default: 0)

Tags
assert

create("ทด \r\nสอบ")->keywords() [==] 'ทด สอบ'

assert

create('U1.username ทดสอบ')->keywords(5) [throws] \Kotchasan\InputItemException

Return values
string

The sanitized and truncated keywords

number()

Extracts numbers or an array of numbers from the input.

public number() : string|array<string|int, mixed>
Tags
assert

create(12345)->number() [==] '12345'

assert

create(0.12345)->number() [==] '012345'

assert

create('ทด0123สอ4บ5')->number() [==] '012345'

Return values
string|array<string|int, mixed>

The extracted numbers or an array of extracted numbers

password()

Validates and filters the input as a password.

public password() : string|array<string|int, mixed>

This function is used to validate and filter the input as a password. It ensures that the value consists of non-space characters only. The resulting value is filtered using the Text::password() function.

Return values
string|array<string|int, mixed>

The validated and filtered password

quote()

Accepts text input and converts single quotes to HTML entity '&#39;', and trims leading and trailing spaces.

public quote() : string|array<string|int, mixed>

This function accepts text input and converts single quotes to the HTML entity '''. It also trims leading and trailing spaces from the input value.

Tags
assert

create("ทด'สอบ")->quote() [==] "ทด'สอบ"

assert

create(' U1.username ')->quote() [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The processed text

text()

Accepts text input and converts special characters '&', '"', "'", '<', '>', and '\' to their corresponding HTML entities, and trims leading and trailing spaces.

public text() : string|array<string|int, mixed>

This function accepts text input and converts the special characters '&', '"', "'", '<', '>', and '' to their corresponding HTML entities. It also trims leading and trailing spaces from the input value. This function is useful for sanitizing input that does not allow HTML tags.

Tags
assert

create(" ทด/สอบ ")->text() [==] 'ทด\/สอบ<?php echo '555'?>'

assert

create('U1.username')->text() [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The processed text

textarea()

Converts '<', '>', '\', '{', '}', and '\n' to their corresponding HTML entities, converts '\n' to '<br>', and trims leading and trailing spaces.

public textarea() : string|array<string|int, mixed>

Used for receiving data from a textarea input.

Tags
assert

create("ทด/สอบ\n")->textarea() [==] "ทด\/สอบ\n<?php echo '$555'?>"

assert

create('U1.username')->textarea() [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The processed textarea input

time()

Get the time value.

public time([bool $strict = false ]) : string|null

Returns null if the time value is empty or in the wrong format.

Parameters
$strict : bool = false

True to validate the time value strictly, false to skip validation (default: false)

Tags
assert

create('20:20:20')->time() [==] '20:20:20'

assert

create('--:--')->time() [==] null

assert

create('')->time() [==] null

assert

create('20:20:20')->time() [==] '20:20:20'

assert

create('20:20')->time() [==] '20:20'

assert

create('20:20')->time(true) [==] '20:20:00'

Return values
string|null

The time value

toArray()

Converts the input value to an array.

public toArray() : array<string|int, mixed>

This function returns the input value as an array. If the value is not an array, an error is thrown.

Tags
assert

create(array('one', 'two'))->toArray() [==] array('one', 'two')

throws
InputItemException

if the value is not an array

Return values
array<string|int, mixed>

The converted array value

toBoolean()

Converts the input value to a boolean or an array of booleans.

public toBoolean() : bool|array<string|int, mixed>

This function returns 1 if the input value is not empty, and 0 otherwise.

Tags
assert

create(true)->toBoolean() [==] 1

assert

create(false)->toBoolean() [==] 0

assert

create(1)->toBoolean() [==] 1

assert

create(0)->toBoolean() [==] 0

assert

create(null)->toBoolean() [==] 0

Return values
bool|array<string|int, mixed>

The converted boolean value

toDouble()

Converts the input value to a double.

public toDouble() : float|array<string|int, mixed>

This function converts the input value to a double. If the value is null, it returns 0. The function removes any commas from the value before conversion.

Tags
assert

create(0.454)->toDouble() [==] 0.454

assert

create(0.545)->toDouble() [==] 0.545

assert

create('15,362.454')->toDouble() [==] 15362.454

Return values
float|array<string|int, mixed>

The converted double value

toFloat()

Converts the input value to a float or an array of floats.

public toFloat() : float|array<string|int, mixed>
Tags
assert

create(0.454)->toFloat() [==] 0.454

assert

create(0.545)->toFloat() [==] 0.545

Return values
float|array<string|int, mixed>

The converted float value

toInt()

Converts the input value to an integer or an array of integers.

public toInt() : int|array<string|int, mixed>
Tags
assert

create(0.454)->toInt() [==] 0

assert

create(2.945)->toInt() [==] 2

Return values
int|array<string|int, mixed>

The converted integer value

toObject()

Converts the input value to an object or an array of objects.

public toObject() : object|array<string|int, mixed>
Tags
assert

create('test')->toObject() [==] (object)'test'

Return values
object|array<string|int, mixed>

The converted object value

topic()

Converts tags and removes extra spaces (not exceeding 1 space) without line breaks.

public topic([bool $double_encode = true ]) : string|array<string|int, mixed>

Used for article titles.

Parameters
$double_encode : bool = true

true (default) to convert HTML entities like & to &amp;, false otherwise

Tags
assert

create(' ทด/สอบ'."\r\n\t".' ')->topic() [==] 'ทด\/สอบ <?php echo '555'?>'

assert

create('U1.username')->topic() [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The processed topic string

toString()

Converts the input value to a string, an array of strings, or null.

public toString() : string|array<string|int, mixed>|null

Caution: This function converts the value to a string without any validation. Use with caution.

Tags
assert

create('ทดสอบ')->toString() [==] 'ทดสอบ'

assert

create('1')->toString() [==] '1'

assert

create(1)->toString() [==] '1'

assert

create(null)->toString() [==] null

Return values
string|array<string|int, mixed>|null

The converted string value

url()

Converts tags without converting &amp; and removes trailing spaces.

public url() : string|array<string|int, mixed>

Used for URLs or emails.

Tags
assert

create(" http://www.kotchasan.com?a=1&b=2&c=3 ")->url() [==] 'http://www.kotchasan.com?a=1&b=2&c=3'

assert

create("javascript:alert('xxx')")->url() [==] 'alertxxx'

assert

create("http://www.xxx.com/javascript/")->url() [==] 'http://www.xxx.com/javascript/'

assert

create('U1.username')->url() [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The processed URL string

username()

Accepts email addresses and phone numbers only.

public username() : string|array<string|int, mixed>
Tags
assert

create(' admin@demo.com')->username() [==] 'admin@demo.com'

assert

create('012 3465')->username() [==] '0123465'

assert

create('U1.username')->username() [throws] \Kotchasan\InputItemException

Return values
string|array<string|int, mixed>

The processed username string

checkValue()

Check the validity of a value.

private checkValue(string $value) : string

Validates the value against a regular expression pattern. If the value matches the pattern, an exception is thrown indicating an invalid value.

Parameters
$value : string

The value to be checked

Tags
throws
InputItemException

If the value is invalid

Return values
string

The validated value

cut()

Truncate a string to a specified length.

private cut(string $str, int $len) : string

Truncates the given string to the specified length. If the length parameter is provided and the string is not empty, the string will be shortened to the specified length using mb_substr().

Parameters
$str : string

The string to be truncated

$len : int

The maximum length of the truncated string

Return values
string

The truncated string

Search results