<commit_before><?php
/**
 * Model, REST Resource entity
 */
namespace Resto\Entity;

use Resto\Common\Str;
use Resto\Common\Helpers as H;
use Resto\Common\Module as Resource;
use Resto\Common\Request;
use Resto\Common\Query;

use Resto\Relations\Relation;
use Resto\Relations\HasMany;
use Resto\Relations\HasOne;
use Resto\Relations\BelongsTo;

class Model
{
	/**
	 * Collection Path
	 */
	protected $collection_path;

	/**
	 * Entity path, action path for a single entity
	 */
	protected $entity_path;


	/**
	 * Unique identifier key
	 * @var string
	 */
	protected static $key = 'id';
	
	/**
	 * Valid attributes for the entity.
	 * If specified, will be filtered before doing the API request
	 * @var [type]
	 */
	protected static $fillable = array();
		
	protected $attributes      = array();

	protected $original        = array();

	public static function all()
	{
		return static::query()->get();
	}

	public static function find($id)
	{
		$model = new static;
		$model->id = $id;

		$query = static::query();
		$query->setPath($model->getEntityPath());

		return $query->first();
	}

	/**
	 * Get value of PK
	 * @return mixed
	 */
	public function getKey()
	{
		return H::arrayGet($this->attributes, static::$key);
	}

	/**
	 * Get the resource instance for this model
	 * @return Resto\Common\Resource
	 */
	public static function getResource()
	{
		return Resource::resolve(get_called_class());
	}

	/**
	 * Get HTTP request object to add more params.
	 * @return Query
	 */
	public static function query()
	{
		$model = new static;
		$query = static::getResource()->getQuery();
		$query->setModelTemplate(new static);
		$query->setPath($model->getCollectionPath());

		return $query;
	}

	/**
	 * static::query's default path is set to current model's collection
	 * getModelQuery will set the path using current entity
	 * used for entity related tasks, ie:- UPDATE/delete(oid)
	 * @return Query
	 */
	public function getModelQuery()
	{
		$query = static::query();
		$query->setPath($this->getEntityPath());
		
		return $query;
	}

	/**
	 * Save or update a model
	 * POST/PUT
	 * @return bool
	 */
	public function save()
	{
		//existing update
		if ($this->getKey()) {

			$query = $this->getModelQuery();
			$query->setMethod(Request::METHOD_PUT);
			$query->addParams($this->attributes);
			return $query->execute()->parse();

		} else {

			$query = static::query();
			$query->setMethod(Request::METHOD_POST);
			$query->addParams($this->attributes);
			return $query->execute()->parse();

		}
	}
<commit_msg>imrefactor<commit_after><?php
/**
 * Model, REST Resource entity
 */
namespace Resto\Entity;

use Resto\Common\Str;
use Resto\Common\Helpers as H;
use Resto\Common\Module as Resource;
use Resto\Common\Request;
use Resto\Common\Query;

use Resto\Relations\Relation;
use Resto\Relations\HasMany;
use Resto\Relations\HasOne;
use Resto\Relations\BelongsTo;

class Model
{
	/**
	 * Collection Path
	 */
	protected $collection_path;

	/**
	 * Entity path, action path for a single entity
	 */
	protected $entity_path;


	/**
	 * Unique identifier key
	 * @var string
	 */
	protected static $key = 'id';
	
	/**
	 * Valid attributes for the entity.
	 * If specified, will be filtered before doing the API request
	 * @var [type]
	 */
	protected static $fillable = array();
		
	protected $attributes      = array();

	protected $original        = array();

	public static function all()
	{
		return static::query()->get();
	}

	public static function find($id)
	{
		$model = new static;
		$model->id = $id;

		$query = static::query();
		$query->setPath($model->getEntityPath());

		return $query->first();
	}

	/**
	 * Get value of PK
	 * @return mixed
	 */
	public function getKey()
	{
		return H::arrayGet($this->attributes, static::$key);
	}

	/**
	 * Get the resource instance for this model
	 * @return Resto\Common\Resource
	 */
	public static function getResource()
	{
		return Resource::resolve(get_called_class());
	}

	/**
	 * Get HTTP request object to add more params.
	 * @return Query
	 */
	public static function query()
	{
		$model = new static;
		$query = static::getResource()->getQuery();
		$query->setModelTemplate(new static);
		$query->setPath($model->getCollectionPath());

		return $query;
	}

	/**
	 * static::query's default path is set to current model's collection
	 * getModelQuery will set the path using current entity
	 * used for entity related tasks, ie:- UPDATE/delete(oid)
	 * @return Query
	 */
	public function getModelQuery()
	{
		$query = static::query();
		$query->setPath($this->getEntityPath());
		
		return $query;
	}

	/**
	 * Save or update a model
	 * POST/PUT
	 * @return bool
	 */
	public function save()
	{
		//existing update
		if ($this->getKey()) {
			$query = $this->getModelQuery();
			$query->setMethod(Request::METHOD_PUT);
		} else {
			$query = static::query();
			$query->setMethod(Request::METHOD_POST);
		}

		$query->addParams($this->attributes);
		return $query->execute()->parse();
	}

{'fraction_non_alphanumeric': 0.14986924160128748, 'fraction_numerical': 0.0, 'mean_word_length': 3.5158946412352408, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, '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': '2103302', 'n_tokens_mistral': 1710, 'n_tokens_neox': 1524, 'n_words': 425}