<commit_before><?php

namespace LaraParse\Repositories;

use LaraParse\Repositories\Contracts\ParseRepository;
use Parse\ParseGeoPoint;
use Parse\ParseObject;
use Parse\ParseQuery;

abstract class AbstractParseRepository implements ParseRepository
{

    /**
     * @var \Parse\ParseQuery
     */
    protected $query;

    /**
     * @var bool
     */
    protected $useMasterKey = false;

    public function __construct()
    {
        $this->query = new ParseQuery($this->getParseClass());
    }

    /**
     * Specify Parse class name
     *
     * @return string
     */
    abstract public function getParseClass();

    /**
     * @param bool $shouldUse
     *
     * @return $this
     */
    public function useMasterKey($shouldUse = false)
    {
        $that               = clone $this;
        $that->useMasterKey = $shouldUse;

        return $that;
    }

    /**
     * @return ParseObject[]
     */
    public function all()
    {
        // TODO: Make this deal with actual pagination
        $this->query->limit(1000);

        return $this->query->find($this->useMasterKey);
    }

    /**
     * @param       $perPage
     *
     * @return ParseObject[]
     */
    public function paginate($perPage = 1)
    {
        // TODO: Implement paginate() method.
    }

    /**
     * @param array $data
     *
     * @return ParseObject
     */
    public function create(array $data)
    {
        $parseClass = new ParseObject($this->getParseClass());

        $this->setValues($data, $parseClass);
        
        return $parseClass;
    }

    /**
     * @param       $id
     * @param array $data
     *
     * @return mixed
     */
    public function update($id, array $data)
    {
        $parseClass = $this->find($id);

        $this->setValues($data, $parseClass);
        
        return $parseClass;
    }

    /**
     * @param $id
     *
     * @return mixed
     */
    public function delete($id)
    {
        $parseClass = $this->find($id);

        $parseClass->destroy($this->useMasterKey);
    }

    /**
     * @param       $id
     * @param array $columns
     *
     * @return ParseObject
     */
    public function find($id, $columns = ['*'])
    {
        return $this->query->get($id, $this->useMasterKey);
    }

    /**
     * @param       $field
     * @param       $value
     * @param array $columns
     *
     * @return ParseObject
     */
    public function findBy($field, $value, $columns = ['*'])
    {
        $this->query->equalTo($field, $value);

        return $this->query->first($this->useMasterKey);
    }

    /**
     * @param $column
     * @param $latitude
     * @param $longitude
     * @param $limit
     *
     * @return \Parse\ParseObject[]
     */
    public function near($column, $latitude, $longitude, $limit = 10)
    {
        $location = new ParseGeoPoint($latitude, $longitude);

        $this->query->near($column, $location);
        $this->query->limit($limit);

        return $this->query->find($this->useMasterKey);
    }

    /**
     * @param $column
     * @param $latitude
     * @param $longitude
     * @param $distance
     *
     * @return \Parse\ParseObject[]
     */
    public function within($column, $latitude, $longitude, $distance)
    {
        $location = new ParseGeoPoint($latitude, $longitude);

        switch (config('parse.units', 'kilometers')) {
            case 'kilometers':
                $this->query->withinKilometers($column, $location, $distance);
                break;

            case 'miles':
                $this->query->withinMiles($column, $location, $distance);
                break;

            case 'radians':
                $this->query->withinRadians($column, $location, $distance);
                break;
        }

        return $this->query->find($this->useMasterKey);
    }

    /**
     * @param $column
     * @param $swLatitude
     * @param $swLongitude
     * @param $neLatitude
     * @param $neLongitude
     *
     * @return \Parse\ParseObject[]
     */
    public function withinBox($column, $swLatitude, $swLongitude, $neLatitude, $neLongitude)
    {
        $southWest = new ParseGeoPoint((float)$swLatitude, (float)$swLongitude);
        $northEast = new ParseGeoPoint((float)$neLatitude, (float)$neLongitude);

        $this->query->withinGeoBox($column, $southWest, $northEast);

        return $this->query->find($this->useMasterKey);
    }

