Logic

Reference for the Logic module: boolean combinators and callback-based conditionals. Or over two, three or four booleans; If and IfNot; value and callback selection; a switch and case pair; and optional-value helpers that run a callback based on whether a value is null.

This page is a pure reference. For how the framework fits together, see the Guide.

Classes
  • Logic - boolean combinators and callback-based conditionals, selection, switch and optional-value helpers.

Logic

A single class of static helpers. Or, Or3 and Or4 combine booleans. If, IfNot, Select and SelectValue branch on a condition, running a callback or returning a value. Switch and Case form a callback-based switch. Optional and OptionalNot branch on whether a value is null.

Case

Logic::Case(mixed $caseValue, Callable $caseCallback): void

Run the callback when the case value strictly equals the current switch value set by Switch.

NameTypeDescription
$caseValuemixedThe value to compare against the current switch value.
$caseCallbackCallableThe callback run when the values match.

Returns void

If

Logic::If(bool $condition, Callable $onTrue): void

Run the callback when the condition is true.

NameTypeDescription
$conditionboolThe condition to test.
$onTrueCallableThe callback run when the condition is true.

Returns void

IfNot

Logic::IfNot(bool $condition, Callable $onFalse): void

Run the callback when the condition is false.

NameTypeDescription
$conditionboolThe condition to test.
$onFalseCallableThe callback run when the condition is false.

Returns void

Optional

Logic::Optional($optionalValue, Callable $onPresent): void

Run the callback when the value is not null, passing the value to it.

NameTypeDescription
$optionalValuemixedThe value that may be null.
$onPresentCallableThe callback run with the value when it is not null.

Returns void

OptionalNot

Logic::OptionalNot($optionalValue, Callable $onNull): void

Run the callback when the value is null.

NameTypeDescription
$optionalValuemixedThe value that may be null.
$onNullCallableThe callback run when the value is null.

Returns void

Or

Logic::Or(bool $a, bool $b): bool

True when either of two booleans is true.

NameTypeDescription
$aboolThe first value.
$bboolThe second value.

Returns bool True when either value is true.

Or3

Logic::Or3(bool $a, bool $b, bool $c): bool

True when any of three booleans is true.

NameTypeDescription
$aboolThe first value.
$bboolThe second value.
$cboolThe third value.

Returns bool True when any value is true.

Or4

Logic::Or4(bool $a, bool $b, bool $c, bool $d): bool

True when any of four booleans is true.

NameTypeDescription
$aboolThe first value.
$bboolThe second value.
$cboolThe third value.
$dboolThe fourth value.

Returns bool True when any value is true.

Select

Logic::Select(bool $condition, Callable $onTrue, Callable $onFalse): void

Run one of two callbacks depending on the condition.

NameTypeDescription
$conditionboolThe condition to test.
$onTrueCallableThe callback run when the condition is true.
$onFalseCallableThe callback run when the condition is false.

Returns void

SelectValue

Logic::SelectValue(bool $condition, mixed $onTrue, mixed $onFalse): mixed

Return one of two values depending on the condition.

NameTypeDescription
$conditionboolThe condition to test.
$onTruemixedThe value returned when the condition is true.
$onFalsemixedThe value returned when the condition is false.

Returns mixed The chosen value.

Switch

Logic::Switch(mixed $value, Callable $casesCallback): void

Set the current switch value, run the callback that declares the cases, then restore the previous switch value.

NameTypeDescription
$valuemixedThe value the cases are compared against.
$casesCallbackCallableThe callback that declares the cases, typically calling Case.

Returns void