Skip to content

Commit 36f6759

Browse files
authored
Merge pull request #38 from WebFiori/dev
Fix Coding Quality Issues
2 parents ced4660 + de7fa80 commit 36f6759

10 files changed

+126
-130
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@ test.*
44
.phpunit.result.cache
55
clover.xml
66
*.phar
7+
.idea/*
8+
vendor/*
9+
composer.lock
10+
.php-cs-fixer.cache

README.md

+13-13
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,26 @@ A helper class library for creating JSON or JSONx strings in PHP. It can be used
1919

2020
## What is JSON?
2121

22-
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.
22+
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.
2323

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

3131
## Supported PHP Versions
32-
| Build Status |
33-
|:-----------:|
34-
|<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>|
35-
|<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>|
36-
|<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>|
37-
|<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>|
38-
|<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>|
39-
|<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>|
40-
|<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>|
41-
|<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)|
32+
| Build Status |
33+
|:-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------:|
34+
| <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> |
35+
| <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> |
36+
| <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> |
37+
| <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> |
38+
| <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> |
39+
| <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> |
40+
| <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> |
41+
| <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> |
4242

4343
## Installation
4444
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.

php_cs.php.dist

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ $finder = PhpCsFixer\Finder::create()
66
->in(__DIR__)
77
;
88

9-
return PhpCsFixer\Config::create()
10-
->setRules([
9+
$config = new PhpCsFixer\Config();
10+
return $config->setRules([
1111
'align_multiline_comment' => [
1212
'comment_type' => 'phpdocs_only'
1313
],
@@ -54,7 +54,7 @@ return PhpCsFixer\Config::create()
5454
'sort_algorithm' => 'alpha'
5555
],
5656
'ordered_class_elements' => [
57-
'sortAlgorithm' => 'alpha',
57+
'sort_algorithm' => 'alpha',
5858
'order' => [
5959
'constant_public',
6060
'constant_protected',

webfiori/json/CaseConverter.php

+21-13
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
11
<?php
2+
/**
3+
* This file is licensed under MIT License.
4+
*
5+
* Copyright (c) 2022 Ibrahim BinAlshikh
6+
*
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/.github/blob/main/LICENSE
9+
*
10+
*/
211
namespace webfiori\json;
312

