<?php
/* ============================================================
 *  SpwAjaxResponse 1.0.0 — обработчик ответов для SpwAjaxSender
 *
 *  (c) 2026 Спектор Дмитрий (SPECTOR & CO) · MIT
 *  spector.pw · web@spector.pw · @S7Dmitry
 *
 *  Документация: https://spector.pw/
 *  ============================================================ */
class SpwAjaxResponse {
	private $action = array(); // Все добавленные действия добавляем сюды

	public function __construct() {
		header('Content-Type: application/json');
	}
	
	public function __call($name, $arguments)  // Замечание: значение $name регистрозависимо. echo "Вызов метода '$name' " . implode(', ', $arguments). "\n";
	{
		$this->action[] = array('type'=>$name, 'data'=>$arguments); //'data'=>$arguments
    }

	// Показать все что поместили в массив отправки
	public function action_list()
	{
		return print_r($this->action, true);
	}

	// Отправляем ответ
	public function send($last_call = false, $last_param = '')
	{
		if($last_call) {$this->__call($last_call, $last_param);}
		exit(json_encode($this->action)); //echo json_encode($this->action);
	}
}

/*
	Варианты ответов из php: html, alert, redirect, attr, text, console, append, reset, reload
	//
	$this->load->library('SpwAjaxResponse'); 
	$this->SpwAjaxResponse->console('hello'); 
	//	
	public function console($text) {$this->action[] = array('type'=>'console', 'data'=>$text);	}
	public function alert($text){$this->action[] = array('type'=>'alert', 'data'=>$text);}
	/////////////
	    $this->SpwAjaxResponse->console('hello');
        $this->SpwAjaxResponse->attr('placeholder','Обновляем тут все');
        $this->SpwAjaxResponse->alert('Неправильная форма');
        $this->SpwAjaxResponse->send(); 
*/