Skip to content

Commit a26a0ab

Browse files
committed
Document changes
1 parent 35f266d commit a26a0ab

File tree

4 files changed

+171
-5
lines changed

4 files changed

+171
-5
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ protected $casts = [
1717

1818
// Encrypts on write, decrypts and typecasts to integer on read.
1919
'secret_number' => Encrypted::class . ':integer',
20+
21+
// Provides utilities for manipulating a name
22+
'name' => Name::class,
2023
];
2124
```
2225

@@ -60,6 +63,7 @@ Please review [my security policy](.github/SECURITY.md) on how to report securit
6063
Some alternatives to this package include:
6164

6265
- [crudly/encrypted](https://github.com/Crudly/Encrypted)
66+
- [watson/nameable](https://github.com/dwightwatson/nameable)
6367

6468
## License
6569

docs/introduction.md

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ protected $casts = [
1818

1919
// Encrypts on write, decrypts and typecasts to integer on read.
2020
'secret_number' => Encrypted::class . ':integer',
21+
22+
// Provides utilities for manipulating a name
23+
'name' => Name::class,
2124
];
2225
```
2326

@@ -26,3 +29,4 @@ protected $casts = [
2629
Some alternatives to this package include:
2730

2831
- [crudly/encrypted](https://github.com/Crudly/Encrypted)
32+
- [watson/nameable](https://github.com/dwightwatson/nameable)

docs/requirements.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ sort: 2
66
## General Requirements
77

88
- PHP **7.4** or greater
9-
- Laravel **7.28** or greater
9+
- Laravel **8.0** or greater
1010

1111
## Version Matrix
1212

13-
| Laravel | Minimum Version |
14-
| --- | --- |
15-
| 7.28 | 1.0.0 |
16-
| 8.0 | 1.0.0 |
13+
| Laravel | Minimum Version | Maximum Version |
14+
| --- | --- | --- |
15+
| 7.28 | 1.0.0 | 1.0.1 |
16+
| 8.0 | 1.0.0 | |

docs/usage/name.md

+158
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
---
2+
title: Name
3+
sort: 3
4+
---
5+
6+
## Introduction
7+
8+
The name cast provides a cast/formatter for presenting your user's names. It can get a user's first, last or full name,
9+
their initials, and common abbreviations. This cast will also allow you to store a user's name in a single column and
10+
fetch what you need.
11+
12+
This cast is based on the [nameable](https://github.com/dwightwatson/nameable) cast by [@dwightwatson](https://github.com/dwightwatson).
13+
14+
## Usage
15+
16+
Add either the `NameCast` or `Name` class as a cast on your model's name field. Using either class will give the same result.
17+
18+
```php
19+
<?php
20+
21+
namespace App\Models;
22+
23+
use Illuminate\Database\Eloquent\Model;
24+
use Rawilk\LaravelCasters\Casts\NameCast;
25+
use Rawilk\LaravelCasters\Support\Name;
26+
27+
class User extends Model
28+
{
29+
protected $casts = [
30+
'name' => Name::class,
31+
32+
// Or
33+
// 'name' => NameCast::class,
34+
];
35+
}
36+
```
37+
38+
## Manipulations
39+
40+
There are several manipulations you can perform on a `Name` instance. Below is a demonstration of each of them
41+
using the `User` model defined above:
42+
43+
```php
44+
$user = new User(['name' => 'John Smith']);
45+
46+
echo $user->name->full; // 'John Smith'
47+
echo $user->name->first; // 'John'
48+
echo $user->name->last; // 'Smith'
49+
echo $user->name->familiar; // 'John S.'
50+
echo $user->name->abbreviated; // 'J. Smith'
51+
echo $user->name->sorted; // 'Smith, John'
52+
echo $user->name->initials; // 'JS'
53+
```
54+
55+
There are also possessive variants you can use which will even work with names that end in `s`:
56+
57+
```php
58+
$user = new User(['name' => 'John Doe']);
59+
60+
echo $user->name->full_possessive; // John Doe's
61+
echo $user->name->first_possessive; // John's
62+
echo $user->name->last_possessive; // Doe's
63+
echo $user->name->abbreviated_possessive; // J. Doe's
64+
echo $user->name->sorted_possessive; // Doe, John's
65+
echo $user->name->initials_possessive; // JD's
66+
67+
$user = new User(['name' => 'Angus Young']);
68+
69+
$user->name->full_possessive; // Angus Young's
70+
$user->name->first_possessive; // Angus'
71+
```
72+
73+
If a user doesn't provide a full name (for example, just a first name) the attributes will just omit the last name.
74+
75+
## Casting from multiple fields
76+
77+
If you prefer to separate your user's first and last names in the database, this cast will still work as well.
78+
You can create a "name" field at runtime on your model from your model's first and last name attributes.
79+
80+
If you have a `first_name` and `last_name` attribute on your model, you can cast it like this:
81+
82+
```php
83+
<?php
84+
85+
namespace App\Models;
86+
87+
use Illuminate\Database\Eloquent\Model;
88+
use Rawilk\LaravelCasters\Support\Name;
89+
90+
class User extends Model
91+
{
92+
// $fillable not needed, just here to show the user has those fields...
93+
protected $fillable = ['first_name', 'last_name'];
94+
95+
protected $casts = [
96+
'name' => Name::class,
97+
];
98+
}
99+
```
100+
101+
Now when you create a new User model with a first and last name, it will be able to grab them and instantiate a new `Name` instance from them.
102+
103+
```php
104+
$user = new User(['first_name' => 'John', 'last_name' => 'Smith']);
105+
106+
echo $user->name->full; // 'John Smith'
107+
echo $user->name->initials; // 'JS'
108+
```
109+
110+
### Using custom first and last name attribute names
111+
112+
If you have your first or last name attributes named something different in the database, you can specify the column names in the
113+
cast definition:
114+
115+
```php
116+
<?php
117+
118+
namespace App\Models;
119+
120+
use Illuminate\Database\Eloquent\Model;
121+
use Rawilk\LaravelCasters\Support\Name;
122+
123+
class User extends Model
124+
{
125+
protected $casts = [
126+
'name' => Name::class . ':given_name,family_name',
127+
];
128+
}
129+
```
130+
131+
Now the name cast will use the `given_name` attribute as the user's "first_name", and
132+
the `family_name` attribute as the user's "last_name" when casting the name attribute.
133+
134+
```php
135+
$user = new User(['given_name' => 'John', 'family_name' => 'Smith']);
136+
137+
echo $user->name->full; // 'John Smith'
138+
```
139+
140+
## Using Without a Model
141+
142+
You can alternatively use the `Name` class without it being a cast on a model. Just provide a name to it directly:
143+
144+
```php
145+
<?php
146+
147+
namespace App;
148+
149+
use Rawilk\LaravelCasters\Support\Name;
150+
151+
$name = new Name('Randall', 'James Wilk');
152+
153+
// or
154+
$name = Name::from('Randall James Wilk');
155+
156+
echo $name->full; // Randall James Wilk
157+
echo $name->initials; // RJW
158+
```

0 commit comments

Comments
 (0)