Documentation

CRM_Utils_Type
in package

Tags
copyright

CiviCRM LLC https://civicrm.org/licensing

Table of Contents

Constants

BIG  = 30
BLOB_SIZE  = 65535
Maximum size of a MySQL BLOB or TEXT column in bytes.
EIGHT  = 8
FORTYFIVE  = 45
FOUR  = 4
HUGE  = 45
INT_MAX  = 2147483647
Maximum value of a MySQL signed INT column.
MEDIUM  = 20
SIX  = 6
SIXTEEN  = 16
T_BLOB  = 64
T_BOOLEAN  = 16
T_CCNUM  = 8192
T_DATE  = 4
T_EMAIL  = 2048
T_ENUM  = 2
T_FLOAT  = 512
T_INT  = 1
T_LONGTEXT  = 32
T_MEDIUMBLOB  = 16384
T_MONEY  = 1024
T_STRING  = 2
T_TEXT  = 32
T_TIME  = 8
T_TIMESTAMP  = 256
T_URL  = 4096
THIRTY  = 30
TWELVE  = 12
TWENTY  = 20
TWO  = 2

Methods

dataTypes()  : array<string|int, mixed>
Get list of available Data Types for Option Groups
escape()  : mixed
Verify that a variable is of a given type, and apply a bit of processing.
escapeAll()  : mixed
Helper function to call escape on arrays.
getDataTypeFromFieldMetadata()  : string
Get the data_type for the field.
getTextTypes()  : array<string|int, mixed>
Get all the types that are text-like.
getValidTypes()  : array<string|int, mixed>
mysqlOrderByCallback()  : mixed
preg_replace_callback for MysqlOrderBy escape.
mysqlOrderByFieldFunctionCallback()  : string
Preg_replace_callback for mysqlOrderByFieldFunction escape.
typeToString()  : string
Gets the string representation for a data type.
validate()  : mixed
Verify that a variable is of a given type.
validateAll()  : mixed
Helper function to call validate on arrays
validatePhpType()  : bool
Validate that a value matches a PHP type.

Constants

BIG

public mixed BIG = 30

BLOB_SIZE

Maximum size of a MySQL BLOB or TEXT column in bytes.

public mixed BLOB_SIZE = 65535

EIGHT

public mixed EIGHT = 8

FORTYFIVE

public mixed FORTYFIVE = 45

FOUR

public mixed FOUR = 4

HUGE

public mixed HUGE = 45

INT_MAX

Maximum value of a MySQL signed INT column.

public mixed INT_MAX = 2147483647

MEDIUM

public mixed MEDIUM = 20

SIX

public mixed SIX = 6

SIXTEEN

public mixed SIXTEEN = 16

T_BLOB

public mixed T_BLOB = 64

T_BOOLEAN

public mixed T_BOOLEAN = 16

T_CCNUM

public mixed T_CCNUM = 8192

T_DATE

public mixed T_DATE = 4

T_EMAIL

public mixed T_EMAIL = 2048

T_ENUM

public mixed T_ENUM = 2

T_FLOAT

public mixed T_FLOAT = 512

T_INT

public mixed T_INT = 1

T_LONGTEXT

public mixed T_LONGTEXT = 32

T_MEDIUMBLOB

public mixed T_MEDIUMBLOB = 16384

T_MONEY

public mixed T_MONEY = 1024

T_STRING

public mixed T_STRING = 2

T_TEXT

public mixed T_TEXT = 32

T_TIME

public mixed T_TIME = 8

T_TIMESTAMP

public mixed T_TIMESTAMP = 256

T_URL

public mixed T_URL = 4096

THIRTY

public mixed THIRTY = 30

TWELVE

public mixed TWELVE = 12

TWENTY

public mixed TWENTY = 20

TWO

public mixed TWO = 2

Methods

dataTypes()

Get list of available Data Types for Option Groups

public static dataTypes() : array<string|int, mixed>
Return values
array<string|int, mixed>

