diff --git a/pint.json b/pint.json index 5b7dcca..2e326cc 100644 --- a/pint.json +++ b/pint.json @@ -7,6 +7,36 @@ "types_spaces": { "space": "none" }, - "single_trait_insert_per_statement": true + "combine_consecutive_issets": true, + "combine_consecutive_unsets": true, + "declare_parentheses": true, + "declare_strict_types": true, + "explicit_string_variable": true, + "single_trait_insert_per_statement": true, + "ordered_class_elements": { + "order": [ + "use_trait", + "case", + "constant", + "constant_public", + "constant_protected", + "constant_private", + "property_public", + "property_protected", + "property_private", + "construct", + "destruct", + "magic", + "phpunit", + "method_abstract", + "method_public_static", + "method_public", + "method_protected_static", + "method_protected", + "method_private_static", + "method_private" + ], + "sort_algorithm": "none" + } } } diff --git a/src/Support/Name.php b/src/Support/Name.php index eb21176..9f6e354 100644 --- a/src/Support/Name.php +++ b/src/Support/Name.php @@ -28,6 +28,30 @@ */ class Name implements Castable, Jsonable, JsonSerializable { + public function __construct(protected ?string $firstName, protected ?string $lastName = null) + { + } + + public function __get(string $key): ?string + { + if ($this->wantsPossessive($key)) { + $key = Str::replaceLast('possessive', '', $key); + + return $this->possessive($this->{$key}); + } + + if (method_exists($this, $method = Str::studly($key))) { + return $this->{$method}(); + } + + return null; + } + + public function __toString(): string + { + return (string) $this->full(); + } + public static function from(?string $name): self { $parts = explode(' ', trim($name), 2); @@ -39,8 +63,9 @@ public static function from(?string $name): self return new static(Arr::get($parts, 0), $lastName); } - public function __construct(protected ?string $firstName, protected ?string $lastName = null) + public static function castUsing(array $arguments): CastsAttributes { + return new NameCast(...$arguments); } public function first(): ?string @@ -95,48 +120,23 @@ public function initials(): ?string ->join(''); } - protected function possessive(string $name): string - { - return sprintf("%s'%s", $name, (Str::endsWith($name, 's') ? null : 's')); - } - - protected function wantsPossessive(string $key): bool - { - return Str::endsWith($key, 'possessive'); - } - - public function __get(string $key): ?string + public function toJson($options = 0): string { - if ($this->wantsPossessive($key)) { - $key = Str::replaceLast('possessive', '', $key); - - return $this->possessive($this->{$key}); - } - - if (method_exists($this, $method = Str::studly($key))) { - return $this->{$method}(); - } - - return null; + return json_encode($this->jsonSerialize(), $options); } - public function __toString(): string + public function jsonSerialize(): string { return (string) $this->full(); } - public static function castUsing(array $arguments): CastsAttributes - { - return new NameCast(...$arguments); - } - - public function toJson($options = 0): string + protected function possessive(string $name): string { - return json_encode($this->jsonSerialize(), $options); + return sprintf("%s'%s", $name, (Str::endsWith($name, 's') ? null : 's')); } - public function jsonSerialize(): string + protected function wantsPossessive(string $key): bool { - return (string) $this->full(); + return Str::endsWith($key, 'possessive'); } }