- md5
- cc7b0d7216bb2d029151eaa1a7a2f469
- sha1
- 66a1db636c7457bc78577bc2597fae7599c55434
- sha256
- 28c332b8823a025693a6bcb583cf0cb662587003d84a589cac8acfb45671a4eb
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
<?php
namespace Tuf;
/**
* Provides normalization to convert objects to canonical JSON strings.
*
* @todo This is a very incomplete implementation of
* http://wiki.laptop.org/go/Canonical_JSON.
* Consider creating a separate library under php-tuf just for this?
* https://github.com/php-tuf/php-tuf/issues/14
*
* @internal
* This is intended to be used only by PHP-TUF metadata classes.
*/
trait CanonicalJsonTrait
{
/**
* Encodes an associative array into a string of canonical JSON.
*
* @param array $data
* The associative array of data.
*
* @return string
* An encoded string of normalized, canonical JSON data.
*/
protected static function encodeJson(array $data): string
{
static::sortKeys($data);
return json_encode($data, JSON_UNESCAPED_SLASHES);
}
/**
* Decodes JSON data to an associative array.
*
* @param string $data
* The JSON to decode.
*
* @return array
* The decoded data.
*/
protected static function decodeJson(string $data): array
{
return json_decode($data, true, 512, JSON_THROW_ON_ERROR);
}
/**
* Sorts an associative array into a canonical order.
*
* @param array $data
* The data to sort, passed by reference.
*
* @throws \RuntimeException
* Thrown if sorting the array fails.
*/
protected static function sortKeys(array &$data): void
{
// Apply recursively on potential subarrays
foreach ($data as $key => $value) {
if (is_array($value)) {
static::sortKeys($data[$key]);
}
}
// If $data is numerically indexed, the keys are already sorted, by
// definition, no key sorting on this level necessary
if (array_is_list($data)) {
return;
}
if (!ksort($data, SORT_STRING)) {
throw new \RuntimeException("Failure sorting keys. Canonicalization is not possible.");
}
}
}
The same content is in 136 releases, Joomla 5.1.0-beta1 to 6.3.0-alpha1. See them all, or every version of this file.