LFT
  • Namespace
  • Class
  • Tree

Namespaces

  • Docoflow
    • Contracts
    • Entity
    • Facades
    • Models
    • Traits
  • Docolight
    • Agno
      • Traits
    • Container
    • Http
      • Contracts
    • Rest
      • Handler
      • Http
    • Support
      • Debug
      • Traits
  • Docotory
    • Traits
  • None

Classes

  • Docoflow\Docoflow
  • Docoflow\Entity\Group
  • Docoflow\Entity\Step
  • Docoflow\Entity\Verificator
  • Docoflow\Facades\Action
  • Docoflow\Facades\Activity
  • Docoflow\Facades\Flo
  • Docoflow\Facades\StateActivity
  • Docoflow\Flo
  • Docoflow\Models\Workflow
  • Docoflow\Models\WorkflowAction
  • Docoflow\Models\WorkflowActivity
  • Docoflow\Models\WorkflowGroups
  • Docoflow\Models\WorkflowNotification
  • Docoflow\Models\WorkflowState
  • Docoflow\Models\WorkflowStateActivity
  • Docoflow\Models\WorkflowStep
  • Docoflow\Models\WorkflowVerificator
  • Docolight
  • Docolight\Agno\AgnoModule
  • Docolight\Container\Container
  • Docolight\Http\Headers
  • Docolight\Http\JsonResponse
  • Docolight\Http\MimeResponse
  • Docolight\Http\Response
  • Docolight\Http\ResponseFactory
  • Docolight\Rest\Handler\RestfulErrorHandler
  • Docolight\Rest\Http\RestFulController
  • Docolight\Support\ActiveRecordWrapper
  • Docolight\Support\Arr
  • Docolight\Support\Carbonate
  • Docolight\Support\ClassLoader
  • Docolight\Support\Collection
  • Docolight\Support\CollectionDataProvider
  • Docolight\Support\Debug\Dumper
  • Docolight\Support\Debug\HtmlDumper
  • Docolight\Support\Facade
  • Docolight\Support\Factory
  • Docolight\Support\Fluent
  • Docolight\Support\Html
  • Docolight\Support\IterablePager
  • Docolight\Support\Repository
  • Docolight\Support\Set
  • Docolight\Support\Str
  • Docotory\Factory

Interfaces

  • Docoflow\Contracts\DocoflowContract
  • Docoflow\Contracts\ValidationStatus
  • Docolight\Http\Contracts\Arrayable

Traits

  • Docoflow\Traits\BulkValidator
  • Docoflow\Traits\Entity
  • Docoflow\Traits\HasMutator
  • Docoflow\Traits\Validable
  • Docolight\Agno\Traits\HasAssetsUrl
  • Docolight\Agno\Traits\HasAutoload
  • Docolight\Support\Traits\Macroable
  • Docotory\Traits\HasFactories

Exceptions

  • Docolight\Container\BindingResolutionException
  • Docotory\ResolvingTypeException