413
/**
5-
* A class which is used to convert string case from one to another (e.g. camle to snake).
14+
* A class which is used to convert string case from one to another (e.g. camel to snake).
615
*
716
* @author Ibrahim
817
*
@@ -40,17 +49,17 @@ class CaseConverter {
4049
* <li>kebab</li>
4150
* <li>camel</li>
4251
* </ul>
43-
* If the given value is non of the given 3, the string woun't be changed.
52+
* If the given value is none of the given 3, the string wouldn't be changed.
4453
*
4554
* @return string The same string converted to selected style.
4655
*
4756
* @since 1.0
4857
*/
49-
public static function convert($value, $style) {
58+
public static function convert(string $value, string $style) : string {
5059
if ($style == 'snake') {
5160
return self::toSnackCase($value);
5261
} else if ($style == 'kebab') {
53-
return self::toKebabCase($value);
62+
return self::toKebabCase($value);
5463
} else if ($style == 'camel') {
5564
return self::toCamelCase($value);
5665
} else {
@@ -67,7 +76,7 @@ public static function convert($value, $style) {
6776
*
6877
* @since 1.0
6978
*/
70-
public static function toCamelCase($value) {
79+
public static function toCamelCase(string $value) : string {
7180
$retVal = '';
7281
$changeNextCharCase = false;
7382
$valueTrim = trim($value);
@@ -100,7 +109,7 @@ public static function toCamelCase($value) {
100109
*
101110
* @since 1.0
102111
*/
103-
public static function toKebabCase($value) {
112+
public static function toKebabCase(string $value) : string {
104113
return self::_toSnakeOrKebab($value, '_', '-');
105114
}
106115
/**
@@ -113,13 +122,13 @@ public static function toKebabCase($value) {
113122
*
114123
* @since 1.0
115124
*/
116-
public static function toSnackCase($value) {
125+
public static function toSnackCase(string $value) : string {
117126
return self::_toSnakeOrKebab($value, '-', '_');
118127
}
119-
private static function _isUpper($char) {
128+
private static function _isUpper(string $char) : bool {
120129
return $char >= 'A' && $char <= 'Z';
121130
}
122-
private static function _toSnakeOrKebab($value, $from, $to) {
131+
private static function _toSnakeOrKebab(string $value, string $from, string $to) : string {
123132
$attr1 = str_replace($from, $to, trim($value));
124133
$retVal = '';
125134
$isNumFound = false;
@@ -140,17 +149,16 @@ private static function _toSnakeOrKebab($value, $from, $to) {
140149

141150
return $retVal;
142151
}
143-
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
152+
private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) : string {
144153
$isUpper = self::_isUpper($char);
145154
$retVal = '';
146155

147156
if (($isUpper || $isNumFound) && $x != 0 && !$snakeOrKebabFound) {
148157
$retVal .= $to.strtolower($char);
149158
} else if ($isUpper && $x == 0) {
150159
$retVal .= strtolower($char);
151-
} else if ($isUpper && $x != 0 && $snakeOrKebabFound) {
160+
} else if ($isUpper && $x != 0 && $snakeOrKebabFound) {
152161
$retVal .= strtolower($char);
153-
154162
} else {
155163
$retVal .= $char;
156164
}
@@ -160,7 +168,7 @@ private static function addChar($x, &$isNumFound, $to, $char, &$snakeOrKebabFoun
160168
return $retVal;
161169
}
162170

163-
private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) {
171+
private static function addNumber($x, &$isNumFound, $to, $char, &$snakeOrKebabFound) : string {
164172
$retVal = '';
165173

166174
if ($x == 0) {

webfiori/json/Json.php

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
11
<?php
2-
/*
3-
* The MIT License
4-
*
5-
* Copyright 2019 Ibrahim, WebFiori Json library.
2+
/**
3+
* This file is licensed under MIT License.
64
*
7-
* Permission is hereby granted, free of charge, to any person obtaining a copy
8-
* of this software and associated documentation files (the "Software"), to deal
9-
* in the Software without restriction, including without limitation the rights
10-
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
* copies of the Software, and to permit persons to whom the Software is
12-
* furnished to do so, subject to the following conditions:
5+
* Copyright (c) 2019 Ibrahim BinAlshikh
136
*
14-
* The above copyright notice and this permission notice shall be included in
15-
* all copies or substantial portions of the Software.
7+
* For more information on the license, please visit:
8+
* https://github.com/WebFiori/.github/blob/main/LICENSE
169
*
17-
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23-
* THE SOFTWARE.
2410
*/
2511
namespace webfiori\json;
2612

@@ -95,12 +81,12 @@ class Json {
9581
* Creates new instance of the class.
9682
*
9783
* @param array|string $initialData Initial data which is used to initialize
98-
* the object. It can be a string which looks like JSON or it can be an
84+
* the object. It can be a string which looks like JSON, or it can be an
9985
* associative array. If it is an associative array, then the keys will be
10086
* acting as properties and the value of each key will be the value of
10187
* the property.
10288
*
103-
* @param boolean $isFormatted If this attribute is set to true, the generated
89+
* @param bool $isFormatted If this attribute is set to true, the generated
10490
* JSON will be indented and have new lines (readable). Note that the parameter
10591
* will be ignored if the constant 'WF_VERBOSE' is defined and is set to true.
10692
*
@@ -124,9 +110,9 @@ public function __construct(array $initialData = [], $isFormatted = false) {
124110
* @param string $key The value of the key. Note that the style of the key
125111
* does not matter.
126112
*
127-
* @return Json|mixed|null The return type will depends on the value which
113+
* @return Json|mixed|null The return type will depend on the value which
128114
* was set by any method which can be used to add props. It can be a number,
129-
* a boolean, string, an object or null if does not exist.
115+
* a boolean, string, an object or null if it does not exist.
130116
*
131117
* @since 1.2
132118
*/
@@ -189,7 +175,7 @@ public function __toString() {
189175
* is an array. If set to true, the array will be added as an object.
190176
* Default is false.
191177
*
192-
* @return boolean The method will return true if the value is set.
178+
* @return bool The method will return true if the value is set.
193179
* If the given value or key is invalid, the method will return false.
194180
*
195181
* @since 1.1
@@ -215,11 +201,11 @@ public function add(string $key, $value, $arrayAsObj = false) {
215201
*
216202
* @param array $value The array that will be added.
217203
*
218-
* @param boolean $asObject If this parameter is set to true,
204+
* @param bool $asObject If this parameter is set to true,
219205
* the array will be added as an object in JSON string. Note that if the
220206
* array is associative, each index will be added as an object. Default is false.
221207
*
222-
* @return boolean The method will return false if the given key is invalid
208+
* @return bool The method will return false if the given key is invalid
223209
* or the given value is not an array.
224210
*/
225211
public function addArray(string $key, $value, $asObject = false) {
@@ -248,16 +234,16 @@ public function addArray(string $key, $value, $asObject = false) {
248234
*
249235
* @param string $key The name of the key.
250236
*
251-
* @param boolean $val true or false. If not specified,
237+
* @param bool $val true or false. If not specified,
252238
* The default will be true.
253239
*
254-
* @return boolean The method will return true in case the value is set.
240+
* @return bool The method will return true in case the value is set.
255241
* If the given value is not a boolean or the key value is invalid string,
256242
* the method will return false.
257243
*
258244
* @since 1.0
259245
*/
260-
public function addBoolean($key, $val = true) {
246+
public function addBoolean($key, $val = true) : bool {
261247
if (!$this->updateExisting($key, $val)) {
262248
$prop = $this->createProb($key, $val);
263249

@@ -294,10 +280,10 @@ public function addMultiple(array $arr) {
294280
*
295281
* @param string $key The name of value key.
296282
*
297-
* @return boolean The method will return true if the value is set.
283+
* @return bool The method will return true if the value is set.
298284
* If the given value or key is invalid, the method will return false.
299285
*/
300-
public function addNull(string $key) {
286+
public function addNull(string $key) : bool {
301287
$nul = null;
302288

303289
if (!$this->updateExisting($key, $nul)) {
@@ -327,7 +313,7 @@ public function addNull(string $key) {
327313
*
328314
* @param int|double $value The value of the key.
329315
*
330-
* @return boolean The method will return true in case the number is
316+
* @return bool The method will return true in case the number is
331317
* added. If the given value is not a number or the key value is invalid
332318
* string, the method
333319
* will return false.
@@ -356,19 +342,19 @@ public function addNumber(string $key, $value) {
356342
* The object that will be added can implement the interface JsonI to make
357343
* the generated JSON string customizable. Also, the object can be of
358344
* type Json. If the given value is an object that does not implement the
359-
* interface JsonI or it is not of type Json,
345+
* interface JsonI, or it is not of type Json,
360346
* The method will try to extract object information based on its "getXxxxx()" public
361347
* methods. Assuming that the object has 2 public methods with names
362348
* <code>getFirstProp()</code> and <code>getSecondProp()</code>.
363-
* In that case, the generated JSON will be on the formate
349+
* In that case, the generated JSON will be on the format
364350
* <b>{"FirstProp":"prop-1","SecondProp":""}</b>.
365351
* This method also can be used to update the value of an existing property.
366352
*
367353
* @param string $key The key value.
368354
*
369355
* @param JsonI|Json|object $val The object that will be added.
370356
*
371-
* @return boolean The method will return true if the object is added.
357+
* @return bool The method will return true if the object is added.
372358
* If the key value is invalid string, the method will return false.
373359
*
374360
* @since 1.0
@@ -394,11 +380,11 @@ public function addObject(string $key, &$val) {
394380
*
395381
* This method also can be used to update the value of an existing property.
396382
*
397-
* @param string $key The name of the key. Must be non empty string.
383+
* @param string $key The name of the key. Must be non-empty string.
398384
*
399385
* @param string $val The value of the string.
400386
*
401-
* @return boolean The method will return true in case the string is added.
387+
* @return bool The method will return true in case the string is added.
402388
* If the given value is not a string or the given key is invalid, the
403389
* method will return false.
404390
*
@@ -558,7 +544,7 @@ public function getPropStyle() {
558544
*
559545
* @param string $key The name of the key.
560546
*
561-
* @return boolean The method will return true if the
547+
* @return bool The method will return true if the
562548
* key exists. false if not.
563549
*
564550
* @since 1.2
@@ -574,7 +560,8 @@ public function hasKey($key) {
574560
* This can be used to make the generated output readable by adding
575561
* indentation and new lines.
576562
*
577-
* @return boolean True if will be formatted. False otherwise.
563+
* @return bool The method will return true if the output will be formatted.
564+
* False otherwise.
578565
*/
579566
public function isFormatted() {
580567
return $this->formatted;
@@ -616,7 +603,7 @@ public function remove($keyName) {
616603
* a tree. If not formatted, the output string will be generated as one line.
617604
*
618605
*
619-
* @param boolean $bool True to make the output formatted and false to make
606+
* @param bool $bool True to make the output formatted and false to make
620607
* it not.
621608
*
622609
* @since 1.2.5
@@ -671,7 +658,7 @@ public function setPropsStyle($style) {
671658
* sent to. If the file does not exist, the method will attempt to create it.
672659
*
673660
* @param string $path The folder in file system that the file will be created
674-
* at. If does not exist, the method will attempt to create it.
661+
* at. If it does not exist, the method will attempt to create it.
675662
*
676663
* @param bool $override If a file exist in the specified location with same
677664
* name and this parameter is set to true, the method will override existing

0 commit comments

Comments
 (0)