1 <?php
2
3 use Docolight\Support\Arr;
4 use Docolight\Support\Str;
5 use Docolight\Support\Fluent;
6 use Docolight\Support\Collection;
7 use Docolight\Support\Debug\Dumper;
8
9 if (!function_exists('array_add')) {
10 /**
11 * Add an element to an array using "dot" notation if it doesn't exist.
12 *
13 * @param array $array
14 * @param string $key
15 * @param mixed $value
16 *
17 * @return array
18 *
19 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
20 */
21 function array_add($array, $key, $value)
22 {
23 return Arr::add($array, $key, $value);
24 }
25 }
26
27 if (!function_exists('array_build')) {
28 /**
29 * Build a new array using a callback.
30 *
31 * @param array $array
32 * @param callable $callback
33 *
34 * @return array
35 *
36 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
37 */
38 function array_build($array, callable $callback)
39 {
40 return Arr::build($array, $callback);
41 }
42 }
43
44 if (!function_exists('array_collapse')) {
45 /**
46 * Collapse an array of arrays into a single array.
47 *
48 * @param array|\ArrayAccess $array
49 *
50 * @return array
51 *
52 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
53 */
54 function array_collapse($array)
55 {
56 return Arr::collapse($array);
57 }
58 }
59
60 if (!function_exists('array_divide')) {
61 /**
62 * Divide an array into two arrays. One with keys and the other with values.
63 *
64 * @param array $array
65 *
66 * @return array
67 *
68 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
69 */
70 function array_divide($array)
71 {
72 return Arr::divide($array);
73 }
74 }
75
76 if (!function_exists('array_dot')) {
77 /**
78 * Flatten a multi-dimensional associative array with dots.
79 *
80 * @param array $array
81 * @param string $prepend
82 *
83 * @return array
84 *
85 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
86 */
87 function array_dot($array, $prepend = '')
88 {
89 return Arr::dot($array, $prepend);
90 }
91 }
92
93 if (!function_exists('array_except')) {
94 /**
95 * Get all of the given array except for a specified array of items.
96 *
97 * @param array $array
98 * @param array|string $keys
99 *
100 * @return array
101 *
102 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
103 */
104 function array_except($array, $keys)
105 {
106 return Arr::except($array, $keys);
107 }
108 }
109
110 if (!function_exists('array_first')) {
111 /**
112 * Return the first element in an array passing a given truth test.
113 *
114 * @param array $array
115 * @param callable|null $callback
116 * @param mixed $default
117 *
118 * @return mixed
119 *
120 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
121 */
122 function array_first($array, callable $callback = null, $default = null)
123 {
124 return Arr::first($array, $callback, $default);
125 }
126 }
127
128 if (!function_exists('array_last')) {
129 /**
130 * Return the last element in an array passing a given truth test.
131 *
132 * @param array $array
133 * @param callable|null $callback
134 * @param mixed $default
135 *
136 * @return mixed
137 *
138 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
139 */
140 function array_last($array, $callback = null, $default = null)
141 {
142 return Arr::last($array, $callback, $default);
143 }
144 }
145
146 if (!function_exists('array_flatten')) {
147 /**
148 * Flatten a multi-dimensional array into a single level.
149 *
150 * @param array $array
151 *
152 * @return array
153 *
154 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
155 */
156 function array_flatten($array)
157 {
158 return Arr::flatten($array);
159 }
160 }
161
162 if (!function_exists('array_forget')) {
163 /**
164 * Remove one or many array items from a given array using "dot" notation.
165 *
166 * @param array $array
167 * @param array|string $keys
168 */
169 function array_forget(&$array, $keys)
170 {
171 return Arr::forget($array, $keys);
172 }
173 }
174
175 if (!function_exists('array_get')) {
176 /**
177 * Get an item from an array using "dot" notation.
178 *
179 * @param array $array
180 * @param string $key
181 * @param mixed $default
182 *
183 * @return mixed
184 *
185 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
186 */
187 function array_get($array, $key, $default = null)
188 {
189 return Arr::get($array, $key, $default);
190 }
191 }
192
193 if (!function_exists('array_has')) {
194 /**
195 * Check if an item exists in an array using "dot" notation.
196 *
197 * @param array $array
198 * @param string $key
199 *
200 * @return bool
201 *
202 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
203 */
204 function array_has($array, $key)
205 {
206 return Arr::has($array, $key);
207 }
208 }
209
210 if (!function_exists('array_only')) {
211 /**
212 * Get a subset of the items from the given array.
213 *
214 * @param array $array
215 * @param array|string $keys
216 *
217 * @return array
218 *
219 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
220 */
221 function array_only($array, $keys)
222 {
223 return Arr::only($array, $keys);
224 }
225 }
226
227 if (!function_exists('array_pluck')) {
228 /**
229 * Pluck an array of values from an array.
230 *
231 * @param array $array
232 * @param string $value
233 * @param string $key
234 *
235 * @return array
236 *
237 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
238 */
239 function array_pluck($array, $value, $key = null)
240 {
241 return Arr::pluck($array, $value, $key);
242 }
243 }
244
245 if (!function_exists('array_pull')) {
246 /**
247 * Get a value from the array, and remove it.
248 *
249 * @param array $array
250 * @param string $key
251 * @param mixed $default
252 *
253 * @return mixed
254 *
255 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
256 */
257 function array_pull(&$array, $key, $default = null)
258 {
259 return Arr::pull($array, $key, $default);
260 }
261 }
262
263 if (!function_exists('array_set')) {
264 /**
265 * Set an array item to a given value using "dot" notation.
266 *
267 * If no key is given to the method, the entire array will be replaced.
268 *
269 * @param array $array
270 * @param string $key
271 * @param mixed $value
272 *
273 * @return array
274 *
275 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
276 */
277 function array_set(&$array, $key, $value)
278 {
279 return Arr::set($array, $key, $value);
280 }
281 }
282
283 if (!function_exists('array_sort')) {
284 /**
285 * Sort the array using the given callback.
286 *
287 * @param array $array
288 * @param callable $callback
289 *
290 * @return array
291 *
292 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
293 */
294 function array_sort($array, callable $callback)
295 {
296 return Arr::sort($array, $callback);
297 }
298 }
299
300 if (!function_exists('array_where')) {
301 /**
302 * Filter the array using the given callback.
303 *
304 * @param array $array
305 * @param callable $callback
306 *
307 * @return array
308 *
309 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
310 */
311 function array_where($array, callable $callback)
312 {
313 return Arr::where($array, $callback);
314 }
315 }
316
317 if (!function_exists('camel_case')) {
318 /**
319 * Convert a value to camel case.
320 *
321 * @param string $value
322 *
323 * @return string
324 *
325 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
326 */
327 function camel_case($value)
328 {
329 return Str::camel($value);
330 }
331 }
332
333 if (!function_exists('class_basename')) {
334 /**
335 * Get the class "basename" of the given object / class.
336 *
337 * @param string|object $class
338 *
339 * @return string
340 *
341 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
342 */
343 function class_basename($class)
344 {
345 $class = is_object($class) ? get_class($class) : $class;
346
347 return basename(str_replace('\\', '/', $class));
348 }
349 }
350
351 if (!function_exists('class_uses_recursive')) {
352 /**
353 * Returns all traits used by a class, its subclasses and trait of their traits.
354 *
355 * @param string $class
356 *
357 * @return array
358 *
359 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
360 */
361 function class_uses_recursive($class)
362 {
363 $results = array();
364
365 foreach (array_merge(array($class => $class), class_parents($class)) as $class) {
366 $results += trait_uses_recursive($class);
367 }
368
369 return array_unique($results);
370 }
371 }
372
373 if (!function_exists('collect')) {
374 /**
375 * Create a collection from the given value.
376 *
377 * @param mixed $value
378 *
379 * @return \Docolight\Support\Collection
380 *
381 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
382 */
383 function collect($value = null)
384 {
385 return new Collection($value);
386 }
387 }
388
389 if (!function_exists('fluent')) {
390 /**
391 * Create a new model from the given value.
392 *
393 * @param mixed $value
394 *
395 * @return \Docolight\Support\Fluent
396 *
397 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
398 */
399 function fluent(array $value = array())
400 {
401 return new Fluent($value);
402 }
403 }
404
405 if (!function_exists('data_get')) {
406 /**
407 * Get an item from an array or object using "dot" notation.
408 *
409 * @param mixed $target
410 * @param string|array $key
411 * @param mixed $default
412 *
413 * @return mixed
414 *
415 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
416 */
417 function data_get($target, $key, $default = null)
418 {
419 if (is_null($key)) {
420 return $target;
421 }
422
423 $key = is_array($key) ? $key : explode('.', $key);
424
425 foreach ($key as $segment) {
426 if (is_array($target)) {
427 if (!array_key_exists($segment, $target)) {
428 return value($default);
429 }
430
431 $target = $target[$segment];
432 } elseif ($target instanceof ArrayAccess) {
433 if (!isset($target[$segment])) {
434 return value($default);
435 }
436
437 $target = $target[$segment];
438 } elseif (is_object($target)) {
439 if (!isset($target->{$segment})) {
440 return value($default);
441 }
442
443 $target = $target->{$segment};
444 } else {
445 return value($default);
446 }
447 }
448
449 return $target;
450 }
451 }
452
453 if (!function_exists('e')) {
454 /**
455 * Escape HTML entities in a string.
456 *
457 * @param string $value
458 *
459 * @return string
460 *
461 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
462 */
463 function e($value)
464 {
465 return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
466 }
467 }
468
469 if (!function_exists('ends_with')) {
470 /**
471 * Determine if a given string ends with a given substring.
472 *
473 * @param string $haystack
474 * @param string|array $needles
475 *
476 * @return bool
477 *
478 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
479 */
480 function ends_with($haystack, $needles)
481 {
482 return Str::endsWith($haystack, $needles);
483 }
484 }
485
486 if (!function_exists('head')) {
487 /**
488 * Get the first element of an array. Useful for method chaining.
489 *
490 * @param array $array
491 *
492 * @return mixed
493 *
494 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
495 */
496 function head($array)
497 {
498 return reset($array);
499 }
500 }
501
502 if (!function_exists('last')) {
503 /**
504 * Get the last element from an array.
505 *
506 * @param array $array
507 *
508 * @return mixed
509 *
510 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
511 */
512 function last($array)
513 {
514 return end($array);
515 }
516 }
517
518 if (!function_exists('object_get')) {
519 /**
520 * Get an item from an object using "dot" notation.
521 *
522 * @param object $object
523 * @param string $key
524 * @param mixed $default
525 *
526 * @return mixed
527 *
528 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
529 */
530 function object_get($object, $key, $default = null)
531 {
532 if (is_null($key) || trim($key) == '') {
533 return $object;
534 }
535
536 foreach (explode('.', $key) as $segment) {
537 if (!is_object($object) || !isset($object->{$segment})) {
538 return value($default);
539 }
540
541 $object = $object->{$segment};
542 }
543
544 return $object;
545 }
546 }
547
548 if (!function_exists('preg_replace_sub')) {
549 /**
550 * Replace a given pattern with each value in the array in sequentially.
551 *
552 * @param string $pattern
553 * @param array $replacements
554 * @param string $subject
555 *
556 * @return string
557 *
558 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
559 */
560 function preg_replace_sub($pattern, &$replacements, $subject)
561 {
562 return preg_replace_callback($pattern, function ($match) use (&$replacements) {
563 foreach ($replacements as $key => $value) {
564 return array_shift($replacements);
565 }
566
567 }, $subject);
568 }
569 }
570
571 if (!function_exists('array_sort_recursive')) {
572 /**
573 * Recursively sort an array by keys and values.
574 *
575 * @param array $array
576 *
577 * @return array
578 *
579 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
580 */
581 function array_sort_recursive($array)
582 {
583 foreach ($array as &$value) {
584 if (is_array($value) && isset($value[0])) {
585 sort($value);
586 } elseif (is_array($value)) {
587 array_sort_recursive($value);
588 }
589 }
590
591 ksort($array);
592
593 return $array;
594 }
595 }
596
597 if (!function_exists('snake_case')) {
598 /**
599 * Convert a string to snake case.
600 *
601 * @param string $value
602 * @param string $delimiter
603 *
604 * @return string
605 *
606 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
607 */
608 function snake_case($value, $delimiter = '_')
609 {
610 return Str::snake($value, $delimiter);
611 }
612 }
613
614 if (!function_exists('starts_with')) {
615 /**
616 * Determine if a given string starts with a given substring.
617 *
618 * @param string $haystack
619 * @param string|array $needles
620 *
621 * @return bool
622 *
623 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
624 */
625 function starts_with($haystack, $needles)
626 {
627 return Str::startsWith($haystack, $needles);
628 }
629 }
630
631 if (!function_exists('str_contains')) {
632 /**
633 * Determine if a given string contains a given substring.
634 *
635 * @param string $haystack
636 * @param string|array $needles
637 *
638 * @return bool
639 *
640 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
641 */
642 function str_contains($haystack, $needles)
643 {
644 return Str::contains($haystack, $needles);
645 }
646 }
647
648 if (!function_exists('str_finish')) {
649 /**
650 * Cap a string with a single instance of a given value.
651 *
652 * @param string $value
653 * @param string $cap
654 *
655 * @return string
656 *
657 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
658 */
659 function str_finish($value, $cap)
660 {
661 return Str::finish($value, $cap);
662 }
663 }
664
665 if (!function_exists('str_is')) {
666 /**
667 * Determine if a given string matches a given pattern.
668 *
669 * @param string $pattern
670 * @param string $value
671 *
672 * @return bool
673 *
674 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
675 */
676 function str_is($pattern, $value)
677 {
678 return Str::is($pattern, $value);
679 }
680 }
681
682 if (!function_exists('str_limit')) {
683 /**
684 * Limit the number of characters in a string.
685 *
686 * @param string $value
687 * @param int $limit
688 * @param string $end
689 *
690 * @return string
691 *
692 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
693 */
694 function str_limit($value, $limit = 100, $end = '...')
695 {
696 return Str::limit($value, $limit, $end);
697 }
698 }
699
700 if (!function_exists('str_random')) {
701 /**
702 * Generate a more truly "random" alpha-numeric string.
703 *
704 * @param int $length
705 *
706 * @return string
707 *
708 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
709 *
710 * @throws \RuntimeException
711 */
712 function str_random($length = 16)
713 {
714 return Str::random($length);
715 }
716 }
717
718 if (!function_exists('str_replace_array')) {
719 /**
720 * Replace a given value in the string sequentially with an array.
721 *
722 * @param string $search
723 * @param array $replace
724 * @param string $subject
725 *
726 * @return string
727 *
728 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
729 */
730 function str_replace_array($search, array $replace, $subject)
731 {
732 foreach ($replace as $value) {
733 $subject = preg_replace('/'.$search.'/', $value, $subject, 1);
734 }
735
736 return $subject;
737 }
738 }
739
740 if (!function_exists('str_slug')) {
741 /**
742 * Generate a URL friendly "slug" from a given string.
743 *
744 * @param string $title
745 * @param string $separator
746 *
747 * @return string
748 *
749 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
750 */
751 function str_slug($title, $separator = '-')
752 {
753 return Str::slug($title, $separator);
754 }
755 }
756
757 if (!function_exists('studly_case')) {
758 /**
759 * Convert a value to studly caps case.
760 *
761 * @param string $value
762 *
763 * @return string
764 *
765 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
766 */
767 function studly_case($value)
768 {
769 return Str::studly($value);
770 }
771 }
772
773 if (!function_exists('title_case')) {
774 /**
775 * Convert a value to title case.
776 *
777 * @param string $value
778 *
779 * @return string
780 *
781 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
782 */
783 function title_case($value)
784 {
785 return Str::title($value);
786 }
787 }
788
789 if (!function_exists('trimtolower')) {
790 /**
791 * Trim and convert a value to lowercase.
792 *
793 * @param string $value
794 *
795 * @return string
796 *
797 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
798 */
799 function trimtolower($value)
800 {
801 return trim(mb_strtolower($value));
802 }
803 }
804
805 if (!function_exists('trait_uses_recursive')) {
806 /**
807 * Returns all traits used by a trait and its traits.
808 *
809 * @param string $trait
810 *
811 * @return array
812 *
813 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
814 */
815 function trait_uses_recursive($trait)
816 {
817 $traits = class_uses($trait);
818
819 foreach ($traits as $trait) {
820 $traits += trait_uses_recursive($trait);
821 }
822
823 return $traits;
824 }
825 }
826
827 if (!function_exists('value')) {
828 /**
829 * Return the default value of the given value.
830 *
831 * @param mixed $value
832 *
833 * @return mixed
834 *
835 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
836 */
837 function value($value)
838 {
839 return $value instanceof Closure ? $value() : $value;
840 }
841 }
842
843 if (!function_exists('with')) {
844 /**
845 * Return the same value you passed to it's argument. Very usefull to access chain object / method in non-reuse-instance cases (PHP v5.3), like `with(with($foo = new Foo)->foo())->bar($foo)`.
846 *
847 * @param mixed $anything
848 *
849 * @return mixed
850 *
851 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
852 */
853 function with($anything)
854 {
855 return $anything;
856 }
857 }
858
859 if (!function_exists('def')) {
860 /**
861 * Save way to access array or public property from an object.
862 *
863 * @param array|object $stack
864 * @param string $offset
865 * @param mixed $default
866 *
867 * @return mixed
868 *
869 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
870 */
871 function def($stack, $offset, $default = null)
872 {
873 if (is_array($stack)) {
874 if (array_key_exists($offset, $stack)) {
875 return $stack[$offset];
876 }
877 } elseif (is_object($stack)) {
878 if (property_exists($stack, $offset) or $stack->__isset($offset)) {
879 return $stack->{$offset};
880 } else {
881 return def((array) $stack, $offset, value($default));
882 }
883 } else {
884 throw new InvalidArgumentException('The first argument of def must be an array or object.');
885 }
886
887 return value($default);
888 }
889 }
890
891 if (!function_exists('container')) {
892 /**
893 * Get the available container instance. Make sure you have enabled `Container` components. In your `components` configuration, add this lines:.
894 *
895 * ```php
896 * 'container' => array( 'class' => 'Container' ),
897 * ```
898 *
899 * @param string $make
900 * @param array $parameters
901 *
902 * @return mixed|\Docolight\Container\Container
903 *
904 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
905 */
906 function container($make = null, array $parameters = array())
907 {
908 $container = Yii::app()->getComponent('container', false);
909
910 if ($container === null) {
911 throw new RuntimeException("[Docolight\Container\Container] component has not been registered yet.");
912 }
913
914 if (func_num_args() === 0) {
915 return $container;
916 }
917
918 return $container->make($make, $parameters);
919 }
920 }
921
922 if (!function_exists('dump')) {
923 /**
924 * The Dumper component provides mechanisms for walking through any arbitrary PHP variable. Built on top, it provides a better `dump()` function that you can use instead of `var_dump`.
925 *
926 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
927 */
928 function dump()
929 {
930 array_map(function ($var) {
931 value(new Dumper())->dump($var);
932 }, func_get_args());
933 }
934 }
935
936 if (!function_exists('dd')) {
937 /**
938 * Same as `dump`, but after dumping, we also stop the PHP exectution runtime by calling `die()` function.
939 *
940 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
941 */
942 function dd()
943 {
944 call_user_func_array('dump', func_get_args());
945
946 die(1);
947 }
948 }
949
950 if (!function_exists('session')) {
951 /**
952 * Session helper.
953 *
954 * @param string|null $identifier If you don't pass anything in first argument, it will return the session object itself.
955 * @param mixed|null $value If you pass a null to second argument, it will destroy the session on it's identifier passed in first argument.
956 *
957 * @return mixed
958 *
959 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
960 */
961 function session($identifier = null, $value = null)
962 {
963 $session = Yii::app()->session;
964
965 if (! $session instanceof CHttpSession) {
966 throw new Exception("Session has not been configured yet.");
967 }
968
969 $numberOfArguments = func_num_args();
970
971 // If you don't pass any argument of the identifier is null, I'll return the session manager object.
972 if ($numberOfArguments === 0 or $identifier === null) {
973 return $session;
974 }
975 // If the first argument is not null, I'll return the session value on it's identifier
976 elseif ($numberOfArguments === 1 and $identifier !== null) {
977 return $session->get($identifier, value($value));
978 }
979 // If the second argument is not null and the identifier is not null and value is not null
980 // I'll set new value of session
981 elseif ($numberOfArguments === 2 and $identifier !== null and $value !== null) {
982 $session[$identifier] = $value;
983 }
984 // If the second argument is null and the identifier is not null but value is null
985 // I'll destroy the session on it's identifier
986 elseif ($numberOfArguments === 2 and $identifier !== null and $value === null) {
987 $session->remove($identifier);
988 }
989 }
990 }
991
992 if (!function_exists('cache')) {
993 /**
994 * A cache helper to simplify it's usage.
995 *
996 * @param string $identifier Cache identifier.
997 * @param mixed $value Cache value.
998 * @param int $time Cache timeout.
999 * @param string $dependency Class name dependency.
1000 * @param array $params Dependency parameters.
1001 *
1002 * @return mixed
1003 *
1004 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1005 */
1006 function cache($identifier = null, $value = null, $time = null, $dependency = null, array $params = array())
1007 {
1008 $cache = Yii::app()->cache;
1009
1010 // Strict using CCache abstraction class, if not, just throw a catchable exception.
1011 if (!$cache instanceof CCache) {
1012 throw new RuntimeException('Cache not configured.');
1013 }
1014
1015 $numberOfArguments = func_num_args();
1016
1017 // If you don't pass any arguments to this function,
1018 // or the $identifier is null. then it will return CCache implementation
1019 if ($numberOfArguments === 0 or $identifier === null) {
1020 return $cache;
1021 }
1022 // If you pass only one arguments and it's not null, then it will get
1023 // the cache value based on the value of $identifier
1024 elseif ($numberOfArguments === 1 and $identifier !== null) {
1025 return $cache->get($identifier);
1026 }
1027 // If you pass only two arguments, the identifier is not null and the value is null
1028 // It will asume you want to delete the cache
1029 elseif ($numberOfArguments === 2 and $identifier !== null and $value === null) {
1030 return $cache->delete($identifier);
1031 }
1032 // Anyhow, if you pass more than one arguments to this method, then it will assume you
1033 // want to store a cache.
1034 else {
1035 return $cache->set(
1036 $identifier,
1037 $value,
1038 $time,
1039 (is_string($dependency) ? container()->make($dependency, $params)
1040 : (($dependency instanceof CCacheDependency) ? $dependency
1041 : null)
1042 )
1043 );
1044 }
1045 }
1046 }
1047
1048 if (!function_exists('request')) {
1049 /**
1050 * Get post / get value
1051 *
1052 * @param null|string $identifier
1053 * @param mixed $default
1054 *
1055 * @return mixed
1056 *
1057 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1058 */
1059 function request($identifier = null, $default = null)
1060 {
1061 $request = Yii::app()->request;
1062
1063 if (! $request instanceof CHttpRequest) {
1064 throw new Exception("Request component has not been cofigured yet.");
1065 }
1066
1067 if (func_num_args() === 0 or $identifier === null) {
1068 return $request;
1069 }
1070
1071 return $request->getParam($identifier, value($default));
1072 }
1073 }
1074
1075 if (!function_exists('input')) {
1076 /**
1077 * Get $_POST value
1078 *
1079 * @param null|string $identifier
1080 * @param mixed $default
1081 *
1082 * @return mixed
1083 *
1084 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1085 */
1086 function input($identifier = null, $default = null)
1087 {
1088 if (func_num_args() === 0 or $identifier === null) {
1089 return $_POST;
1090 }
1091
1092 return request()->getPost($identifier, value($default));
1093 }
1094 }
1095
1096 if (!function_exists('array_replace_value')) {
1097 /**
1098 * Replace your array value.
1099 *
1100 * ```php
1101 * $header = [
1102 * ':type_address',
1103 * ':type_citizenship',
1104 * ':type_city',
1105 * ':type_country' ]
1106 *
1107 * Arr::replaceValue($header, ':type_')
1108 *
1109 * // Will produce:
1110 *
1111 * $header = [
1112 * 'address',
1113 * 'citizenship',
1114 * 'city',
1115 * 'country' ]
1116 * ```
1117 *
1118 * @param array $input
1119 * @param string|callable $search
1120 * @param string $replacement
1121 *
1122 * @return array
1123 *
1124 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1125 */
1126 function array_replace_value(array $input, $search, $replacement = '')
1127 {
1128 return Arr::replaceValue($input, $search, $replacement);
1129 }
1130 }
1131
1132 if (!function_exists('get')) {
1133 /**
1134 * Get $_GET value
1135 *
1136 * @param null|string $identifier
1137 * @param mixed $default
1138 *
1139 * @return mixed
1140 *
1141 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1142 */
1143 function get($identifier = null, $default = null)
1144 {
1145 if (func_num_args() === 0 or $identifier === null) {
1146 return $_GET;
1147 }
1148
1149 return request()->getQuery($identifier, value($default));
1150 }
1151 }
1152
1153 if (!function_exists('transaction')) {
1154 /**
1155 * Get a transaction on a connection.
1156 *
1157 * @param \CDbConnection $connection
1158 *
1159 * @return \CDbTransaction
1160 *
1161 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1162 */
1163 function transaction(CDbConnection $connection)
1164 {
1165 if (container()->bound($connectionClass = get_class($connection))) {
1166 return container($connectionClass);
1167 }
1168
1169 $transaction = $connection->getCurrentTransaction();
1170
1171 if (is_null($transaction)) {
1172 $transaction = $connection->beginTransaction();
1173 }
1174
1175 if (! $connection->getActive()) {
1176 $connection->setActive(true);
1177 }
1178
1179 container()->bindIf($connectionClass, function () use ($transaction) {
1180 return $transaction;
1181 }, true);
1182
1183 return container($connectionClass);
1184 }
1185 }
1186
1187 if (!function_exists('response')) {
1188 /**
1189 * Make response even quicker.
1190 *
1191 * @param string $driver Possible values are 'base' | 'json' | null
1192 * @param integer $status Your HTTP status code
1193 * @param mixed $body Your response body
1194 * @param array $headers Index - Value paired array for your header information
1195 * @param bool $immediatelySend Choose whether to immediately send the response or not
1196 *
1197 * @return mixed
1198 *
1199 * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
1200 */
1201 function response($driver = 'base', $status = 200, $body = '', array $headers = array(), $immediatelySend = false)
1202 {
1203 if (! container()->bound('Docolight\Http\ResponseFactory')) {
1204 throw new RuntimeException('Class [Docolight\Http\ResponseFactory] has not been bound yet.');
1205 }
1206
1207 $numberOfArguments = func_num_args();
1208
1209 $response = container('response');
1210
1211 if ($numberOfArguments === 0) {
1212 return $response;
1213 } elseif ($numberOfArguments === 1) {
1214 return $response->produce(((!$driver) ? 'base' : $driver));
1215 }
1216
1217 $response = $response->produce($driver);
1218
1219 $response->setStatus($status);
1220
1221 $response->setBody($body);
1222
1223 array_walk($headers, function ($value, $key) use ($response) {
1224 $response->headers->set($key, $value);
1225 });
1226
1227 return ($immediatelySend) ? $response->send()
1228 : $response;
1229 }
1230 }
1231