final class UserSessions extends Model

Supports operations of the User Session model. Extends the Model class.

Traits

Creates the $created_at and $updated_at fields and performs the operation that updates timestamps.

Supports ability to validate console input.

Constants

BLACKLIST

Constant for blacklist.

WHITELIST

Constant for whitelist.

Properties

protected array $errors

An array of errors.

from  HasValidators
protected string $fieldName

The name of the field to be validated.

from  HasValidators
protected array $reservedKeywords

An array of reserved keywords.

from  HasValidators
protected array $validators

Array of validator callbacks.

from  HasValidators
static protected DB $_db

The instance of the DB class.

from  Model
int $id

The id for the session.

protected string $_modelName

The name of the model.

from  Model
static protected bool $_softDelete

$_softDelete value.

from  Model
static protected string $_table

The name of the table.

protected bool $_validates

Flag to indicate if validation is successful.

from  Model
string $created_at

Time record was created.

from  HasTimestamps
string $updated_at

Time record was updated.

from  HasTimestamps
string $session

The session.

string $user_agent

The user agent.

int $user_id

ID of user associated with this session.

Methods

void
addErrorMessage(string $message)

Adds a new error message to the $errors array.

alpha()

Enforce rule where input must contain only alphabetic characters.

alphaNumeric()

Enforce rule where input must be alphanumeric characters.

between(array $range)

Ensures input is between within a certain range in length.

classExists(string|array $namespace)

Checks if class exists within the specified namespace.

colonNotation()

Ensures response is in colon notation format.

void
compositeFieldValidation(array|string $additionalFieldData, array $conditions, array $bind)

Tests form validation with multiple fields. Used by the unique validator to test several fields for uniqueness.

different(mixed $data)

Enforce rule where response and $match parameter needs to be different.

void
displayErrorMessages()

Displays a list of all error messages.

dotNotation()

Ensures response is in dot notation format.

email()

Ensures input is a valid E-mail address.

fieldName(string|array $fieldName)

Sets name of field to be validated.

void
includeDeleted(bool|string $includeDeleted, array $queryParams)

Supports includeDeleted feature for database operations.

integer()

Enforce rule where input must be an integer.

ip()

Enforce rule where input must be a valid IP address.

isPortUsed(array $attributes)

Checks if a port on a particular host is in use. Assists in verifying if a port is available for a serve command. If the port is already in use an error message is presented to the user.

list(array $attributes)

Ensure user inputs valid comma separated list of values. The user must provide the following in the $attributes parameter: 1) Class containing full namespaced path 2) Name of function that returns an array of strings or a comma separated array of strings.

lower()

Enforces rule when input must contain at least one lower case character.

match(mixed $match)

Enforce rule where response and $match parameter needs to match.

max(int|array $maxRule)

Ensures input meets requirements for maximum allowable length.

min(int|array $minRule)

Ensures input meets requirements for minimum allowable length.

negative()

Enforces rule when input must be a negative number.

noSpecialChars()

Enforces rule when input must contain no special characters.

notReservedKeyword()

Enforce rule when reserved keywords should be avoided.

number()

Enforces rule when input must contain at least one numeric character.

numeric()

Enforce rule where input must contain only numeric characters.

static void
parseAttributes(object $object, array $validators)

Parse array containing additional validators or attributes for FrameworkQuestion as strings along with any additional parameters that maybe expected.

required()

Ensures required input is entered.

void
resetAfterValidation()

Resets fieldname and validators instance variables so duplicate messages are not displayed in forms.

positive()

Enforces rule when input must a positive number.

queue()

Validates if queue exists in database or redis.

setValidator(callable $validator)

Adds validator to array of validators to be used.

special()

Enforces rule when input must contain at least one special character.

tel()

Enforces rule where input must be a valid phone number.

testFilterNotation()

Ensures response is in colon notation format.

static array
tokens(string $data)

Split on commas (tolerate spaces), normalize to lowercase, drop empties.

unique(array $attributes)

Checks if value already exists in a database.

upper()

Enforces rule when input must contain at least one lower case character.

url()

Enforce rule where input must be a valid URL.

bool
validate(mixed $response)

Calls validator callbacks. This function also ensures validators don't bleed into next question if instance is reused.

__construct()

Default constructor.

from  Model
void
afterDelete()

Called before delete.

from  Model
void
afterSave()

Called before save.

from  Model
assign(array $params, array $list = [], string $assignmentFilter = self::BLACKLIST)

Update the object with an associative array.

