- var_dump: documentation ( source)
- preg_match: documentation ( source)
<?php
readonly class FormattedString {
private string $value;
private function __construct (string $str) {
$this->value = $str;
}
public static function create (string $str): self|NotValidFormattedString {
$matches = null;
preg_match("/[a-z]{2}-[a-z]{2}-[a-z]{8}-[a-z]{1}/", $str, $matches);
if (count($matches) === 0) {
return new NotValidFormattedString($str, "poshel nahoi suka, chto za govno ti mne peredal, blyad");
}
return new self($str);
}
}
readonly class NotValidFormattedString {
public function __construct (public string $str, public string $err) {}
}
class ValidateFormattedString {
public function __invoke(string $str): FormattedString|NotValidFormattedString {
return FormattedString::create($str);
}
}
class SuperYellowCircle {
public function __construct (public FormattedString $toChtoVMoemPrimereByloClassomYoba) {}
}
$stringOne = (new ValidateFormattedString)("aa-bb-cccccccc-d");
$circleOne = new SuperYellowCircle($stringOne);
var_dump($circleOne);
$stringTwo = (new ValidateFormattedString)("butthurt");
$circleTwo = new SuperYellowCircle($stringTwo);
var_dump($circleTwo);