• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..--

src/19-Apr-2024-1,7761,171

composer.jsonD18-Apr-2024456 2121

readme.mdD18-Apr-202412.9 KiB729624

readme.md

1## Laravel 5 Helpers for Non-Laravel Projects
2
3This project takes the useful [Laravel helper functions](http://laravel.com/docs/5.0/helpers) and allows you to use them in Non-Laravel projects.
4
5All dependencies have been extracted out to a single helpers file. No need to import half of Symphony and Laravel to make these work.
6
7## License
8
9Since the Laravel framework is open-sourced software licensed under the [MIT license](https://opensource.org/licenses/MIT), this project is licensed under the same license.
10
11## Setup
12
13Run the following in your project root:
14
15    composer require rappasoft/laravel-helpers
16
17## Documentation
18
19* [Arrays](#arrays)
20    * [append_config](#append_config)
21    * [array_add](#array_add)
22    * [array_build](#array_build)
23    * [array_divide](#array_divide)
24    * [array_dot](#array_dot)
25    * [array_except](#array_except)
26    * [array_fetch](#array_fetch)
27    * [array_first](#array_first)
28    * [array_last](#array_last)
29    * [array_flatten](#array_flatten)
30    * [array_forget](#array_forget)
31    * [array_get](#array_get)
32    * [array_has](#array_has)
33    * [array_only](#array_only)
34    * [array_pluck](#array_pluck)
35    * [array_pull](#array_pull)
36    * [array_set](#array_set)
37    * [array_where](#array_where)
38    * [head](#head)
39    * [last](#last)
40* [Strings](#strings)
41    * [ascii](#ascii)
42    * [camel_case](#camel_case)
43    * [charsArray](#charsArray)
44    * [ends_with](#ends_with)
45    * [preg_replace_sub](#preg_replace_sub)
46    * [snake_case](#snake_case)
47    * [starts_with](#starts_with)
48    * [str_contains](#str_contains)
49    * [str_finish](#str_finish)
50    * [str_is](#str_is)
51    * [str_limit](#str_limit)
52    * [str_random](#str_random)
53    * [str_replace_array](#str_replace_array)
54    * [str_slug](#str_slug)
55    * [studly_case](#studly_case)
56* [Classes](#classes)
57    * [class_basename](#class_basename)
58    * [class_uses_recursive](#class_uses_recursive)
59    * [trait_uses_recursive](#trait_uses_recursive)
60* [Misc.](#misc)
61    * [data_get](#data_get)
62    * [data_set](#data_set)
63    * [data_fill](#data_fill)
64    * [e](#e)
65    * [object_get](#object_get)
66    * [value](#value)
67    * [with](#with)
68    * [dd](#dd)
69
70## Arrays
71<a name="arrays"/>
72
73### append_config
74<a name="append_config"/>
75
76```php
77/**
78	 * Assign high numeric IDs to a config item to force appending.
79	 *
80	 * @param  array  $array
81	 * @return array
82*/
83function append_config(array $array)
84```
85
86### array_add
87<a name="array_add"/>
88
89```php
90/**
91	 * Add an element to an array using "dot" notation if it doesn't exist.
92	 *
93	 * @param  array   $array
94	 * @param  string  $key
95	 * @param  mixed   $value
96	 * @return array
97*/
98function array_add($array, $key, $value)
99```
100
101### array_build
102<a name="array_build"/>
103
104```php
105/**
106	 * Build a new array using a callback.
107	 *
108	 * @param  array     $array
109	 * @param  \Closure  $callback
110	 * @return array
111*/
112function array_build($array, Closure $callback)
113```
114
115### array_divide
116<a name="array_divide"/>
117
118```php
119/**
120	 * Divide an array into two arrays. One with keys and the other with values.
121	 *
122	 * @param  array  $array
123	 * @return array
124*/
125function array_divide($array)
126```
127
128### array_dot
129<a name="array_dot"/>
130
131```php
132/**
133	 * Flatten a multi-dimensional associative array with dots.
134	 *
135	 * @param  array   $array
136	 * @param  string  $prepend
137	 * @return array
138*/
139function array_dot($array, $prepend = '')
140```
141
142### array_except
143<a name="array_except"/>
144
145```php
146/**
147	 * Get all of the given array except for a specified array of items.
148	 *
149	 * @param  array  $array
150	 * @param  array|string  $keys
151	 * @return array
152*/
153function array_except($array, $keys)
154```
155
156### array_fetch
157<a name="array_fetch"/>
158
159```php
160/**
161	 * Fetch a flattened array of a nested array element.
162	 *
163	 * @param  array   $array
164	 * @param  string  $key
165	 * @return array
166*/
167function array_fetch($array, $key)
168```
169
170### array_first
171<a name="array_first"/>
172
173```php
174/**
175	 * Return the first element in an array passing a given truth test.
176	 *
177	 * @param  array     $array
178	 * @param  \Closure  $callback
179	 * @param  mixed     $default
180	 * @return mixed
181*/
182function array_first($array, $callback, $default = null)
183```
184
185### array_last
186<a name="array_last"/>
187
188```php
189/**
190	 * Return the last element in an array passing a given truth test.
191	 *
192	 * @param  array     $array
193	 * @param  \Closure  $callback
194	 * @param  mixed     $default
195	 * @return mixed
196*/
197function array_last($array, $callback, $default = null)
198```
199
200### array_flatten
201<a name="array_flatten"/>
202
203```php
204/**
205	 * Flatten a multi-dimensional array into a single level.
206	 *
207	 * @param  array  $array
208	 * @return array
209*/
210function array_flatten($array)
211```
212
213### array_forget
214<a name="array_forget"/>
215
216```php
217/**
218	 * Remove one or many array items from a given array using "dot" notation.
219	 *
220	 * @param  array  $array
221	 * @param  array|string  $keys
222	 * @return void
223*/
224function array_forget(&$array, $keys)
225```
226
227### array_get
228<a name="array_get"/>
229
230```php
231/**
232	 * Get an item from an array using "dot" notation.
233	 *
234	 * @param  array   $array
235	 * @param  string  $key
236	 * @param  mixed   $default
237	 * @return mixed
238*/
239function array_get($array, $key, $default = null)
240```
241
242### array_has
243<a name="array_has"/>
244
245```php
246/**
247	 * Check if an item exists in an array using "dot" notation.
248	 *
249	 * @param  array   $array
250	 * @param  string  $key
251	 * @return bool
252*/
253function array_has($array, $key)
254```
255
256### array_only
257<a name="array_only"/>
258
259```php
260/**
261	 * Get a subset of the items from the given array.
262	 *
263	 * @param  array  $array
264	 * @param  array|string  $keys
265	 * @return array
266*/
267function array_only($array, $keys)
268```
269
270### array_pluck
271<a name="array_pluck"/>
272
273```php
274/**
275	 * Pluck an array of values from an array.
276	 *
277	 * @param  array   $array
278	 * @param  string  $value
279	 * @param  string  $key
280	 * @return array
281*/
282function array_pluck($array, $value, $key = null)
283```
284
285### array_pull
286<a name="array_pull"/>
287
288```php
289/**
290	 * Get a value from the array, and remove it.
291	 *
292	 * @param  array   $array
293	 * @param  string  $key
294	 * @param  mixed   $default
295	 * @return mixed
296*/
297function array_pull(&$array, $key, $default = null)
298```
299
300### array_set
301<a name="array_set"/>
302
303```php
304/**
305	 * Set an array item to a given value using "dot" notation.
306	 *
307	 * If no key is given to the method, the entire array will be replaced.
308	 *
309	 * @param  array   $array
310	 * @param  string  $key
311	 * @param  mixed   $value
312	 * @return array
313*/
314function array_set(&$array, $key, $value)
315```
316
317### array_where
318<a name="array_where"/>
319
320```php
321/**
322	 * Filter the array using the given Closure.
323	 *
324	 * @param  array     $array
325	 * @param  \Closure  $callback
326	 * @return array
327*/
328function array_where($array, Closure $callback)
329```
330
331### head
332<a name="head"/>
333
334```php
335/**
336	 * Get the first element of an array. Useful for method chaining.
337	 *
338	 * @param  array  $array
339	 * @return mixed
340*/
341function head($array)
342```
343
344### last
345<a name="last"/>
346
347```php
348/**
349	 * Get the last element from an array.
350	 *
351	 * @param  array  $array
352	 * @return mixed
353*/
354function last($array)
355```
356
357## Strings
358<a name="strings"/>
359
360### ascii
361<a name="ascii"/>
362
363```php
364/**
365     * Transliterate a UTF-8 value to ASCII.
366     *
367     * @param  string  $value
368     * @return string
369 */
370function ascii($value)
371```
372
373### camel_case
374<a name="camel_case"/>
375
376```php
377/**
378	 * Convert a value to camel case.
379	 *
380	 * @param  string  $value
381	 * @return string
382*/
383function camel_case($value)
384```
385
386### charsArray
387<a name="charsArray"/>
388
389```php
390/**
391     * Returns the replacements for the ascii method.
392     *
393     * Note: Adapted from Stringy\Stringy.
394     *
395     * @see https://github.com/danielstjules/Stringy/blob/2.3.1/LICENSE.txt
396     *
397     * @return array
398 */
399function charsArray()
400```
401
402### ends_with
403<a name="ends_with"/>
404
405```php
406/**
407	 * Determine if a given string ends with a given substring.
408	 *
409	 * @param  string  $haystack
410	 * @param  string|array  $needles
411	 * @return bool
412*/
413function ends_with($haystack, $needles)
414```
415
416### preg_replace_sub
417<a name="preg_replace_sub"/>
418
419```php
420/**
421	 * Replace a given pattern with each value in the array in sequentially.
422	 *
423	 * @param  string  $pattern
424	 * @param  array   $replacements
425	 * @param  string  $subject
426	 * @return string
427*/
428function preg_replace_sub($pattern, &$replacements, $subject)
429```
430
431### snake_case
432<a name="snake_case"/>
433
434```php
435/**
436	 * Convert a string to snake case.
437	 *
438	 * @param  string  $value
439	 * @param  string  $delimiter
440	 * @return string
441*/
442function snake_case($value, $delimiter = '_')
443```
444
445### starts_with
446<a name="starts_with"/>
447
448```php
449/**
450	 * Determine if a given string starts with a given substring.
451	 *
452	 * @param  string  $haystack
453	 * @param  string|array  $needles
454	 * @return bool
455*/
456function starts_with($haystack, $needles)
457```
458
459### str_contains
460<a name="str_contains"/>
461
462```php
463/**
464	 * Determine if a given string contains a given substring.
465	 *
466	 * @param  string  $haystack
467	 * @param  string|array  $needles
468	 * @return bool
469*/
470function str_contains($haystack, $needles)
471```
472
473### str_finish
474<a name="str_finish"/>
475
476```php
477/**
478	 * Cap a string with a single instance of a given value.
479	 *
480	 * @param  string  $value
481	 * @param  string  $cap
482	 * @return string
483*/
484function str_finish($value, $cap)
485```
486
487### str_is
488<a name="str_is"/>
489
490```php
491/**
492	 * Determine if a given string matches a given pattern.
493	 *
494	 * @param  string  $pattern
495	 * @param  string  $value
496	 * @return bool
497*/
498function str_is($pattern, $value)
499```
500
501### str_limit
502<a name="str_limit"/>
503
504```php
505/**
506	 * Limit the number of characters in a string.
507	 *
508	 * @param  string  $value
509	 * @param  int     $limit
510	 * @param  string  $end
511	 * @return string
512*/
513function str_limit($value, $limit = 100, $end = '...')
514```
515
516### str_random
517<a name="str_random"/>
518
519```php
520/**
521	 * Generate a more truly "random" alpha-numeric string.
522	 *
523	 * @param  int  $length
524	 * @return string
525	 *
526	 * @throws \RuntimeException
527*/
528function str_random($length = 16)
529```
530
531### str_replace_array
532<a name="str_replace_array"/>
533
534```php
535/**
536	 * Replace a given value in the string sequentially with an array.
537	 *
538	 * @param  string  $search
539	 * @param  array   $replace
540	 * @param  string  $subject
541	 * @return string
542*/
543function str_replace_array($search, array $replace, $subject)
544```
545
546### str_slug
547<a name="str_slug"/>
548
549```php
550/**
551	 * Generate a URL friendly "slug" from a given string.
552	 *
553	 * @param  string  $title
554	 * @param  string  $separator
555	 * @return string
556*/
557function str_slug(string $title, string $separator = '-')
558```
559
560### studly_case
561<a name="studly_case"/>
562
563```php
564/**
565	 * Convert a value to studly caps case.
566	 *
567	 * @param  string  $value
568	 * @return string
569*/
570function studly_case($value)
571```
572
573## Classes
574<a name="classes"/>
575
576### class_basename
577<a name="class_basename"/>
578
579```php
580/**
581	 * Get the class "basename" of the given object / class.
582	 *
583	 * @param  string|object  $class
584	 * @return string
585*/
586function class_basename($class)
587```
588
589### class_uses_recursive
590<a name="class_uses_recursive"/>
591
592```php
593/**
594	 * Returns all traits used by a class, it's subclasses and trait of their traits
595	 *
596	 * @param  string  $class
597	 * @return array
598*/
599function class_uses_recursive($class)
600```
601
602### trait_uses_recursive
603<a name="trait_uses_recursive"/>
604
605```php
606/**
607	 * Returns all traits used by a trait and its traits
608	 *
609	 * @param  string  $trait
610	 * @return array
611*/
612function trait_uses_recursive($trait)
613```
614
615## Misc.
616<a name="misc"/>
617
618### data_get
619<a name="data_get"/>
620
621```php
622/**
623	 * Get an item from an array or object using "dot" notation.
624	 *
625	 * @param  mixed   $target
626	 * @param  string  $key
627	 * @param  mixed   $default
628	 * @return mixed
629*/
630function data_get($target, $key, $default = null)
631```
632
633### e
634<a name="e"/>
635
636```php
637/**
638	 * Escape HTML entities in a string.
639	 *
640	 * @param  string  $value
641	 * @return string
642*/
643function e($value)
644```
645
646### object_get
647<a name="object_get"/>
648
649```php
650/**
651	 * Get an item from an object using "dot" notation.
652	 *
653	 * @param  object  $object
654	 * @param  string  $key
655	 * @param  mixed   $default
656	 * @return mixed
657*/
658function object_get($object, $key, $default = null)
659```
660
661### value
662<a name="value"/>
663
664```php
665/**
666	 * Return the default value of the given value.
667	 *
668	 * @param  mixed  $value
669	 * @return mixed
670*/
671function value($value)
672```
673
674### with
675<a name="with"/>
676
677```php
678/**
679	 * Return the given object. Useful for chaining.
680	 *
681	 * @param  mixed  $object
682	 * @return mixed
683*/
684function with($object)
685```
686
687### dd
688<a name="dd"/>
689
690```php
691 /**
692 	* Dump the passed variables and end the script.
693	*
694	* @param  mixed  $args
695	* @return void
696*/
697function dd($arg...)
698```
699
700### data_set
701<a name="data_set"/>
702
703```php
704 /**
705      * Set an item on an array or object using dot notation.
706      *
707      * @param  mixed  $target
708      * @param  string|array  $key
709      * @param  mixed  $value
710      * @param  bool  $overwrite
711      * @return mixed
712 */
713 function data_set(&$target, $key, $value, $overwrite = true)
714```
715
716### data_fill
717<a name="data_fill"/>
718
719```php
720/**
721     * Fill in data where it's missing.
722     *
723     * @param  mixed   $target
724     * @param  string|array  $key
725     * @param  mixed  $value
726     * @return mixed
727*/
728function data_fill(&$target, $key, $value)
729```