1: <?php
2:
3: /**
4: * Copyright 2015, Eduardo Trujillo
5: *
6: * For the full copyright and license information, please view the LICENSE
7: * file that was distributed with this source code.
8: *
9: * This file is part of the Nucleus package
10: */
11:
12: namespace Chromabits\Nucleus\Support;
13:
14: use Chromabits\Nucleus\Foundation\StaticObject;
15:
16: use Chromabits\Nucleus\Meditation\Exceptions\InvalidArgumentException;
17: use Chromabits\Nucleus\Strings\Rope;
18: use RuntimeException;
19:
20: /**
21: * Class Str.
22: *
23: * Some string utilities (from Laravel)
24: *
25: * @author Eduardo Trujillo <ed@chromabits.com>
26: * @package Chromabits\Nucleus\Support
27: */
28: class Str extends StaticObject
29: {
30: /**
31: * Convert a value to camel case.
32: *
33: * @param string $value
34: * @param string|null $encoding
35: *
36: * @return string
37: */
38: public static function camel($value, $encoding = null)
39: {
40: return (new Rope($value, $encoding))->toCamel()->toString();
41: }
42:
43: /**
44: * Convert a string to snake case.
45: *
46: * @param string $value
47: * @param string $delimiter
48: * @param string|null $encoding
49: *
50: * @return string
51: */
52: public static function snake($value, $delimiter = '_', $encoding = null)
53: {
54: return (new Rope($value, $encoding))->toSnake($delimiter)->toString();
55: }
56:
57: /**
58: * Convert a value to studly caps case.
59: *
60: * @param string $value
61: * @param string|null $encoding
62: *
63: * @return string
64: */
65: public static function studly($value, $encoding = null)
66: {
67: return (new Rope($value, $encoding))->toStudly()->toString();
68: }
69:
70: /**
71: * Generate a more truly "random" alpha-numeric string.
72: *
73: * @param int $length
74: *
75: * @throws RuntimeException
76: * @return string
77: */
78: public static function random($length = 16)
79: {
80: if (!function_exists('openssl_random_pseudo_bytes')) {
81: throw new RuntimeException('OpenSSL extension is required.');
82: }
83:
84: $bytes = openssl_random_pseudo_bytes($length * 2);
85:
86: if ($bytes === false) {
87: throw new RuntimeException('Unable to generate random string.');
88: }
89:
90: return substr(
91: str_replace(['/', '+', '='], '', base64_encode($bytes)),
92: 0,
93: $length
94: );
95: }
96:
97: /**
98: * Generate a "random" alpha-numeric string.
99: *
100: * Should not be considered sufficient for cryptography, etc.
101: *
102: * @param int $length
103: *
104: * @return string
105: */
106: public static function quickRandom($length = 16)
107: {
108: $pool = '0123456789abcdefghijklmnopqrstuvwxyz'
109: . 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
110:
111: return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
112: }
113:
114: /**
115: * Return whether or not the provided subject beings with the prefix.
116: *
117: * @param string $subject
118: * @param string $prefix
119: * @param null|string $encoding
120: *
121: * @return bool
122: */
123: public static function beginsWith($subject, $prefix, $encoding = null)
124: {
125: return Rope::of($subject, $encoding)->beginsWith($prefix);
126: }
127:
128: /**
129: * Return whether or not the provided subject ends with suffix.
130: *
131: * @param string $subject
132: * @param string $suffix
133: * @param null|string $encoding
134: *
135: * @return bool
136: * @throws InvalidArgumentException
137: */
138: public static function endsWith($subject, $suffix, $encoding = null)
139: {
140: return Rope::of($subject, $encoding)->endsWith($suffix);
141: }
142:
143: /**
144: * Return whether or not the subject contains the inner string.
145: *
146: * @param string $subject
147: * @param string $inner
148: * @param null|string $encoding
149: *
150: * @return bool
151: */
152: public static function contains($subject, $inner, $encoding = null)
153: {
154: return Rope::of($subject, $encoding)->contains($inner);
155: }
156: }
157: