Skip to content

TextCase

The casing to apply while a text value is being cleaned.

Data arrives shouted ('JOHN DOE'), whispered ('john doe'), or somewhere in between. A TextCase parameter tells the cleaner what to do about it. Every option uses the invariant culture — a fixed, language-neutral rule set, so Turkish dotted-I rules never apply no matter where the code runs, and no setlocale() call changes the answer. The title-, sentence- and first-letter options lower-case the value first and then re-case it (TitlecaseUnlessMixed only when the value is not already mixed-case), while Uppercase and Lowercase map it directly.

Members

Member Meaning
None Leave the casing exactly as it is. The default everywhere except Name.
Lowercase Lower-case the whole value: 'JOHN DOE' becomes 'john doe'.
Uppercase Upper-case the whole value: 'john doe' becomes 'JOHN DOE'.
Titlecase Lower-case the value, then upper-case the first letter of every word. Spaces, hyphens, periods and underscores are word boundaries, and so is an apostrophe followed by two or more letters — "o'brien-smith" becomes "O'Brien-Smith", while "mcdonald's" becomes "Mcdonald's".
Sentencecase Lower-case the value, then upper-case the first letter of every sentence. A sentence ends at a line break, or at ., !, ? or : followed by a space — 'HELLO THERE. how are you?' becomes 'Hello there. How are you?', while 'visit example.com' stays 'Visit example.com'.
Capitalize Lower-case the value and upper-case its first letter only, ignoring any sentence breaks: 'JOHN DOE. jane' becomes 'John doe. jane'. Identical to Sentencecase for single-sentence input.
TitlecaseUnlessMixed Title-case the value only when it arrives entirely upper-case or entirely lower-case: 'JOHN MCDONALD' becomes 'John Mcdonald', while 'John McDonald' is left exactly as typed — a mix of cases is taken as deliberate. Otherwise identical to Titlecase. The default for Name.

The enum is backed by an int — None is 0, Lowercase 1, Uppercase 2, Titlecase 3, Sentencecase 4, Capitalize 5, TitlecaseUnlessMixed 6 — so a choice can be stored and read back with TextCase::from().

Where it is used

Four cleaners on CkText take a textCase parameter: String, ToStringOrNull, Name and Address. The default is None — casing is left alone — except on Name, where it is TitlecaseUnlessMixed, because names are the one place where re-casing shouted input is almost always what you want.

Quick look

use SafeCoder\CommonKit\CkText;
use SafeCoder\CommonKit\Enums\TextCase;

CkText::String('JOHN DOE');            // -> 'JOHN DOE'  (None is the default: casing kept)

CkText::String(
    input: 'JOHN DOE',
    textCase: TextCase::Lowercase
);                                     // -> 'john doe'

CkText::String(
    input: "o'brien-smith",
    textCase: TextCase::Titlecase
);                                     // -> "O'Brien-Smith"

CkText::Name('JOHN MCDONALD');         // -> 'John Mcdonald'  (Name defaults to TitlecaseUnlessMixed)
CkText::Name('John McDonald');         // -> 'John McDonald'  (mixed case is deliberate: kept as typed)