<commit_before><?php

namespace Tokenly\TCA;
use Exception;
use Config;
use Tokenly\XChainClient\Client;

class Access
{
	static $balances = array();
	
	function __construct()
	{
		$config = Config('tokenly');
		if(empty($config['xchain']['api_token'])){
			throw new Exception('XChain API Token and API Secret required');
		}
		$this->api_url = $config['xchain']['url'];
		$this->api_token = $config['xchain']['api_token'];
		$this->api_secret = $config['xchain']['api_secret'];
		$this->client = new Client($this->api_url, $this->api_token, $this->api_secret);
	}
	
	/**
	 * Checks if one or more addresses meet the defined Token-Access conditions. Returns true or false
	 * 
	 * @param $address string|array - can be a single BTC address or a string of addresses
	 * @param $conditions array
	 * @example $conditions = array(array('asset' => 'ASSET', 'amount' => 5000, 'op' => '=', 'stackOp' => 'AND'));
	 * 
	 * @return bool
	 */
	public function checkAccess($address, $conditions = array(), $balances = false)
	{
		if($balances !== false){
			$getBalances = $this->getAddressBalances($address);
		}
		else{
			$getBalances = $balances;
		}
		$stack = array();
		foreach($conditions as $lock){
			$hasReq = $this->parseLock($getBalances, $lock);
			$stack[] = array('hasReq' => $hasReq, 'stackOp' => $lock['stackOp']);
		}
		$doCheck = $this->parseStack($stack);
		return $doCheck;
	}
	
	public function getAddressBalances($address, $grouped = true)<commit_msg>made loading xchain client on construct optional (and using .env variables)<commit_after><?php

namespace Tokenly\TCA;
use Exception;
use Config;
use Tokenly\XChainClient\Client;

class Access
{
	static $balances = array();
	protected $client = false;
	
	function __construct($load_client = false)
	{
		if($load_client){
			$this->client = new Client(env('XCHAIN_CONNECTION_URL'), env('XCHAIN_API_TOKEN'), env('XCHAIN_API_KEY'));
		}
	}
	
	/**
	 * Checks if one or more addresses meet the defined Token-Access conditions. Returns true or false
	 * 
	 * @param $address string|array - can be a single BTC address or a string of addresses
	 * @param $conditions array
	 * @example $conditions = array(array('asset' => 'ASSET', 'amount' => 5000, 'op' => '=', 'stackOp' => 'AND'));
	 * 
	 * @return bool
	 */
	public function checkAccess($address, $conditions = array(), $balances = false)
	{
		if($balances !== false){
			$getBalances = $this->getAddressBalances($address);
		}
		else{
			$getBalances = $balances;
		}
		$stack = array();
		foreach($conditions as $lock){
			$hasReq = $this->parseLock($getBalances, $lock);
			$stack[] = array('hasReq' => $hasReq, 'stackOp' => $lock['stackOp']);
		}
		$doCheck = $this->parseStack($stack);
		return $doCheck;
	}
	
	public function getAddressBalances($address, $grouped = true)
{'fraction_non_alphanumeric': 0.16816063105055576, 'fraction_numerical': 0.0028684116170670493, 'mean_word_length': 4.557768924302789, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2943741', 'n_tokens_mistral': 983, 'n_tokens_neox': 894, 'n_words': 232}