<?php
/*
 * Software Name: Eorisis Setup
 * Software URI: https://eorisis.com/shop/software/joomla/setup
 * Author: Eorisis
 * Author URI: https://eorisis.com
 * Copyright: Copyright (C) 2011-2025 EORISIS. All Rights Reserved.
 * License: GNU General Public License version 2; See https://eorisis.com/gpl
 * Terms of Service: https://eorisis.com/tos
 */
namespace eorisis_setup\framework\lib\core\model;
\defined('_JEXEC') or die;

class params
{
	public function __construct() {}

	// --------------------------------------------------

	public static function get(string $key, $item, $default = '')
	{
		$item = (array)$item;
		if (isset($item[$key]))
		{
			if ($item[$key] or \is_numeric($item[$key]))
			{
				return $item[$key];
			}
		}

		return $default;
	}

	// --------------------------------------------------

	public static function xml_attr(string $key, $obj, $default = '')
	{
		// Get params from xml
		// Especially good for attributes()

		if (isset($obj->{$key}))
		{
			$val = (string)$obj->{$key};
			if ($val or \is_numeric($val))
			{
				return $val;
			}
		}

		return $default;
	}

	// --------------------------------------------------

	public static function field_value(string $key, $obj, $default = '')
	{
		// Get params from xml
		// Especially good for attributes()

		$arr = (array)$obj;
		if (isset($arr['@attributes']))
		{
			$arr = $arr['@attributes'];
		}

		if (isset($arr[$key]))
		{
			$val = (string)$arr[$key];
			if ($val or \is_numeric($val))
			{
				return $val;
			}
		}

		return $default;
	}

	// --------------------------------------------------

	public static function valid(string $key, array $item, array $items, $default = '', bool $strict = false)
	{
		if (!isset($item[$key]))
		{
			return $default;
		}

		if (!$item[$key] and !\is_numeric($item[$key]))
		{
			return $default;
		}

		return \in_array($item[$key], $items, $strict) ? $item[$key] : $default;
	}
}
