\duncan3dc\HelpersHelper

Summary

Methods
Properties
Constants
getOptions()
getAnyOptions()
toString()
toArray()
cleanupArray()
date()
dateDiff()
url()
getBestDivisor()
createPassword()
checkPassword()
curl()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
No private properties found
N/A

Methods

getOptions()

getOptions(array $userSpecified, array $defaults) : array

Simulate named arguments using associative arrays.

Basically just merge the two arrays, giving user specified options the preference. Also ensures that each paramater in the user array is valid and throws an exception if an unknown element is found.

Parameters

array $userSpecified

The array of options passed to the function call

array $defaults

The default options to be used

Returns

array

getAnyOptions()

getAnyOptions(array $userSpecified, array $defaults) : array

This is a safe version of the getOptions() method.

It allows any custom option key in the userSpecified array.

Parameters

array $userSpecified

The array of options passed to the function call

array $defaults

The default options to be used

Returns

array

toString()

toString(mixed $data) : string|array<mixed,string>

Ensure that the passed parameter is a string, or an array of strings.

Parameters

mixed $data

The value to convert to a string

Returns

string|array<mixed,string>

toArray()

toArray(mixed $value) : array

Ensure that the passed parameter is an array.

If it is a truthy value then make it the sole element of an array.

Parameters

mixed $value

The value to convert to an array

Returns

array

cleanupArray()

cleanupArray(array $array) : array

Run each element value through trim() and remove any elements that are falsy.

Parameters

array $array

The array to cleanup

Returns

array

date()

date(string $format, string|integer $date, string|integer $time) : integer|string

Extension to the standard date() function to handle many more formats.

Returns zero if any step of the parsing fails.

Parameters

string $format

The format to apply to the date

string|integer $date

The date to parse

string|integer $time

The time to parse

Returns

integer|string

dateDiff()

dateDiff(string|integer $from, string|integer $to) : integer|null

Compare two dates and return an integer representing their difference in days.

Returns null if any of the parsing fails.

Parameters

string|integer $from

The first date to parse

string|integer $to

The second date to parse

Returns

integer|null

url()

url(string $url, array $params) : string

Append parameters on a url (adding a question mark if none is present).

Parameters

string $url

The base url

array $params

An array of parameters to append

Returns

string

getBestDivisor()

getBestDivisor(integer $rows, array $options) : integer

Calculate the most reasonable divisor for a total.

Useful for repeating headers in a table with many rows.

$options:

  • int "min" The minimum number of rows to allow before breaking (default: 5)
  • int "max" The maximum number of rows to allow before breaking (default: 10)

Parameters

integer $rows

The total number to calculate from (eg, total number of rows in a table)

array $options

An array of options (see above)

Returns

integer

createPassword()

createPassword(array $options) : string

Generate a password.

$options:

  • array "bad" Characters that should not be used as they are ambigious (default: [1, l, I, 5, S, 0, O, o])
  • array "exclude" Other characters that should not be used (default: [])
  • int "length" The length of the password that should be generated (default: 10)
  • bool "lowercase" Include lowercase letters (default: true)
  • bool "uppercase" Include uppercase letters (default: true)
  • bool "numbers" Include numbers (default: true)
  • bool "specialchars" Include special characters (default: true)

Parameters

array $options

An array of options (see above)

Returns

string

checkPassword()

checkPassword(string $password, array $options) : array<mixed,string>

Check if a password conforms to the specified complexitiy rules.

If the password passes all tests then the function returns an empty array. Otherwise it returns an array of all the checks that failed.

$options:

  • int "length" The minimum length the password must be (default: 8)
  • int "unique" The number of unique characters the password must contain (default: 4)
  • bool "lowercase" Whether the password must contain lowercase letters (default: true)
  • bool "uppercase" Whether the password must contain uppercase letters (default: true)
  • bool "alpha" Whether the password must contain letters (default: true)
  • bool "numbers" Whether the password must contain numbers (default: true)

Parameters

string $password

The password to check

array $options

An array of options (see above)

Returns

array<mixed,string>

curl()

curl(string|array $options, string|array $body) : string|array

Simple wrapper for curl.

$options:

  • string "url" The url to request
  • array "headers" An array of key/value pairs of headers to send
  • int "connect" CURLOPT_CONNECTTIMEOUT (default: 0)
  • int "timeout" CURLOPT_TIMEOUT (default: 0)
  • bool "follow" CURLOPT_FOLLOWLOCATION (default: true)
  • bool "verifyssl" CURLOPT_SSL_VERIFYPEER (default: true)
  • string "cookies" The file to use for both CURLOPT_COOKIEFILE and CURLOPT_COOKIEJAR
  • bool "put" Set to try to send the $body parameter as the contents of a put request
  • string "custom" CURLOPT_CUSTOMREQUEST
  • bool "nobody" CURLOPT_NOBODY
  • string "useragent" CURLOPT_USERAGENT
  • bool "returnheaders" CURLOPT_HEADER (default: false)
  • array "curlopts" Any extra curlopt constants (as the keys) and the values to use (as the values)

Parameters

string|array $options

Can either be a url to use with the default options, or an array of options (see above)

string|array $body

Content to send in the body of the request, if an array is passed it will be run through http_build_query()

Returns

string|array —

If "returnheaders" is false then the body of the response is returned as a string, otherwise an array of data about the response is available