diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..1b01aa4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +build +vendor +.idea +composer.lock +phpunit.xml \ No newline at end of file diff --git a/Helper.php b/Helper.php index 084bfa3..614fb4c 100644 --- a/Helper.php +++ b/Helper.php @@ -264,39 +264,42 @@ function str_snake_case_safe($title, $separator = '_') */ function str_json($str = '/', $returnDecoded = true, $asArray = false) { - if ((str_starts('{', $str) == true) and (str_ends('}', $str) == true)) { + $ciEnv = function_exists('log_message') ? true : false; + + if ((str_starts('{', $str) == true) && (str_ends('}', $str) == true)) { $json = json_decode($str, $asArray); + switch (json_last_error()) { case JSON_ERROR_NONE: return ($returnDecoded) ? $json : true; break; case JSON_ERROR_DEPTH: - log_message('debug', 'str_json: Maximum stack depth exceeded.'); + !$ciEnv || log_message('debug', 'str_json: Maximum stack depth exceeded.'); return false; break; case JSON_ERROR_STATE_MISMATCH: - log_message('debug', 'str_json: Underflow or the modes mismatch.'); + !$ciEnv || log_message('debug', 'str_json: Underflow or the modes mismatch.'); return false; break; case JSON_ERROR_CTRL_CHAR: - log_message('debug', 'str_json: Unexpected control character found.'); + !$ciEnv || log_message('debug', 'str_json: Unexpected control character found.'); return false; break; case JSON_ERROR_SYNTAX: - log_message('debug', 'str_json: Syntax error, malformed JSON.'); + !$ciEnv || log_message('debug', 'str_json: Syntax error, malformed JSON.'); return false; break; case JSON_ERROR_UTF8: - log_message('debug', 'str_json: Malformed UTF-8 characters, possibly incorrectly encoded.'); + !$ciEnv || log_message('debug', 'str_json: Malformed UTF-8 characters, possibly incorrectly encoded.'); return false; break; default: - log_message('debug', 'str_json: Unknown error.'); + !$ciEnv || log_message('debug', 'str_json: Unknown error.'); return false; break; } } else { - log_message('debug', 'str_json: String does not starts or ends properly.'); + !$ciEnv || log_message('debug', 'str_json: String does not starts or ends properly.'); return false; } } diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..2474fe2 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,21 @@ +# The MIT License (MIT) + +Copyright (c) 2019-2023 Yahya Erturan + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..3417b2f --- /dev/null +++ b/README.md @@ -0,0 +1,36 @@ +# Str - Str Utilities for PHP + +- str_starts($needles, $haystack) +- str_contains($needles, $haystack) +- str_ends($needles, $haystack) +- str_limit($value, $limit = 100, $end = '...') { +- str_snake_case($value, $delimiter = '\_') +- str_camel_case($value) +- str_studly_case($value) +- str_slug($title, $separator = '-') +- str_snake_case_safe($title, $separator = '_') +- str_json($str = '/', $returnDecoded = true, $asArray = false) + +## Install + +Install the latest version using [Composer](https://getcomposer.org/): + +``` +$ composer require vayes/str +``` + +Not available via composer yet. + +## Usage + +Import and use as it is: + +```php +use function Vayes\Str\str_camel_case + +$property = str_camel_case('snaked_case_string'); +``` + +## License + +[MIT license](LICENSE.md) \ No newline at end of file