<?php

namespace App\JsonApi;


/**
 * Class JsonApiFormater
 * @package App\Utils
 *
 * Format responses
 *
 */
class JsonApiFormater {

  
  public $data= array();
  public $errors = array();
  public $message;

  /**
   * JsonApiFormater constructor.
   * @param  $data
   */

  public function __construct($data=[]) {
      try{
          $this->data = $data;
      } catch (Exception $ex) {
          $this->data[] = $data;
      }
  }


  /**
   * Add a error to the array
   * @param \App\JsonApi\Error $error
   * @param null $field
   */
  public function addError(Error $error, $field = NULL) {
    $this->errors[] = $error;
  }

  public function setData($data) {
    $this->data = $data;
  }

  public function addData($data) {
    $this->data = $data;
  }

  public function setMessage($message) {
    $this->message = $message;
  }

  public function asData() {
    unset($this->errors);
    return $this;
  }

  public function asError() {
    unset($this->data);
    return $this;
  }


}