<commit_before><?php

/**
 * This file is part of the JSend package.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, <NAME>
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
 * @version 1.0.0
 */

namespace MWebbers\JSend;

/**
 * The JSend class.
 *
 * @since 1.0.0
 * @see http://labs.omniti.com/labs/jsend
 */
class JSend
{
	const SUCCESS = 'success';
	const FAIL = 'fail';
	const ERROR = 'error';

	/** @var string The status. */
	private $status;

	/** @var array The data. */
	private $data;

	/** @var string|null The message. */
	private $message;

	/** @var int|null The code. */
	private $code;

	/**
	 * Construct a JSend object with the given status, data, message and code.
	 *
	 * @param string $status
	 * @param array|null $data = null
	 * @param string|null $message = null
	 * @param int|null $code = null
	 * @throws \UnexpectedValueException
	 */
	public function __construct($status, array $data = null, $message = null, $code = null)
	{
		$this->setStatus($status);
		$this->setData($data);
		$this->setMessage($message);
		$this->setCode($code);
	}

	/**
	 * Returns the string representation of the object.
	 *
	 * @return string the string representation of the object.
	 */
	public function __toString()
	{
		return $this->encode();
	}

	/**
	 * Returns the array representation of the object.
	 *
	 * @return array the array representation of the object.
	 */
	public function toArray()
	{
		$result = ['status' => $this->status];

		switch ($this->status) {
			case self::ERROR:
				$result['message'] = $this->message;

				if ($this->code !== null) {
					$result['code'] = $this->code;
				}

				if (!empty($this->data)) {
					$result['data'] = $this->data;
				}

				break;

			default:
				$result['data'] = $this->data;

				break;
		}

		return $result;
	}

	/**
	 * Returns a JSend succes object with the given data.
	 *
	 * @param array $data
	 * @return JSend a JSend succes object with the given data.
	 */
	public static function success(array $data)
	{
		return new self(self::SUCCESS, $data);
	}

	/**
	 * Returns a JSend fail object with the given data.
	 *
	 * @param array $data
	 * @return JSend a JSend fail object with the given data.
	 */
	public static function fail(array $data)
	{
		return new self(self::FAIL, $data);
	}

	/**
	 * Returns a JSend error object with the given message, code and data.
	 *
	 * @param string $message
	 * @param int|null $code = null
	 * @param array $data = []
	 * @return JSend a JSend error object with the given message, code and data.
	 */
	public static function error($message, $code = null, array $data = [])
	{
		return new self(self::ERROR, $data, $message, $code);
	}

	/**
	 * Returns the decoded JSend object.
	 *
	 * @param string $input
	 * @return JSend the decoded JSend object.
	 * @throws \UnexpectedValueException
	 */
	public static function decode($input)
	{
		$json = json_decode($input, true);

		if ($json === null) {
			throw new \UnexpectedValueException('JSend JSON can not be decoded.');
		}

		return self::decodeJson($json);
	}

	/**
	 * Returns the decoded JSend input.<commit_msg>Fixed error method data<commit_after><?php

/**
 * This file is part of the JSend package.
 *
 * @author <NAME> <<EMAIL>.io>
 * @copyright Copyright (c) 2015, <NAME>
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
 * @version 1.0.0
 */

namespace MWebbers\JSend;

/**
 * The JSend class.
 *
 * @since 1.0.0
 * @see http://labs.omniti.com/labs/jsend
 */
class JSend
{
	const SUCCESS = 'success';
	const FAIL = 'fail';
	const ERROR = 'error';

	/** @var string The status. */
	private $status;

	/** @var array The data. */
	private $data;

	/** @var string|null The message. */
	private $message;

	/** @var int|null The code. */
	private $code;

	/**
	 * Construct a JSend object with the given status, data, message and code.
	 *
	 * @param string $status
	 * @param array|null $data = null
	 * @param string|null $message = null
	 * @param int|null $code = null
	 * @throws \UnexpectedValueException
	 */
	public function __construct($status, array $data = null, $message = null, $code = null)
	{
		$this->setStatus($status);
		$this->setData($data);
		$this->setMessage($message);
		$this->setCode($code);
	}

	/**
	 * Returns the string representation of the object.
	 *
	 * @return string the string representation of the object.
	 */
	public function __toString()
	{
		return $this->encode();
	}

	/**
	 * Returns the array representation of the object.
	 *
	 * @return array the array representation of the object.
	 */
	public function toArray()
	{
		$result = ['status' => $this->status];

		switch ($this->status) {
			case self::ERROR:
				$result['message'] = $this->message;

				if ($this->code !== null) {
					$result['code'] = $this->code;
				}

				if (!empty($this->data)) {
					$result['data'] = $this->data;
				}

				break;

			default:
				$result['data'] = $this->data;

				break;
		}

		return $result;
	}

	/**
	 * Returns a JSend succes object with the given data.
	 *
	 * @param array $data
	 * @return JSend a JSend succes object with the given data.
	 */
	public static function success(array $data)
	{
		return new self(self::SUCCESS, $data);
	}

	/**
	 * Returns a JSend fail object with the given data.
	 *
	 * @param array $data
	 * @return JSend a JSend fail object with the given data.
	 */
	public static function fail(array $data)
	{
		return new self(self::FAIL, $data);
	}

	/**
	 * Returns a JSend error object with the given message, code and data.
	 *
	 * @param string $message
	 * @param int|null $code = null
	 * @param array $data = null
	 * @return JSend a JSend error object with the given message, code and data.
	 */
	public static function error($message, $code = null, array $data = null)
	{
		return new self(self::ERROR, $data, $message, $code);
	}

	/**
	 * Returns the decoded JSend object.
	 *
	 * @param string $input
	 * @return JSend the decoded JSend object.
	 * @throws \UnexpectedValueException
	 */
	public static function decode($input)
	{
		$json = json_decode($input, true);

		if ($json === null) {
			throw new \UnexpectedValueException('JSend JSON can not be decoded.');
		}

		return self::decodeJson($json);
	}

	/**
	 * Returns the decoded JSend input.
{'fraction_non_alphanumeric': 0.14894293435065967, 'fraction_numerical': 0.004132888253059927, 'mean_word_length': 3.109732201175702, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 39, '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': '1621983', 'n_tokens_mistral': 2235, 'n_tokens_neox': 1986, 'n_words': 656}