<commit_before><?php

namespace PrateekKathal\SimpleCurl;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class ResponseTransformer
{

  /**
   * Response Variable
   *
   * @var string
   */
  protected $response = '';

  /**
   * Default Data Key In Response For making Collections/Models etc
   *
   * @var string
   */
  protected $dataKey = '';

  /**
   * Set the response for this class from the CURL Request
   *
   * @param string $response
   */
  public function setResponse($response, $dataKey = '')
  {
      $this->response = $response;
      $this->dataKey = (!empty($dataKey)) ? $dataKey : '';
      return $this;
  }

  /**
   * Transform to JSON
   *
   * @return JSON
   */
  public function toJson()
  {
      if (is_string($this->response)) {
          $response = json_decode($this->response);
          return ($response && !empty($this->dataKey)) ? (!empty($response->{$this->dataKey})) ? $response->{$this->dataKey} : null : $response;
      }
      return null;
  }

  /**
   * Transfrom to Array
   *
   * @return array
   */
  public function toArray()
  {
      if (is_string($this->response)) {
          $response = json_decode($this->response, true);
          return ($response && !empty($this->dataKey)) ? (!empty($response[$this->dataKey])) ? $response[$this->dataKey] : null : $response;
      }
      return null;
  }

  /**
   * Transfrom to Collection
   *
   * @return Collection
   */
  public function toCollection($model = null)
  {
      $response = $this->toJson();

      if ($model && $response) {
          return $this->responseArrayToModelCollection($model, $response);
      }

      return ($response) ? ($this->checkIfAllAreArray($response)) ? collect($response) : collect([$response]) : collect([]);
  }

  /**
   * Transfrom to Collection
   *
   * @return Collection
   */
  public function toPaginated($perPage)
  {
      $response = $this->toJson();

      if (!isset($response->total, $response->per_page, $response->current_page, $response->data)) {
          throw new \Exception('Missing Required Fields for Pagination');
      }

      return new LengthAwarePaginator(
      collect($response->data), $response->total, $response->per_page,
      Paginator::resolveCurrentPage(), ['path' => Paginator::resolveCurrentPath()]
    );
  }

  /**
   * Check If All Elements in Response Are Array
   *
   * @param  array $response
   *
   * @return boolean
   */
  private function checkIfAllAreArray(array $response)
  {
      foreach ($response as $key => $value) {
          if (!is_object($value) || is_string($value)) {
              return false;
              break;
          }
      }
      return true;
  }

  /**
   * Transform to a Specific Model
   *
   * @param  string $modelName
   * @param  array $relations
   *
   * @return Model
   */
  public function toModel($modelName, $relations = [])<commit_msg>Small fix when defaultDataKey is not empty<commit_after><?php

namespace PrateekKathal\SimpleCurl;

use Illuminate\Pagination\Paginator;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\ModelNotFoundException;

class ResponseTransformer
{

  /**
   * Response Variable
   *
   * @var string
   */
  protected $response = '';

  /**
   * Default Data Key In Response For making Collections/Models etc
   *
   * @var string
   */
  protected $dataKey = '';

  /**
   * Set the response for this class from the CURL Request
   *
   * @param string $response
   */
  public function setResponse($response, $dataKey = '')
  {
      $this->response = $response;
      $this->dataKey = (!empty($dataKey)) ? $dataKey : '';
      return $this;
  }

  /**
   * Transform to JSON
   *
   * @return JSON
   */
  public function toJson()
  {
      if (is_string($this->response)) {
          $response = json_decode($this->response);
          return ($response && !empty($this->dataKey)) ? (!empty($response->{$this->dataKey})) ? $response->{$this->dataKey} : null : $response;
      }
      return null;
  }

  /**
   * Transfrom to Array
   *
   * @return array
   */
  public function toArray()
  {
      if (is_string($this->response)) {
          $response = json_decode($this->response, true);
          return ($response && !empty($this->dataKey)) ? (!empty($response[$this->dataKey])) ? $response[$this->dataKey] : null : $response;
      }
      return null;
  }

  /**
   * Transfrom to Collection
   *
   * @return Collection
   */
  public function toCollection($model = null)
  {
      $response = $this->toJson();

      if ($model && $response) {
          return $this->responseArrayToModelCollection($model, $response);
      }

      return ($response) ? ($this->checkIfAllAreArray($response)) ? collect($response) : collect([$response]) : collect([]);
  }

  /**
   * Transfrom to Collection
   *
   * @return Collection
   */
  public function toPaginated($perPage)
  {
      $response = $this->toJson();

      if (!isset($response->total, $response->per_page, $response->current_page, $response->data)) {
          throw new \Exception('Missing Required Fields for Pagination');
      }

      return new LengthAwarePaginator(
      collect($response->data), $response->total, $response->per_page,
      Paginator::resolveCurrentPage(), ['path' => Paginator::resolveCurrentPath()]
    );
  }

  /**
   * Check If All Elements in Response Are Array
   *
   * @param  array $response
   *
   * @return boolean
   */
  private function checkIfAllAreArray(array $response)
  {
      if(!empty($this->dataKey) && is_array($response)) return true;

      foreach ($response as $key => $value) {
          if (!is_object($value) || is_string($value)) {
              return false;
              break;
          }
      }

      return true;
  }

  /**
   * Transform to a Specific Model
   *
   * @param  string $modelName
   * @param  array $relations
   *
   * @return Model
   */
  public function toModel($modelName, $relations = [])
{'fraction_non_alphanumeric': 0.14885874958650347, 'fraction_numerical': 0.0, 'mean_word_length': 2.6939523518631643, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 58, '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': '6510927', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1805, 'n_words': 406}