Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: update pint config #21

Merged
merged 2 commits into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion pint.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
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');
}
}
Loading