2022-03-09 23:46:45 +00:00
|
|
|
<?php
|
2022-09-20 11:06:03 +00:00
|
|
|
/**
|
|
|
|
* @package Joomla.Component.Builder
|
|
|
|
*
|
|
|
|
* @created 3rd September, 2020
|
|
|
|
* @author Llewellyn van der Merwe <https://dev.vdm.io>
|
|
|
|
* @git Joomla Component Builder <https://git.vdm.dev/joomla/Component-Builder>
|
|
|
|
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
|
|
|
|
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
|
|
|
*/
|
2022-03-09 23:46:45 +00:00
|
|
|
|
|
|
|
namespace VDM\Joomla\Utilities;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2022-03-12 04:00:16 +00:00
|
|
|
* The json checker
|
|
|
|
*
|
|
|
|
* @since 3.0.9
|
2022-03-09 23:46:45 +00:00
|
|
|
*/
|
|
|
|
abstract class JsonHelper
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Check if you have a json string
|
|
|
|
*
|
|
|
|
* @input string $string The json string to check
|
|
|
|
*
|
|
|
|
* @returns bool true on success
|
2022-03-12 04:00:16 +00:00
|
|
|
*
|
|
|
|
* @since 3.0.9
|
2022-03-09 23:46:45 +00:00
|
|
|
*/
|
|
|
|
public static function check($string): bool
|
|
|
|
{
|
|
|
|
if (StringHelper::check($string))
|
|
|
|
{
|
2023-01-01 02:11:34 +00:00
|
|
|
json_decode((string) $string);
|
2022-03-09 23:46:45 +00:00
|
|
|
return (json_last_error() === JSON_ERROR_NONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-03-12 04:00:16 +00:00
|
|
|
/**
|
|
|
|
* Convert a json object to a string
|
|
|
|
*
|
|
|
|
* @input string $value The json string to convert
|
|
|
|
*
|
|
|
|
* @returns a string
|
|
|
|
*
|
|
|
|
* @since 3.0.9
|
|
|
|
*/
|
2022-03-09 23:46:45 +00:00
|
|
|
public static function string($value, $separator = ", ", $table = null, $id = 'id', $name = 'name')
|
|
|
|
{
|
|
|
|
// do some table foot work
|
|
|
|
$external = false;
|
2023-01-01 02:11:34 +00:00
|
|
|
if (is_string($table) && strpos((string) $table, '#__') !== false)
|
2022-03-09 23:46:45 +00:00
|
|
|
{
|
|
|
|
$external = true;
|
2023-01-01 02:11:34 +00:00
|
|
|
$table = str_replace('#__', '', (string) $table);
|
2022-03-09 23:46:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// check if string is JSON
|
2023-01-01 02:11:34 +00:00
|
|
|
$result = json_decode((string) $value, true);
|
2022-03-09 23:46:45 +00:00
|
|
|
if (json_last_error() === JSON_ERROR_NONE)
|
|
|
|
{
|
|
|
|
// is JSON
|
|
|
|
if (ArrayHelper::check($result))
|
|
|
|
{
|
|
|
|
if (StringHelper::check($table))
|
|
|
|
{
|
|
|
|
$names = array();
|
|
|
|
foreach ($result as $val)
|
|
|
|
{
|
|
|
|
if ($external)
|
|
|
|
{
|
|
|
|
if ($_name = GetHelper::var(null, $val, $id, $name, '=', $table))
|
|
|
|
{
|
|
|
|
$names[] = $_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ($_name = GetHelper::var($table, $val, $id, $name))
|
|
|
|
{
|
|
|
|
$names[] = $_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ArrayHelper::check($names))
|
|
|
|
{
|
|
|
|
return (string) implode($separator, $names);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (string) implode($separator, $result);
|
|
|
|
}
|
2023-01-01 02:11:34 +00:00
|
|
|
return (string) json_decode((string) $value);
|
2022-03-09 23:46:45 +00:00
|
|
|
}
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|