from  Model
void
beforeDelete()

This runs before delete, needs to return a boolean

from  Model
void
beforeSave()

Called before save.

static int
count()

Retrieves number of rows affected or returned by the last query. This is a wrapper function for the DB::count function.

from  Model
data()

Grab object and if we just need data for smaller result set.

from  Model
bool
delete()

Wrapper for database delete function. If not softDelete we set it.

from  Model
static array
getColumns()

Gets columns from table.

from  Model
array
getColumnsForSave()

Gets an associative array of field values for insert or updating.

from  Model
static DB
getDb()

Returns an instance of the DB class.

from  Model
array
getErrorMessages()

Displays error messages when form validation fails.

from  Model
static array
_fetchStyleParams(array $params)

Used to set default fetchStyle param.

from  Model
static array|bool
find(array $params = [])

Wrapper for the find function that is found in the DB class.

from  Model
static object|bool
findById(int $id)

Get result from database by primary key ID.

from  Model
static array|bool
findByTable(string $table, array $params = [])

Performs a database query against a specified table using the same query-building logic as the default find method.

from  Model
static array
findAllByUserId(int $user_id, array $params = [])

Retrieves list of all records within a table related to a user.

from  Model
static object|bool
findFirst(array $params = [])

Wrapper for the findFirst function that is found in the DB class.

from  Model
static int
findTotal(array $params = [])

Returns number of records in a table. A wrapper function for findTotal function in DB class.

from  Model
bool
insert(array $fields)

Wrapper for database insert function.

from  Model
bool
isNew()

Checks if an object is a new insertion or an existing record.

from  Model
void
onConstruct()

Runs when the object is constructed.

from  Model
void
populateObjData(array|object $result)

Populates object with data.

from  Model
DB
query(string $sql, array $bind = [])

Wrapper for database query function.

from  Model
void
runValidation(bool|string $param, array $validators = [], string|null $fieldName = null)

Checks if results of validation is true or false. If false, the $this->_validates boolean instance variable is set to false. There are two ways to provide parameters for validation. You can pass the results from chaining functions and use just one parameter. Or you can provide the value of the field's name attribute, an array of validators, and a value for how you want the name of the field represented in error messages. If this value is not provided then the name attribute is used in messaging.

from  Model
bool
save()

Wrapper for update and insert functions. A failed form validation will cause this function to return false.

from  Model
static array
_softDeleteParams(array $params)

Adds to the conditions to avoid getting soft deleted rows returned

from  Model
bool
update(array $fields)

Wrapper for the update function found in the DB class.

from  Model
static int
updateWhere(array $fields, array $params)

Updates one or more rows in this model's underlying table using your framework's params-style conditions.

from  Model
bool
validationPassed()

Getter function for the $_validates boolean instance variable.

from  Model
void
validator()

Function that is called on save. If validation fails the save function will not proceed. This function is just a signature and must be implemented by models that run form validation because since it is called from within this class.

from  Model
void
timeStamps()

Sets values for timestamp fields.

static bool|UserSessions
getFromCookie()

Retrieves User Session information from cookie.

Details

in HasValidators at line 82
void addErrorMessage(string $message)

Adds a new error message to the $errors array.

Parameters

string $message

The error message to be added to the $errors array.

Return Value

void

in HasValidators at line 92
HasValidators alpha()

Enforce rule where input must contain only alphabetic characters.

Return Value

HasValidators

in HasValidators at line 106
HasValidators alphaNumeric()

Enforce rule where input must be alphanumeric characters.

Return Value

HasValidators

in HasValidators at line 124
HasValidators between(array $range)

Ensures input is between within a certain range in length.

Parameters

array $range

2 element array where position 0 is min and position 1 is max.

Return Value

HasValidators

Exceptions

FrameworkRuntimeException

in HasValidators at line 149
HasValidators classExists(string|array $namespace)

Checks if class exists within the specified namespace.

Parameters

string|array $namespace

A string or an array containing one element with string for the namespace.

Return Value

HasValidators

in HasValidators at line 164
HasValidators colonNotation()

Ensures response is in colon notation format.

Return Value

HasValidators

in HasValidators at line 186
void compositeFieldValidation(array|string $additionalFieldData, array $conditions, array $bind)

Tests form validation with multiple fields. Used by the unique validator to test several fields for uniqueness.

Parameters

array|string $additionalFieldData

Additional fields and values to test.

array $conditions

The conditions array passed by reference.

array $bind

The binds array passed by reference.

Return Value

void

