Skip to content

Commit

Permalink
Merge pull request #38 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix Coding Quality Issues
  • Loading branch information
usernane authored Feb 28, 2023
2 parents ced4660 + de7fa80 commit 36f6759
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 130 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ test.*
.phpunit.result.cache
clover.xml
*.phar
.idea/*
vendor/*
composer.lock
.php-cs-fixer.cache
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,26 @@ A helper class library for creating JSON or JSONx strings in PHP. It can be used

## What is JSON?

Accoording to [json.org](https://www.json.org/json-en.html), JSON is a data exchange format which is based partially on JavaScript. It is easy for humans to read and for machines to understand. JSON data is represented as pairs of keys and values.
According to [json.org](https://www.json.org/json-en.html), JSON is a data exchange format which is based partially on JavaScript. It is easy for humans to read and for machines to understand. JSON data is represented as pairs of keys and values.

## Library Features
* Support fo creating well formatted JSON.
* Support for creating [JSONx](https://www.ibm.com/docs/en/datapower-gateways/10.0.1?topic=20-jsonx).
* Ability to decode JSON strings and convert them to `Json` objects.
* Ability to read JSON files and map JSON values to PHP datatypes.
* Ability to manupulate JSON properties as needed.
* Ability to read JSON files and map JSON values to PHP data types.
* Ability to manipulate JSON properties as needed.

## Supported PHP Versions
| Build Status |
|:-----------:|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php70.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.0/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php71.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.1/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php72.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.2/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php73.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.3/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php74.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.4/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%208.0/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%208.1/badge.svg?branch=master"></a>|
|<a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%208.2/badge.svg?branch=dev"></a><br>(dev)|
| Build Status |
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php70.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.0/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php71.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.1/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php72.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.2/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php73.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.3/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php74.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%207.4/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php80.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%208.0/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php81.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%208.1/badge.svg?branch=master"></a> |
| <a target="_blank" href="https://github.com/WebFiori/json/actions/workflows/php82.yml"><img src="https://github.com/WebFiori/json/workflows/Build%20PHP%208.2/badge.svg?branch=master"></a> |

## Installation
If you are using composer to manage your dependencies, then it is possible to install the library by including the entry `"webfiori/jsonx":"*"` in the `require` section of your `composer.json` file to install the latest release.
Expand Down
6 changes: 3 additions & 3 deletions php_cs.php.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $finder = PhpCsFixer\Finder::create()
->in(__DIR__)
;

return PhpCsFixer\Config::create()
->setRules([
$config = new PhpCsFixer\Config();
return $config->setRules([
'align_multiline_comment' => [
'comment_type' => 'phpdocs_only'
],
Expand Down Expand Up @@ -54,7 +54,7 @@ return PhpCsFixer\Config::create()
'sort_algorithm' => 'alpha'
],
'ordered_class_elements' => [
'sortAlgorithm' => 'alpha',
'sort_algorithm' => 'alpha',
'order' => [
'constant_public',
'constant_protected',
Expand Down
34 changes: 21 additions & 13 deletions webfiori/json/CaseConverter.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?php
/**
* This file is licensed under MIT License.
*
* Copyright (c) 2022 Ibrahim BinAlshikh
*
* For more information on the license, please visit:
* https://github.com/WebFiori/.github/blob/main/LICENSE
*
*/
namespace webfiori\json;

