Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate urls when normalizing to JSON (#380)
migrate/fixup URLs when normalizing to JSON followup of #35 --------- Signed-off-by: Jan Kowalleck <jan.kowalleck@gmail.com>
- Loading branch information
1 parent
02d05eb
commit 3b5482a
Showing
14 changed files
with
185 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
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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of CycloneDX PHP Library. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
|
||
namespace CycloneDX\Core\_helpers; | ||
|
||
use Opis\JsonSchema\Formats\IriFormats; | ||
|
||
/** | ||
* Namespace of functions related to JSON. | ||
* | ||
* @internal as this class may be affected by breaking changes without notice | ||
* | ||
* @author jkowalleck | ||
*/ | ||
abstract class JSON | ||
{ | ||
use UriTrait; | ||
|
||
/** | ||
* Make a string valid to JSON::iri-reference spec - best-effort. | ||
* | ||
* Complete and failsafe implementation is pretty context-dependent. | ||
* Best-effort solution: replacement & drop every URI that is not well-formed already. | ||
* | ||
* @see UriTrait::fixUriBE | ||
* @see filterIriReference | ||
* | ||
* @return string|null string on success; null if encoding failed, or input was null | ||
*/ | ||
public static function encodeIriReferenceBE(?string $uri): ?string | ||
{ | ||
$uri = self::fixUriBE($uri); | ||
|
||
return null === $uri || self::filterIriReference($uri) | ||
? $uri | ||
: null; // @codeCoverageIgnore | ||
} | ||
|
||
/** @SuppressWarnings(PHPMD.StaticAccess) */ | ||
public static function filterIriReference(string $uri): bool | ||
{ | ||
return IriFormats::iriReference($uri); | ||
} | ||
} |
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,57 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of CycloneDX PHP Library. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) OWASP Foundation. All Rights Reserved. | ||
*/ | ||
|
||
namespace CycloneDX\Core\_helpers; | ||
|
||
/** | ||
* Namespace of functions related to URI. | ||
* | ||
* @internal as this trait may be affected by breaking changes without notice | ||
* | ||
* @author jkowalleck | ||
*/ | ||
trait UriTrait | ||
{ | ||
/** | ||
* Make a string valid to | ||
* - XML::anyURI spec. | ||
* - JSON::iri-reference spec. | ||
* | ||
* BEST EFFORT IMPLEMENTATION | ||
* | ||
* @see http://www.w3.org/TR/xmlschema-2/#anyURI | ||
* @see http://www.datypic.com/sc/xsd/t-xsd_anyURI.html | ||
* @see https://datatracker.ietf.org/doc/html/rfc2396 | ||
* @see https://datatracker.ietf.org/doc/html/rfc3987 | ||
*/ | ||
private static function fixUriBE(?string $uri): ?string | ||
{ | ||
return null === $uri | ||
? $uri | ||
: str_replace( | ||
[' ', '[', ']', '<', '>', '{', '}'], | ||
['%20', '%5B', '%5D', '%3C', '%3E', '%7B', '%7D'], | ||
$uri | ||
); | ||
} | ||
} |
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
Oops, something went wrong.