in HasValidators at line 209
HasValidators different(mixed $data)

Enforce rule where response and $match parameter needs to be different.

Parameters

mixed $data

The value we want to compare.

Return Value

HasValidators

in HasValidators at line 224
void displayErrorMessages()

Displays a list of all error messages.

Return Value

void

in HasValidators at line 240
HasValidators dotNotation()

Ensures response is in dot notation format.

Return Value

HasValidators

in HasValidators at line 257
HasValidators email()

Ensures input is a valid E-mail address.

Return Value

HasValidators

in HasValidators at line 272
HasValidators fieldName(string|array $fieldName)

Sets name of field to be validated.

Parameters

string|array $fieldName

The name of the field to be validated.

Return Value

HasValidators

in HasValidators at line 288
void includeDeleted(bool|string $includeDeleted, array $queryParams)

Supports includeDeleted feature for database operations.

Parameters

bool|string $includeDeleted

The includedDeleted flag. When true the 'includeDeleted' element with a value of true is added to the $queryParams associative array.

array $queryParams

The parameters for the query that is passed by reference.

Return Value

void

in HasValidators at line 299
HasValidators integer()

Enforce rule where input must be an integer.

Return Value

HasValidators

in HasValidators at line 313
HasValidators ip()

Enforce rule where input must be a valid IP address.

Return Value

HasValidators

in HasValidators at line 331
HasValidators isPortUsed(array $attributes)

Checks if a port on a particular host is in use. Assists in verifying if a port is available for a serve command. If the port is already in use an error message is presented to the user.

Parameters

array $attributes

An array that assumes index 0 is the host and index 1 is timeout variable which is set to 3 if not provided.

Return Value

HasValidators

in HasValidators at line 360
HasValidators list(array $attributes)

Ensure user inputs valid comma separated list of values. The user must provide the following in the $attributes parameter: 1) Class containing full namespaced path 2) Name of function that returns an array of strings or a comma separated array of strings.

3) A string value in this array as an alias (optional).

Parameters

array $attributes

A : separate list in the following format: NamespaceToClass\Class:Method:Alias.

Return Value

HasValidators

in HasValidators at line 398
HasValidators lower()

Enforces rule when input must contain at least one lower case character.

Return Value

HasValidators

in HasValidators at line 413
HasValidators match(mixed $match)

Enforce rule where response and $match parameter needs to match.

Parameters

mixed $match

The value we want to compare.

Return Value

HasValidators

in HasValidators at line 428
HasValidators max(int|array $maxRule)

Ensures input meets requirements for maximum allowable length.

Parameters

int|array $maxRule

The maximum allowed size for input.

Return Value

HasValidators

in HasValidators at line 444
HasValidators min(int|array $minRule)

Ensures input meets requirements for minimum allowable length.

Parameters

int|array $minRule

The minimum allowed size for input.

Return Value

HasValidators

in HasValidators at line 459
HasValidators negative()

Enforces rule when input must be a negative number.

Return Value

HasValidators

in HasValidators at line 473
HasValidators noSpecialChars()

Enforces rule when input must contain no special characters.

Return Value

HasValidators

in HasValidators at line 487
HasValidators notReservedKeyword()

Enforce rule when reserved keywords should be avoided.

Return Value

HasValidators

in HasValidators at line 501
HasValidators number()

Enforces rule when input must contain at least one numeric character.

Return Value

HasValidators

in HasValidators at line 515
HasValidators numeric()

Enforce rule where input must contain only numeric characters.

Return Value

HasValidators

in HasValidators at line 537
static protected void parseAttributes(object $object, array $validators)

Parse array containing additional validators or attributes for FrameworkQuestion as strings along with any additional parameters that maybe expected.

Parameters

object $object

The instance of a class using the HasValidators trait.

array $validators

An array of validators. Any additional parameters must be separated with a ":".

Return Value

void

Exceptions

FrameworkException

in HasValidators at line 558
HasValidators required()

Ensures required input is entered.

Return Value

HasValidators

in HasValidators at line 572
protected void resetAfterValidation()

Resets fieldname and validators instance variables so duplicate messages are not displayed in forms.

Return Value

void

in HasValidators at line 582
HasValidators positive()

Enforces rule when input must a positive number.

Return Value

HasValidators

in HasValidators at line 596
HasValidators queue()

Validates if queue exists in database or redis.

Return Value

HasValidators

in HasValidators at line 621
HasValidators setValidator(callable $validator)

Adds validator to array of validators to be used.

Parameters

callable $validator

