A page's PHP spends much of its time asking whether a value is there, picking a default
when it is not, and guarding the parts that may fail. These helpers say each of those in one
call - the functions Laravel knows as blank(), filled(),
value(), transform(), tap(), retry(),
rescue() and once(). They are loaded on every request, like the
rest of pad/lib/.
| function | what it does |
|---|---|
padBlank ( $value ) | TRUE for NULL, '' or only whitespace, an empty array, an empty Countable - FALSE for 0, '0', FALSE and every other value |
padFilled ( $value ) | the opposite of padBlank |
padValue ( $value, ...$args ) | a Closure called with the arguments, anything else answered as it is |
padTransform ( $value, $callback, $default ) | the callback's answer for a filled value, the default for a blank one |
padTap ( $value, $callback ) | hands the value to the callback and answers the value itself |
padRetry ( $times, $callback, $sleepMilliseconds, $when ) | calls the callback until it stops throwing, at most $times times |
padRescue ( $callback, $rescue, $report ) | the callback's answer, or the rescue value when it throws |
padOnce ( $callback ) | runs the callback once per request for the line that calls it |
PHP's empty() calls a quantity of '0' empty and a remark of a
few spaces filled. padBlank is the test a page means: nothing there is NULL,
text of nothing but whitespace (a non-breaking space too), an empty array and an empty
Countable. A number and a boolean are always values.
|
PHP fragments/values_1.php |
HTML fragments/values_1.pad |
Result | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
|
padTransform runs the callback for a filled value only, so the callback
never has to test for nothing; a blank value gets the default.padValue answers a value, or calls a Closure for it: a default that is
costly to make is written as a Closure and made only when it is needed. A string such as
'date' is a value, never the name of a function to call.padTap hands a value to a callback - to note it, to check it - and goes on
with the value itself, whatever the callback answers.|
PHP fragments/values_2.php |
HTML fragments/values_2.pad |
Result |
|---|---|---|
|
|
Ann Smith - no nickname - somewhere Total 50 (noted 50) |
padRetry calls the callback with the number of the attempt until it answers
without throwing. Between two attempts it waits $sleepMilliseconds - one number,
or a list with a wait per attempt whose last one repeats. $when, a callable given
the exception, decides whether another attempt is worth it; when it says no, or after the
last attempt, the exception is thrown on.
padRescue lets a part of a page fail alone: when the callback throws - an
Exception or an Error - the answer is the rescue value, a Closure given the exception.
With $report (on unless it is FALSE) the failure is written to PHP's error log,
so a rescued fault is not lost.
|
PHP fragments/values_3.php |
HTML fragments/values_3.pad |
Result |
|---|---|---|
|
|
EUR 1 = USD 1.17, on attempt 3 No forecast today - the weather service is down |
padOnce runs its callback the first time its line is reached in a request,
and answers that first result each later time - in a loop, or in a function several parts of
the page call. A callback that throws gave no result, so the next call runs it again. The
place is the file and line of the call: two padOnce calls on one line share their
result.
|
PHP fragments/values_4.php |
HTML fragments/values_4.pad |
Result |
|---|---|---|
|
|
EUR 120 EUR 75 EUR 310 The settings were read 1 time. |
A callback that is no function - a misspelled name - and a number of attempts below 1 are reported as errors that name the function; the function then answers NULL.