Skip to content

BoolFormat

The spelling a boolean is written in: words, a digit, a letter or a label.

A true-or-false value has no single written form. JSON and XML want the lowercase words true and false; a database flag column wants 1 and 0; a legacy CHAR(1) column holds Y and N; a screen shows Yes and No. The capitalised spelling, True, suits none of those — it is not valid JSON, and an XML boolean rejects it. BoolFormat names the spelling, so the writer never guesses.

Members

Member True / false Meant for
Lowercase 'true' / 'false' JSON, XML (xs:boolean), JavaScript, URLs. This is the default.
Capitalized 'True' / 'False' The capitalised spelling — what CkText::BoolToString writes.
OneZero '1' / '0' A numeric flag: SQL BIT and TINYINT columns, CSV exports.
YN 'Y' / 'N' A legacy CHAR(1) flag column.
YesNo 'Yes' / 'No' A label for people.

The enum is backed by an int — Lowercase is 0, Capitalized 1, OneZero 2, YN 3, YesNo 4 — so a choice can be stored and read back with BoolFormat::from().

Where it is used

CkBool::Format takes a format parameter. The default is Lowercase (CkSettings::DefaultBoolFormat). Whichever spelling you write, the readers on CkBool read it back as the same boolean.

Quick look

use SafeCoder\CommonKit\CkBool;
use SafeCoder\CommonKit\Enums\BoolFormat;

CkBool::Format(true);                  // -> 'true'  (Lowercase is the default)

CkBool::Format(
    input: true,
    format: BoolFormat::OneZero
);                                     // -> '1'

CkBool::Format(
    input: 'no',
    format: BoolFormat::YesNo
);                                     // -> 'No'

CkBool::Format(
    input: 'maybe',
    format: BoolFormat::YN
);                                     // -> ''  (unreadable: rejected, in every spelling)