escape()

Verify that a variable is of a given type, and apply a bit of processing.

public static escape(mixed $data, string $type[, bool $abort = TRUE ]) : mixed
Parameters
$data : mixed

The value to be verified/escaped.

$type : string

The type to verify against.

$abort : bool = TRUE

If TRUE, the operation will throw an CRM_Core_Exception on invalid data.

Tags
throws
CRM_Core_Exception
Return values
mixed

The data, escaped if necessary.

escapeAll()

Helper function to call escape on arrays.

public static escapeAll(mixed $data, mixed $type[, mixed $abort = TRUE ]) : mixed
Parameters
$data : mixed
$type : mixed
$abort : mixed = TRUE
Tags
see
escape

getDataTypeFromFieldMetadata()

Get the data_type for the field.

public static getDataTypeFromFieldMetadata(array<string|int, mixed> $fieldMetadata) : string
Parameters
$fieldMetadata : array<string|int, mixed>

Metadata about the field.

Return values
string

getTextTypes()

Get all the types that are text-like.

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

The returned types would all legitimately be compared to '' by mysql in a query.

e.g WHERE display_name = '' is valid WHERE id = '' is not and in some mysql configurations and queries could cause an error.

Return values
array<string|int, mixed>

getValidTypes()

public static getValidTypes() : array<string|int, mixed>
Return values
array<string|int, mixed>

An array of type in the form 'type name' => 'int representing type'

mysqlOrderByCallback()

preg_replace_callback for MysqlOrderBy escape.

public static mysqlOrderByCallback(mixed $matches) : mixed
Parameters
$matches : mixed

mysqlOrderByFieldFunctionCallback()

Preg_replace_callback for mysqlOrderByFieldFunction escape.

public static mysqlOrderByFieldFunctionCallback(string $clause) : string

Add backticks around the field name.

Parameters
$clause : string
Return values
string

typeToString()

Gets the string representation for a data type.

public static typeToString(int $type) : string
Parameters
$type : int

Integer number identifying the data type.

Return values
string

String identifying the data type, e.g. 'Int' or 'String'.

validate()

Verify that a variable is of a given type.

public static validate(mixed $data, string $type[, bool $abort = TRUE ][, string $name = 'One of parameters ' ]) : mixed
Parameters
$data : mixed

The value to validate.

$type : string

The type to validate against.

$abort : bool = TRUE

If TRUE, the operation will CRM_Core_Error::fatal() on invalid data.

$name : string = 'One of parameters '

The name of the attribute

Tags
throws
CRM_Core_Exception
Return values
mixed

The data, escaped if necessary

validateAll()

Helper function to call validate on arrays

public static validateAll(mixed $data, string $type) : mixed
Parameters
$data : mixed
$type : string
Tags
throws
CRM_Core_Exception
see
validate

validatePhpType()

Validate that a value matches a PHP type.

public static validatePhpType(mixed $value, string|array<string|int, string> $types[, bool $isStrict = TRUE ]) : bool

Note that, at a micro-level, this is probably slower than using real PHP type-checking, but it doesn't seem bad. (In light benchmarking of ~1000 validations on an i3-10100, there is no obvious effect on the execution-time.) Should be fast enough for validating business entities.

Example usage: 'validatePhpType(123, 'int|double');`

Parameters
$value : mixed
$types : string|array<string|int, string>

The list of acceptable PHP types and/or classnames. Either an array or a string (with '|' delimiters). Note that 'null' is a distinct type. Ex: 'int' Ex: 'Countable|null' Ex: 'string|bool' Ex: 'string|false'

$isStrict : bool = TRUE

If data is likely to come from another text medium, then you may want to allow (say) numbers and string-like-numbers to be used interchangably.

With $isStrict=TRUE, the string "123" does not match type "int". The int 456 does not match type "double". etc.

With $isStrict=FALSE, the string "123" will match types "string", "int", and "double".

Return values
bool

        
On this page

Search results