The anonymous function for a validator.

Return Value

HasValidators

in HasValidators at line 631
HasValidators special()

Enforces rule when input must contain at least one special character.

Return Value

HasValidators

in HasValidators at line 645
HasValidators tel()

Enforces rule where input must be a valid phone number.

Return Value

HasValidators

in HasValidators at line 669
HasValidators testFilterNotation()

Ensures response is in colon notation format.

Return Value

HasValidators

in HasValidators at line 690
static protected array tokens(string $data)

Split on commas (tolerate spaces), normalize to lowercase, drop empties.

Useful for cases where you have a comma separated string.

Parameters

string $data

Comma separated strings of values to be converted into an array.

Return Value

array

An array containing values originally found in comma separated string.

in HasValidators at line 703
HasValidators unique(array $attributes)

Checks if value already exists in a database.

Parameters

array $attributes

The model, includeDeleted flag, and list of additional fields.

Return Value

HasValidators

in HasValidators at line 747
HasValidators upper()

Enforces rule when input must contain at least one lower case character.

Return Value

HasValidators

in HasValidators at line 761
HasValidators url()

Enforce rule where input must be a valid URL.

Return Value

HasValidators

in HasValidators at line 777
protected bool validate(mixed $response)

Calls validator callbacks. This function also ensures validators don't bleed into next question if instance is reused.

Parameters

mixed $response

The user answer.

Return Value

bool

True if validation passed. Otherwise, we return false.

in Model at line 65
__construct()

Default constructor.

in Model at line 75
void afterDelete()

Called before delete.

Return Value

void

in Model at line 82
void afterSave()

Called before save.

Return Value

void

in Model at line 94
Model assign(array $params, array $list = [], string $assignmentFilter = self::BLACKLIST)

Update the object with an associative array.

Parameters

array $params

Take values from post array and assign values.

array $list

A list of values to blacklist or whitelist.

string $assignmentFilter

When set to ModelName::BLACKLIST the values in the $list array is blacklisted. When set to ModelName::WHITELIST they are whitelisted.

Return Value

Model

Report for whether or not the operation was successful.

in Model at line 117
void beforeDelete()

This runs before delete, needs to return a boolean

Return Value

void

Boolean value depending on results of operation.

at line 50
void beforeSave()

Called before save.

Return Value

void

in Model at line 132
static int count()

Retrieves number of rows affected or returned by the last query. This is a wrapper function for the DB::count function.

Return Value

int

The number of rows affected or returned by the last query.

in Model at line 141
stdClass data()

Grab object and if we just need data for smaller result set.

Return Value

stdClass

The data associated with an object.

in Model at line 162
bool delete()

Wrapper for database delete function. If not softDelete we set it.

If row is set to softDelete we call the database delete function.

Return Value

bool

$deleted True if delete operation is successful. Otherwise, we return false.

in Model at line 184
static array getColumns()

Gets columns from table.

Return Value

array

An array of objects where each one represents a column from a database table.

in Model at line 194
array getColumnsForSave()

Gets an associative array of field values for insert or updating.

Return Value

array

Associative array of fields from database and values from model object.

in Model at line 223
static DB getDb()

Returns an instance of the DB class.

Return Value

DB

$_db The instance of the DB class.

in Model at line 236
array getErrorMessages()

Displays error messages when form validation fails.

Return Value

array

An array that contains a list of items that failed form validation.

in Model at line 246
static protected array _fetchStyleParams(array $params)

Used to set default fetchStyle param.

Parameters

array $params

Query params.

Return Value

array

$params Updated params.

in Model at line 260
static array|bool find(array $params = [])

Wrapper for the find function that is found in the DB class.

Parameters

array $params

The values for the query. They are the fields of the table in our database. The default value is an empty array.

Return Value

array|bool

An array of objects returned from an SQL query.

in Model at line 274
static object|bool findById(int $id)

Get result from database by primary key ID.

Parameters

int $id

The ID of the row we want to retrieve from the database.

Return Value

object|bool

The row from a database.

in Model at line 290
static array|bool findByTable(string $table, array $params = [])

Performs a database query against a specified table using the same query-building logic as the default find method.

This method is useful when querying a different table than the one defined by the model's static::$_table property (e.g., joining or selecting from related tables).

Parameters

string $table

The name of the table to query (can include alias, e.g. 'cart_items AS items').

array $params

The query parameters such as columns, joins, conditions, bind, order, limit, etc.

Return Value

array|bool

An array of result objects if found, or false/empty array if no results.

