Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rawilk committed Mar 10, 2024
1 parent 74ae9fd commit 76e5301
Showing 1 changed file with 33 additions and 33 deletions.
66 changes: 33 additions & 33 deletions src/Support/Name.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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');
}
}

0 comments on commit 76e5301

Please sign in to comment.