Response.php 892 Bytes
<?php
/**
 * Created by PhpStorm.
 * User: Zip
 * Date: 1/27/15
 * Time: 10:30 PM
 */

namespace Hood\Cache\Ssdb;


class Response
{
    public $cmd;
    public $code;
    public $data = null;
    public $message;

    function __construct($code = 'ok', $data_or_message = null)
    {
        $this->code = $code;
        if ($code == 'ok') {
            $this->data = $data_or_message;
        } else {
            $this->message = $data_or_message;
        }
    }

    function __toString()
    {
        if ($this->code == 'ok') {
            $s = $this->data === null ? '' : json_encode($this->data);
        } else {
            $s = $this->message;
        }
        return sprintf('%-13s %12s %s', $this->cmd, $this->code, $s);
    }

    function ok()
    {
        return $this->code == 'ok';
    }

    function not_found()
    {
        return $this->code == 'not_found';
    }
}