in Model at line 306
static array findAllByUserId(int $user_id, array $params = [])

Retrieves list of all records within a table related to a user.

Parameters

int $user_id

The user ID.

array $params

Used to build conditions for database query. The default value is an empty array.

Return Value

array

The list of records that is returned from the database.

in Model at line 324
static object|bool findFirst(array $params = [])

Wrapper for the findFirst function that is found in the DB class.

Parameters

array $params

The values for the query. They are the fields of the table in our database. The default value is an empty array.

Return Value

object|bool

An array of object returned from an SQL query.

in Model at line 339
static int findTotal(array $params = [])

Returns number of records in a table. A wrapper function for findTotal function in DB class.

Parameters

array $params

The values for the query. They are the fields of the table in our database. The default value is an empty array.

Return Value

int

The number of records in a table.

in Model at line 354
bool insert(array $fields)

Wrapper for database insert function.

Parameters

array $fields

The field names and the respective values we will use to populate a database record. The default value is an empty array.

Return Value

bool

Report for whether or not the operation was successful.

in Model at line 366
bool isNew()

Checks if an object is a new insertion or an existing record.

Return Value

bool

Returns true if the record exists. Otherwise, we return false.

in Model at line 375
void onConstruct()

Runs when the object is constructed.

Return Value

void

in Model at line 383
protected void populateObjData(array|object $result)

Populates object with data.

Parameters

array|object $result

Results from a database query.

Return Value

void

in Model at line 398
DB query(string $sql, array $bind = [])

Wrapper for database query function.

Parameters

string $sql

The database query we will submit to the database.

array $bind

The values we want to bind in our database query. The default value is an empty array.

Return Value

DB

The results of the database query.

in Model at line 420
void runValidation(bool|string $param, array $validators = [], string|null $fieldName = null)

Checks if results of validation is true or false. If false, the $this->_validates boolean instance variable is set to false. There are two ways to provide parameters for validation. You can pass the results from chaining functions and use just one parameter. Or you can provide the value of the field's name attribute, an array of validators, and a value for how you want the name of the field represented in error messages. If this value is not provided then the name attribute is used in messaging.

Parameters

bool|string $param

The results of the validation operation or the name of the field to be tested.

array $validators

An array of validators and any attributes that affect validation behavior.

string|null $fieldName

The name of the field to be displayed in the error message.

Return Value

void

in Model at line 443
bool save()

Wrapper for update and insert functions. A failed form validation will cause this function to return false.

Return Value

bool

True if the update operation is successful. Otherwise, we return false.

in Model at line 484
static protected array _softDeleteParams(array $params)

Adds to the conditions to avoid getting soft deleted rows returned

Parameters

array $params

Defined parameters to search by.

Return Value

array

$params parameters with appended conditions for soft delete.

in Model at line 513
bool update(array $fields)

Wrapper for the update function found in the DB class.

Parameters

array $fields

The value of the fields we want to set for the database record. The default value is an empty array.

Return Value

bool

True if the update operation is successful. Otherwise, we return false.

in Model at line 543
static int updateWhere(array $fields, array $params)

Updates one or more rows in this model's underlying table using your framework's params-style conditions.

This method delegates to the DB::updateWhere() method and allows you to pass both the fields to update and a parameterized WHERE clause (using conditions and bind arrays just like find/findFirst).

Example:

Queue::updateWhere(
    ['reserved_at' => date('Y-m-d H:i:s')],
    ['conditions' => 'id = ?', 'bind' => [$jobId]]
);

Parameters

array $fields

Associative array of columns to update with their new values. Example: ['status' => 'processed', 'attempts' => 3]

array $params

Query parameters for the WHERE clause. Supports:

  • conditions (string|array): SQL conditions with placeholders.
  • bind (array): Values to bind to placeholders.

Return Value

int

The number of rows affected by the update.

in Model at line 553
bool validationPassed()

Getter function for the $_validates boolean instance variable.

Return Value

bool

$_validates is true if validation is successful and false if there is a failure.

in Model at line 564
void validator()

Function that is called on save. If validation fails the save function will not proceed. This function is just a signature and must be implemented by models that run form validation because since it is called from within this class.

Return Value

void

in HasTimestamps at line 28
void timeStamps()

Sets values for timestamp fields.

Return Value

void

at line 61
static bool|UserSessions getFromCookie()

Retrieves User Session information from cookie.

Return Value

bool|UserSessions

An object containing information about the current user's session. If a user session does not exist false is returned.