Skip to content
This repository was archived by the owner on Nov 26, 2024. It is now read-only.

Commit 0a3fba4

Browse files
committed
Update readme on helpers section
1 parent 346a2de commit 0a3fba4

File tree

1 file changed

+132
-0
lines changed

1 file changed

+132
-0
lines changed

README.md

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,138 @@ There's some commands area ready built-in. Others, may refer to respective packa
6060
- `reload:db` - Run `migrate:fresh --seed` with `profile:seed`. You may extend the usage.
6161
- `reload:cache` - Recache everything
6262

63+
### Helpers
64+
65+
**generate_sequence($count)**
66+
67+
Generate sequence with leading zero like `000001`, `000002` and so on.
68+
69+
Length for the leading zero can be configure in `.env` file - `DOCUMENT_SEQUENCE_LENGTH`.
70+
71+
**abbrv($word, $unique_characters)**
72+
73+
A helper to create abbreviation by removing non-alphanumeric, vowels.
74+
75+
Passing second argument as false will return non-unique characters - means that you will see repeatative characters.
76+
77+
Following are the sample usage and output:
78+
79+
```
80+
>>> abbrv("Cleanique Coders")
81+
=> "CLNQDRS"
82+
>>> abbrv("Cleanique Coders", false)
83+
=> "CLNQCDRS"
84+
```
85+
86+
**generate_reference($prefix, $count)**
87+
88+
Generate document reference string. Usually a document have their own reference on the particular date event.
89+
90+
For instance a payslip document have a reference number / string like `CCR/PYSL/2018/01/000001`.
91+
92+
The format generate is `abbrv/Year/Month/Date/reference_id`.
93+
94+
You can use in two ways:
95+
96+
```
97+
>>> generate_reference("Cleanique Coders Payroll")
98+
=> "CLNQ/CDRS/PYRL/2018/02/04/TBIPOU"
99+
>>> generate_reference("Cleanique Coders Payroll", 10)
100+
=> "CLNQ/CDRS/PYRL/2018/02/04/000010"
101+
```
102+
103+
The length of the `TBIPOU` or `000010` is depends on `DOCUMENT_SEQUENCE_LENGTH` in `.env` file.
104+
105+
**hashids($salt, $length, $alphabet)**
106+
107+
It's essential to have important resources to use other than just incremental id. One of the option is to use [Hashids](https://github.com/ivanakimov/hashids.php).
108+
109+
Following are an example how to use the helper.
110+
111+
```
112+
>>> hashids()->encode(1)
113+
=> "yR5ajG4DBwlz"
114+
>>> hashids('random-salt')->encode(1)
115+
=> "WZvoOMBr19YN"
116+
>>> hashids('random-salt', 24)->encode(1)
117+
=> "GPz1W4mLavAeqAwB2XZbOgQn"
118+
>>> hashids('random-salt', 6)->encode(1)
119+
=> "5qy0lP"
120+
>>> hashids('random-salt', 6, 'qwertyuiopasdfghjkl')->encode(1)
121+
=> "orgpqe"
122+
>>> hashids('random-salt', 6, ',./;<>?:"{}|[]\-=`~!@#$%^&*()')->encode(1)
123+
=> "{&(^&-"
124+
>>> hashids('random-salt', 12, ',./;<>?:"{}|[]\-=`~!@#$%^&*()')->encode(1)
125+
=> "~-!\`-&(*[{)"
126+
```
127+
128+
For `$salt`, by default it will use `APP_KEY`, but you may override it by passing the salt name at the first argument.
129+
130+
For `$length`, you can pass any length of hashids you want to create. By default it's 12 characters.
131+
132+
For `$alphabet`, you can put any characters as per example above. But remember, the alphabets need to be minimum of **16 characters**.
133+
134+
**str_slug_fqcn($object)**
135+
136+
Return slug name for given object.
137+
138+
```
139+
>>> $user = \App\Models\User::first();
140+
=> App\Models\User {#1013
141+
id: 1,
142+
hashslug: "krOErpkv6EVR",
143+
slug: "elza-bins",
144+
name: "Elza Bins",
145+
146+
deleted_at: null,
147+
created_at: "2018-02-04 03:11:07",
148+
updated_at: "2018-02-04 03:11:07",
149+
}
150+
>>> str_slug_fqcn($user)
151+
=> "app-models-user"
152+
```
153+
154+
**audit($model, $message, $causedBy)**
155+
156+
Simply record audit trail on given `$model`, with proper `$message`. You can optionally passed the third argument - `$causedBy`.
157+
158+
```
159+
>>> auth()->loginUsingId(1)
160+
>>> $user = \App\Models\User::first();
161+
>>> audit($user, 'access via terminal')
162+
>>> auth()->user()->activity->toArray()
163+
=> [
164+
[
165+
"id" => 4,
166+
"log_name" => "default",
167+
"description" => "User successfully logged in.",
168+
"subject_id" => 1,
169+
"subject_type" => "App\Models\User",
170+
"causer_id" => 1,
171+
"causer_type" => "App\Models\User",
172+
"properties" => Illuminate\Support\Collection {#971
173+
all: [],
174+
},
175+
"created_at" => "2018-02-04 03:18:50",
176+
"updated_at" => "2018-02-04 03:18:50",
177+
],
178+
[
179+
"id" => 5,
180+
"log_name" => "default",
181+
"description" => "access via terminal",
182+
"subject_id" => 1,
183+
"subject_type" => "App\Models\User",
184+
"causer_id" => 1,
185+
"causer_type" => "App\Models\User",
186+
"properties" => Illuminate\Support\Collection {#980
187+
all: [],
188+
},
189+
"created_at" => "2018-02-04 03:19:16",
190+
"updated_at" => "2018-02-04 03:19:16",
191+
],
192+
]
193+
```
194+
63195
## Test
64196

65197
To run the test, type `vendor/bin/phpunit` in your terminal.

0 commit comments

Comments
 (0)