generated from crwlrsoft/package-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
155 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,5 @@ | ||
<p align="center"><a href="https://www.crwlr.software" target="_blank"><img src="https://github.com/crwlrsoft/graphics/blob/eee6cf48ee491b538d11b9acd7ee71fbcdbe3a09/crwlr-logo.png" alt="crwlr.software logo" width="260"></a></p> | ||
|
||
# Crwlr Package Template | ||
# Crwlr Utils | ||
|
||
This is the starting point for new crwlr packages. After checkout, you should at least customize the following things: | ||
|
||
- `composer.json`: name, description, keywords, homepage, authors, support (issues, source, docs), autoload path. | ||
- Adapt URLs to file issues in `CONTRIBUTING.md`. | ||
- Remove `ExampleClass` and `ExampleTest` from `src` and `test` and start adding your actual code instead. | ||
- Rewrite the content of this file. | ||
This package contains utilities that are needed in multiple crwlr packages. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
parameters: | ||
level: 8 | ||
level: 9 | ||
paths: | ||
- src | ||
- tests | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace Crwlr\Utils\Exceptions; | ||
|
||
use Exception; | ||
|
||
class InvalidJsonException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace Crwlr\Utils; | ||
|
||
use Crwlr\Utils\Exceptions\InvalidJsonException; | ||
|
||
class Json | ||
{ | ||
/** | ||
* @param string $string | ||
* @return mixed[] | ||
* @throws InvalidJsonException | ||
*/ | ||
public static function stringToArray(string $string): array | ||
{ | ||
$array = json_decode($string, true); | ||
|
||
if (!is_array($array)) { | ||
$array = json_decode(self::fixJsonString($string), true); | ||
|
||
if (!is_array($array)) { | ||
throw new InvalidJsonException('Failed to decode JSON string.'); | ||
} | ||
} | ||
|
||
return $array; | ||
} | ||
|
||
/** | ||
* Try to fix JSON keys without quotes | ||
* | ||
* PHPs json_decode() doesn't work with JSON objects where the keys are not wrapped in quotes. | ||
* This method tries to fix this, when json_decode() fails to parse a JSON string. | ||
*/ | ||
protected static function fixJsonString(string $jsonString): string | ||
{ | ||
return preg_replace_callback( | ||
'/(?:(\w+):(\s*".*?"\s*(?:,|}))|(\w+):(\s*[^"]+?\s*(?:,|})))/i', | ||
function ($match) { | ||
if (count($match) === 3) { | ||
$key = $match[1]; | ||
|
||
$value = $match[2]; | ||
} elseif (count($match) === 5) { | ||
$key = $match[3]; | ||
|
||
$value = $match[4]; | ||
} else { | ||
return $match[0]; | ||
} | ||
|
||
if (!str_starts_with($key, '"')) { | ||
$key = '"' . $key; | ||
} | ||
|
||
if (!str_ends_with($key, '"')) { | ||
$key = $key . '"'; | ||
} | ||
|
||
return $key . ':' . $value; | ||
}, | ||
$jsonString | ||
) ?? $jsonString; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
use Crwlr\Utils\Exceptions\InvalidJsonException; | ||
use Crwlr\Utils\Json; | ||
|
||
it('converts a valid JSON string to an array', function () { | ||
$jsonString = <<<JSON | ||
{ | ||
"foo": "one", | ||
"bar": 2, | ||
"baz": ["three", true, 5.1] | ||
} | ||
JSON; | ||
|
||
expect(Json::stringToArray($jsonString))->toBe([ | ||
'foo' => 'one', | ||
'bar' => 2, | ||
'baz' => ['three', true, 5.1], | ||
]); | ||
}); | ||
|
||
it('works with JS style JSON objects without quotes around keys', function () { | ||
$jsonString = <<<JSON | ||
{ | ||
foo: "one", | ||
bar: "two", | ||
"baz": "three" | ||
} | ||
JSON; | ||
|
||
expect(Json::stringToArray($jsonString))->toBe(['foo' => 'one', 'bar' => 'two', 'baz' => 'three']); | ||
}); | ||
|
||
it('correctly fixes keys without quotes, even when values contain colons', function () { | ||
$jsonString = <<<JSON | ||
{ | ||
foo: "https://www.example.com", | ||
bar: 2, | ||
"baz": "some: thing" | ||
} | ||
JSON; | ||
|
||
expect(Json::stringToArray($jsonString))->toBe([ | ||
'foo' => 'https://www.example.com', | ||
'bar' => 2, | ||
'baz' => 'some: thing', | ||
]); | ||
}); | ||
|
||
it('correctly fixes keys without quotes, when the value is an empty string', function () { | ||
$jsonString = <<<JSON | ||
{ | ||
foo: "", | ||
"bar": "baz" | ||
} | ||
JSON; | ||
|
||
expect(Json::stringToArray($jsonString))->toBe([ | ||
'foo' => '', | ||
'bar' => 'baz', | ||
]); | ||
}); | ||
|
||
it('throws an exception when the string is not a (valid) JSON string', function () { | ||
Json::stringToArray('{ foo: bar ]'); | ||
})->throws(InvalidJsonException::class); |