Functions

  • array_add
  • array_build
  • array_collapse
  • array_divide
  • array_dot
  • array_except
  • array_first
  • array_flatten
  • array_forget
  • array_get
  • array_has
  • array_last
  • array_only
  • array_pluck
  • array_pull
  • array_replace_value
  • array_set
  • array_sort
  • array_sort_recursive
  • array_where
  • cache
  • camel_case
  • class_basename
  • class_uses_recursive
  • collect
  • container
  • data_get
  • dd
  • def
  • dump
  • e
  • ends_with
  • fluent
  • get
  • head
  • input
  • last
  • object_get
  • preg_replace_sub
  • request
  • response
  • session
  • snake_case
  • starts_with
  • str_contains
  • str_finish
  • str_is
  • str_limit
  • str_random
  • str_replace_array
  • str_slug
  • studly_case
  • title_case
  • trait_uses_recursive
  • transaction
  • trimtolower
  • value
  • with
  1 <?php
  2 
  3 namespace Docolight\Support;
  4 
  5 use RuntimeException;
  6 use Docolight\Support\Traits\Macroable;
  7 
  8 /**
  9  * String helper.
 10  *
 11  * @author Krisan Alfa Timur <krisanalfa@docotel.co.id>
 12  */
 13 class Str
 14 {
 15     use Macroable;
 16 
 17     /**
 18      * The cache of snake-cased words.
 19      *
 20      * @var array
 21      */
 22     protected static $snakeCache = [];
 23 
 24     /**
 25      * The cache of camel-cased words.
 26      *
 27      * @var array
 28      */
 29     protected static $camelCache = [];
 30 
 31     /**
 32      * The cache of studly-cased words.
 33      *
 34      * @var array
 35      */
 36     protected static $studlyCache = [];
 37 
 38     /**
 39      * Convert a value to camel case.
 40      *
 41      * @param string $value
 42      *
 43      * @return string
 44      */
 45     public static function camel($value)
 46     {
 47         if (isset(static::$camelCache[$value])) {
 48             return static::$camelCache[$value];
 49         }
 50 
 51         return static::$camelCache[$value] = lcfirst(static::studly($value));
 52     }
 53 
 54     /**
 55      * Determine if a given string contains a given substring.
 56      *
 57      * @param string       $haystack
 58      * @param string|array $needles
 59      *
 60      * @return bool
 61      */
 62     public static function contains($haystack, $needles)
 63     {
 64         foreach ((array) $needles as $needle) {
 65             if ($needle != '' and strpos($haystack, $needle) !== false) {
 66                 return true;
 67             }
 68         }
 69 
 70         return false;
 71     }
 72 
 73     /**
 74      * Determine if a given string ends with a given substring.
 75      *
 76      * @param string       $haystack
 77      * @param string|array $needles
 78      *
 79      * @return bool
 80      */
 81     public static function endsWith($haystack, $needles)
 82     {
 83         foreach ((array) $needles as $needle) {
 84             if ((string) $needle === substr($haystack, -strlen($needle))) {
 85                 return true;
 86             }
 87         }
 88 
 89         return false;
 90     }
 91 
 92     /**
 93      * Cap a string with a single instance of a given value.
 94      *
 95      * @param string $value
 96      * @param string $cap
 97      *
 98      * @return string
 99      */
100     public static function finish($value, $cap)
101     {
102         $quoted = preg_quote($cap, '/');
103 
104         return preg_replace('/(?:'.$quoted.')+$/', '', $value).$cap;
105     }
106 
107     /**
108      * Determine if a given string matches a given pattern.
109      *
110      * @param string $pattern
111      * @param string $value
112      *
113      * @return bool
114      */
115     public static function is($pattern, $value)
116     {
117         if ($pattern == $value) {
118             return true;
119         }
120 
121         $pattern = preg_quote($pattern, '#');
122 
123         // Asterisks are translated into zero-or-more regular expression wildcards
124         // to make it convenient to check if the strings starts with the given
125         // pattern such as "library/*", making any string check convenient.
126         $pattern = str_replace('\*', '.*', $pattern).'\z';
127 
128         return (bool) preg_match('#^'.$pattern.'#', $value);
129     }
130 
131     /**
132      * Return the length of the given string.
133      *
134      * @param string $value
135      *
136      * @return int
137      */
138     public static function length($value)
139     {
140         return mb_strlen($value);
141     }
142 
143     /**
144      * Limit the number of characters in a string.
145      *
146      * @param string $value
147      * @param int    $limit
148      * @param string $end
149      *
150      * @return string
151      */
152     public static function limit($value, $limit = 100, $end = '...')
153     {
154         if (mb_strwidth($value, 'UTF-8') <= $limit) {
155             return $value;
156         }
157 
158         return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
159     }
160 
161     /**
162      * Convert the given string to lower-case.
163      *
164      * @param string $value
165      *
166      * @return string
167      */
168     public static function lower($value)
169     {
170         return mb_strtolower($value);
171     }
172 
173     /**
174      * Limit the number of words in a string.
175      *
176      * @param string $value
177      * @param int    $words
178      * @param string $end
179      *
180      * @return string
181      */
182     public static function words($value, $words = 100, $end = '...')
183     {
184         preg_match('/^\s*+(?:\S++\s*+){1,'.$words.'}/u', $value, $matches);
185 
186         if (!isset($matches[0]) || strlen($value) === strlen($matches[0])) {
187             return $value;
188         }
189 
190         return rtrim($matches[0]).$end;
191     }
192 
193     /**
194      * Parse a Class@method style callback into class and method.
195      *
196      * @param string $callback
197      * @param string $default
198      *
199      * @return array
200      */
201     public static function parseCallback($callback, $default)
202     {
203         return static::contains($callback, '@') ? explode('@', $callback, 2) : [$callback, $default];
204     }
205 
206     /**
207      * Generate a more truly "random" alpha-numeric string.
208      *
209      * @param int $length
210      *
211      * @return string
212      *
213      * @throws \RuntimeException
214      */
215     public static function random($length = 16)
216     {
217         $string = '';
218 
219         while (($len = strlen($string)) < $length) {
220             $size = $length - $len;
221 
222             $bytes = static::randomBytes($size);
223 
224             $string .= substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $size);
225         }
226 
227         return $string;
228     }
229 
230     /**
231      * Generate a more truly "random" bytes.
232      *
233      * @param int $length
234      *
235      * @return string
236      *
237      * @throws \RuntimeException
238      */
239     public static function randomBytes($length = 16)
240     {
241         if (function_exists('random_bytes')) {
242             $bytes = random_bytes($length);
243         } elseif (function_exists('openssl_random_pseudo_bytes')) {
244             $bytes = openssl_random_pseudo_bytes($length, $strong);
245             if ($bytes === false || $strong === false) {
246                 throw new RuntimeException('Unable to generate random string.');
247             }
248         } else {
249             throw new RuntimeException('OpenSSL extension is required for PHP 5 users.');
250         }
251 
252         return $bytes;
253     }
254 
255     /**
256      * Generate a "random" alpha-numeric string.
257      *
258      * Should not be considered sufficient for cryptography, etc.
259      *
260      * @param int $length
261      *
262      * @return string
263      */
264     public static function quickRandom($length = 16)
265     {
266         $pool = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
267 
268         return substr(str_shuffle(str_repeat($pool, $length)), 0, $length);
269     }
270 
271     /**
272      * Compares two strings using a constant-time algorithm.
273      *
274      * Note: This method will leak length information.
275      *
276      * Note: Adapted from Symfony\Component\Security\Core\Util\StringUtils.
277      *
278      * @param string $knownString
279      * @param string $userInput
280      *
281      * @return bool
282      */
283     public static function equals($knownString, $userInput)
284     {
285         if (!is_string($knownString)) {
286             $knownString = (string) $knownString;
287         }
288 
289         if (!is_string($userInput)) {
290             $userInput = (string) $userInput;
291         }
292 
293         if (function_exists('hash_equals')) {
294             return hash_equals($knownString, $userInput);
295         }
296 
297         $knownLength = mb_strlen($knownString);
298 
299         if (mb_strlen($userInput) !== $knownLength) {
300             return false;
301         }
302 
303         $result = 0;
304 
305         for ($i = 0; $i < $knownLength; ++$i) {
306             $result |= (ord($knownString[$i]) ^ ord($userInput[$i]));
307         }
308 
309         return 0 === $result;
310     }
311 
312     /**
313      * Convert the given string to upper-case.
314      *
315      * @param string $value
316      *
317      * @return string
318      */
319     public static function upper($value)
320     {
321         return mb_strtoupper($value);
322     }
323 
324     /**
325      * Convert the given string to title case.
326      *
327      * @param string $value
328      *
329      * @return string
330      */
331     public static function title($value)
332     {
333         return mb_convert_case($value, MB_CASE_TITLE, 'UTF-8');
334     }
335 
336     /**
337      * Generate a URL friendly "slug" from a given string.
338      *
339      * @param string $title
340      * @param string $separator
341      *
342      * @return string
343      */
344     public static function slug($title, $separator = '-')
345     {
346         $title = static::ascii($title);
347 
348         // Convert all dashes/underscores into separator
349         $flip = $separator == '-' ? '_' : '-';
350 
351         $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
352 
353         // Remove all characters that are not the separator, letters, numbers, or whitespace.
354         $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title));
355 
356         // Replace all separator characters and whitespace by a single separator
357         $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);
358 
359         return trim($title, $separator);
360     }
361 
362     /**
363      * Convert a string to snake case.
364      *
365      * @param string $value
366      * @param string $delimiter
367      *
368      * @return string
369      */
370     public static function snake($value, $delimiter = '_')
371     {
372         $key = $value.$delimiter;
373 
374         if (isset(static::$snakeCache[$key])) {
375             return static::$snakeCache[$key];
376         }
377 
378         if (!ctype_lower($value)) {
379             $value = strtolower(preg_replace('/(.)(?=[A-Z])/', '$1'.$delimiter, $value));
380         }
381 
382         return static::$snakeCache[$key] = $value;
383     }
384 
385     /**
386      * Determine if a given string starts with a given substring.
387      *
388      * @param string       $haystack
389      * @param string|array $needles
390      *
391      * @return bool
392      */
393     public static function startsWith($haystack, $needles)
394     {
395         foreach ((array) $needles as $needle) {
396             if ($needle != '' and strpos($haystack, $needle) === 0) {
397                 return true;
398             }
399         }
400 
401         return false;
402     }
403 
404     /**
405      * Convert a value to studly caps case.
406      *
407      * @param string $value
408      *
409      * @return string
410      */
411     public static function studly($value)
412     {
413         $key = $value;
414 
415         if (isset(static::$studlyCache[$key])) {
416             return static::$studlyCache[$key];
417         }
418 
419         $value = ucwords(str_replace(['-', '_'], ' ', $value));
420 
421         return static::$studlyCache[$key] = str_replace(' ', '', $value);
422     }
423 
424     /**
425      * Get first section of a string after we split them by a specific separator.
426      *
427      * @param string $string
428      * @param string $separator
429      *
430      * @return mixed
431      */
432     public static function first($string, $separator)
433     {
434         return trim(head(explode($separator, $string)));
435     }
436 
437     /**
438      * Get last section of a string after we split them by a specific separator.
439      *
440      * @param string $string
441      * @param string $separator
442      *
443      * @return mixed
444      */
445     public static function last($string, $separator)
446     {
447         return trim(last(explode($separator, $string)));
448     }
449 }
450 
LFT API documentation generated by ApiGen