/**
* A class which is used to convert string case from one to another (e.g. camle to snake).
* A class which is used to convert string case from one to another (e.g. camel to snake).
*
* @author Ibrahim
*
Expand Down Expand Up @@ -40,17 +49,17 @@ class CaseConverter {
* <li>kebab</li>
* <li>camel</li>
* </ul>
* If the given value is non of the given 3, the string woun't be changed.
* If the given value is none of the given 3, the string wouldn't be changed.
*
* @return string The same string converted to selected style.
*
* @since 1.0
*/
public static function convert($value, $style) {
public static function convert(string $value, string $style) : string {
if ($style == 'snake') {
return self::toSnackCase($value);
} else if ($style == 'kebab') {
return self::toKebabCase($value);
return self::toKebabCase($value);
} else if ($style == 'camel') {
return self::toCamelCase($value);
} else {
Expand All @@ -67,7 +76,7 @@ public static function convert($value, $style) {
*
* @since 1.0
*/
public static function toCamelCase($value) {
public static function toCamelCase(string $value) : string {
$retVal = '';
$changeNextCharCase = false;
$valueTrim = trim($value);
Expand Down Expand Up @@ -100,7 +109,7 @@ public static function toCamelCase($value) {
*
* @since 1.0
*/
public static function toKebabCase($value) {
public static function toKebabCase(string $value) : string {
return self::_toSnakeOrKebab($value, '_', '-');
}
/**
Expand All @@ -113,13 +122,13 @@ public static function toKebabCase($value) {
*
* @since 1.0
*/
public static function toSnackCase($value) {
public static function toSnackCase(string $value) : string {
return self::_toSnakeOrKebab($value, '-', '_');
}
private static function _isUpper($char) {
private static function _isUpper(string $char) : bool {
return $char >= 'A' && $char <= 'Z';
}
private static function _toSnakeOrKebab($value, $from, $to) {
private static function _toSnakeOrKebab(string $value, string $from, string $to) : string {
$attr1 = str_replace($from, $to, trim($value));
$retVal = '';
$isNumFound = false;
Expand All @@ -140,17 +149,16 @@ private static function _toSnakeOrKebab($value, $from, $to) {

return $retVal;
}
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) : string {
$isUpper = self::_isUpper($char);
$retVal = '';

if (($isUpper || $isNumFound) && $x != 0 && !$snakeOrKebabFound) {
$retVal .= $to.strtolower($char);
} else if ($isUpper && $x == 0) {
$retVal .= strtolower($char);
} else if ($isUpper && $x != 0 && $snakeOrKebabFound) {
} else if ($isUpper && $x != 0 && $snakeOrKebabFound) {
$retVal .= strtolower($char);

} else {
$retVal .= $char;
}
Expand All @@ -160,7 +168,7 @@ private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFoun
return $retVal;
}

private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) : string {
$retVal = '';

if ($x == 0) {
Expand Down
69 changes: 28 additions & 41 deletions webfiori/json/Json.php
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
<?php
/*
* The MIT License
*
* Copyright 2019 Ibrahim, WebFiori Json library.
/**
* This file is licensed under MIT License.
*
* 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:
* Copyright (c) 2019 Ibrahim BinAlshikh
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* For more information on the license, please visit:
* https://github.com/WebFiori/.github/blob/main/LICENSE
*
* 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.
*/
namespace webfiori\json;

Expand Down Expand Up @@ -95,12 +81,12 @@ class Json {
* Creates new instance of the class.
*
* @param array|string $initialData Initial data which is used to initialize
* the object. It can be a string which looks like JSON or it can be an
* the object. It can be a string which looks like JSON, or it can be an
* associative array. If it is an associative array, then the keys will be
* acting as properties and the value of each key will be the value of
* the property.
*
* @param boolean $isFormatted If this attribute is set to true, the generated
* @param bool $isFormatted If this attribute is set to true, the generated
* JSON will be indented and have new lines (readable). Note that the parameter
* will be ignored if the constant 'WF_VERBOSE' is defined and is set to true.
*
Expand All @@ -124,9 +110,9 @@ public function __construct(array $initialData = [], $isFormatted = false) {
* @param string $key The value of the key. Note that the style of the key
* does not matter.
*
* @return Json|mixed|null The return type will depends on the value which
* @return Json|mixed|null The return type will depend on the value which
* was set by any method which can be used to add props. It can be a number,
* a boolean, string, an object or null if does not exist.
* a boolean, string, an object or null if it does not exist.
*
* @since 1.2
*/
Expand Down Expand Up @@ -189,7 +175,7 @@ public function __toString() {
* is an array. If set to true, the array will be added as an object.
* Default is false.
*
* @return boolean The method will return true if the value is set.
* @return bool The method will return true if the value is set.
* If the given value or key is invalid, the method will return false.
*
* @since 1.1
Expand All @@ -215,11 +201,11 @@ public function add(string $key, $value, $arrayAsObj = false) {
*
* @param array $value The array that will be added.
*
* @param boolean $asObject If this parameter is set to true,
* @param bool $asObject If this parameter is set to true,
* the array will be added as an object in JSON string. Note that if the
* array is associative, each index will be added as an object. Default is false.
*
* @return boolean The method will return false if the given key is invalid
* @return bool The method will return false if the given key is invalid
* or the given value is not an array.
*/
public function addArray(string $key, $value, $asObject = false) {
Expand Down Expand Up @@ -248,16 +234,16 @@ public function addArray(string $key, $value, $asObject = false) {
*
* @param string $key The name of the key.
*
* @param boolean $val true or false. If not specified,
* @param bool $val true or false. If not specified,
* The default will be true.
*
* @return boolean The method will return true in case the value is set.
* @return bool The method will return true in case the value is set.
* If the given value is not a boolean or the key value is invalid string,
* the method will return false.
*
* @since 1.0
*/
public function addBoolean($key, $val = true) {
public function addBoolean($key, $val = true) : bool {
if (!$this->updateExisting($key, $val)) {
$prop = $this->createProb($key, $val);

Expand Down Expand Up @@ -294,10 +280,10 @@ public function addMultiple(array $arr) {
*
* @param string $key The name of value key.
*
* @return boolean The method will return true if the value is set.
* @return bool The method will return true if the value is set.
* If the given value or key is invalid, the method will return false.
*/
public function addNull(string $key) {
public function addNull(string $key) : bool {
$nul = null;

if (!$this->updateExisting($key, $nul)) {
Expand Down Expand Up @@ -327,7 +313,7 @@ public function addNull(string $key) {
*
* @param int|double $value The value of the key.
*
* @return boolean The method will return true in case the number is
* @return bool The method will return true in case the number is
* added. If the given value is not a number or the key value is invalid
* string, the method
* will return false.
Expand Down Expand Up @@ -356,19 +342,19 @@ public function addNumber(string $key, $value) {
* The object that will be added can implement the interface JsonI to make
* the generated JSON string customizable. Also, the object can be of
* type Json. If the given value is an object that does not implement the
* interface JsonI or it is not of type Json,
* interface JsonI, or it is not of type Json,
* The method will try to extract object information based on its "getXxxxx()" public
* methods. Assuming that the object has 2 public methods with names
* <code>getFirstProp()</code> and <code>getSecondProp()</code>.
* In that case, the generated JSON will be on the formate
* In that case, the generated JSON will be on the format
* <b>{"FirstProp":"prop-1","SecondProp":""}</b>.
* This method also can be used to update the value of an existing property.
*
* @param string $key The key value.
*
* @param JsonI|Json|object $val The object that will be added.
*
* @return boolean The method will return true if the object is added.
* @return bool The method will return true if the object is added.
* If the key value is invalid string, the method will return false.
*
* @since 1.0
Expand All @@ -394,11 +380,11 @@ public function addObject(string $key, &$val) {
*
* This method also can be used to update the value of an existing property.
*
* @param string $key The name of the key. Must be non empty string.
* @param string $key The name of the key. Must be non-empty string.
*
* @param string $val The value of the string.
*
* @return boolean The method will return true in case the string is added.
* @return bool The method will return true in case the string is added.
* If the given value is not a string or the given key is invalid, the
* method will return false.
*
Expand Down Expand Up @@ -558,7 +544,7 @@ public function getPropStyle() {
*
* @param string $key The name of the key.
*
* @return boolean The method will return true if the
* @return bool The method will return true if the
* key exists. false if not.
*
* @since 1.2
Expand All @@ -574,7 +560,8 @@ public function hasKey($key) {
* This can be used to make the generated output readable by adding
* indentation and new lines.
*
* @return boolean True if will be formatted. False otherwise.
* @return bool The method will return true if the output will be formatted.
* False otherwise.
*/
public function isFormatted() {
return $this->formatted;
Expand Down Expand Up @@ -616,7 +603,7 @@ public function remove($keyName) {
* a tree. If not formatted, the output string will be generated as one line.
*
*
* @param boolean $bool True to make the output formatted and false to make
* @param bool $bool True to make the output formatted and false to make
* it not.
*
* @since 1.2.5
Expand Down Expand Up @@ -671,7 +658,7 @@ public function setPropsStyle($style) {
* sent to. If the file does not exist, the method will attempt to create it.
*
* @param string $path The folder in file system that the file will be created
* at. If does not exist, the method will attempt to create it.
* at. If it does not exist, the method will attempt to create it.
*
* @param bool $override If a file exist in the specified location with same
* name and this parameter is set to true, the method will override existing
Expand Down
Loading

0 comments on commit 36f6759

Please sign in to comment.