    /**
     * @param array       $data
     * @param ParseObject $parseObject
     *
     * @return mixed
     */
    protected function setValues(array $data, $parseObject)
    {
        foreach ($data as $key => $value) {
            // If it's an array, we need to use different setter methods
            if (is_array($value)) {
                // Associative array
                if (count(array_filter(array_keys($value), 'is_string'))) {<commit_msg>Wrapping repository results in Collections<commit_after><?php

namespace LaraParse\Repositories;

use LaraParse\Repositories\Contracts\ParseRepository;
use Parse\ParseGeoPoint;
use Parse\ParseObject;
use Parse\ParseQuery;
use Illuminate\Support\Collection;

abstract class AbstractParseRepository implements ParseRepository
{

    /**
     * @var \Parse\ParseQuery
     */
    protected $query;

    /**
     * @var bool
     */
    protected $useMasterKey = false;

    public function __construct()
    {
        $this->query = new ParseQuery($this->getParseClass());
    }

    /**
     * Specify Parse class name
     *
     * @return string
     */
    abstract public function getParseClass();

    /**
     * @param bool $shouldUse
     *
     * @return $this
     */
    public function useMasterKey($shouldUse = false)
    {
        $that               = clone $this;
        $that->useMasterKey = $shouldUse;

        return $that;
    }

    /**
     * @return Collection
     */
    public function all()
    {
        // TODO: Make this deal with actual pagination
        $this->query->limit(1000);

        return Collection::make($this->query->find($this->useMasterKey));
    }

    /**
     * @param       $perPage
     *
     * @return ParseObject[]
     */
    public function paginate($perPage = 1)
    {
        // TODO: Implement paginate() method.
    }

    /**
     * @param array $data
     *
     * @return ParseObject
     */
    public function create(array $data)
    {
        $parseClass = new ParseObject($this->getParseClass());

        $this->setValues($data, $parseClass);
        
        return $parseClass;
    }

    /**
     * @param       $id
     * @param array $data
     *
     * @return mixed
     */
    public function update($id, array $data)
    {
        $parseClass = $this->find($id);

        $this->setValues($data, $parseClass);
        
        return $parseClass;
    }

    /**
     * @param $id
     *
     * @return mixed
     */
    public function delete($id)
    {
        $parseClass = $this->find($id);

        $parseClass->destroy($this->useMasterKey);
    }

    /**
     * @param       $id
     * @param array $columns
     *
     * @return ParseObject
     */
    public function find($id, $columns = ['*'])
    {
        return $this->query->get($id, $this->useMasterKey);
    }

    /**
     * @param       $field
     * @param       $value
     * @param array $columns
     *
     * @return ParseObject
     */
    public function findBy($field, $value, $columns = ['*'])
    {
        $this->query->equalTo($field, $value);

        return $this->query->first($this->useMasterKey);
    }

    /**
     * @param $column
     * @param $latitude
     * @param $longitude
     * @param $limit
     *
     * @return Collection
     */
    public function near($column, $latitude, $longitude, $limit = 10)
    {
        $location = new ParseGeoPoint($latitude, $longitude);

        $this->query->near($column, $location);
        $this->query->limit($limit);

        return Collection::make($this->query->find($this->useMasterKey));
    }

    /**
     * @param $column
     * @param $latitude
     * @param $longitude
     * @param $distance
     *
     * @return Collection
     */
    public function within($column, $latitude, $longitude, $distance)
    {
        $location = new ParseGeoPoint($latitude, $longitude);

        switch (config('parse.units', 'kilometers')) {
            case 'kilometers':
                $this->query->withinKilometers($column, $location, $distance);
                break;

            case 'miles':
                $this->query->withinMiles($column, $location, $distance);
                break;

            case 'radians':
                $this->query->withinRadians($column, $location, $distance);
                break;
        }

        return Collection::make($this->query->find($this->useMasterKey));
    }

    /**
     * @param $column
     * @param $swLatitude
     * @param $swLongitude
     * @param $neLatitude
     * @param $neLongitude
     *
     * @return Collection
     */
    public function withinBox($column, $swLatitude, $swLongitude, $neLatitude, $neLongitude)
    {
        $southWest = new ParseGeoPoint((float)$swLatitude, (float)$swLongitude);
        $northEast = new ParseGeoPoint((float)$neLatitude, (float)$neLongitude);

        $this->query->withinGeoBox($column, $southWest, $northEast);

        return Collection::make($this->query->find($this->useMasterKey));
    }

    /**
     * @param array       $data
     * @param ParseObject $parseObject
     *
     * @return mixed
     */
    protected function setValues(array $data, $parseObject)
    {
        foreach ($data as $key => $value) {
            // If it's an array, we need to use different setter methods
            if (is_array($value)) {
                // Associative array
                if (count(array_filter(array_keys($value), 'is_string'))) {
{'fraction_non_alphanumeric': 0.1422172452407615, 'fraction_numerical': 0.0014252265092130714, 'mean_word_length': 1.9796784956020625, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 93, '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': '5926869', 'n_tokens_mistral': 2979, 'n_tokens_neox': 2867, 'n_words': 600}