<commit_before><?php
namespace App\Model\Entity;

use App\AuthState;
use Cake\ORM\Entity;
use Cake\Utility\Inflector;

abstract class AppEntity
	extends Entity
{

	protected $_defaults = [ ];
	protected $_editAuth = [ ];
	protected $_showAuth = [ ];
	protected $_compact  = [ ];

	public function __construct($properties = [], $options = [])
	{
		parent::__construct($properties, $options);

		if($this->isNew()) {
			$this->set($this->_defaults, ['guard' => false]);
		}

		foreach($this->_editAuth as $p => $access) {
			if(is_bool($access)) {
				$this->accessible($p, $access);
				continue;
			}

			if(!is_array($access))
				$access = [$access];

			$this->accessible($p, false);
			foreach($access as $auth) {
				if(AuthState::hasRole($auth)) {
					$this->accessible($p, true);
					break;
				}
			}
		}

		foreach($this->_showAuth as $p => $access) {
			if(!is_array($access))
				$access = [$access];

			$show = false;
			foreach($access as $auth) {
				if(AuthState::hasRole($auth)) {
					$show = true;
					break;
				}
			}
			if(!$show)
				$this->_hidden[] = $p;
		}
	}

	public function getClass()
	{
		$class = get_class($this);
		return substr($class, strrpos($class, '\\') + 1);
	}

	public function compactProperties()
	{
		$props = [];
		foreach($this->visibleProperties() as $key) {
			if(in_array($key, $this->_compact))
				$props[] = $key;
		}
		return $props;
	}

	protected function getBaseUrl()
	{
		return strtolower(Inflector::pluralize($this->getClass()));
	}
	protected function getRelationUrl($first, $second, $fallback)
	{
		$a = $this->$first  ?: $fallback;
		$b = $this->$second ?: $fallback;
		return $a->getUrl().$b->getUrl();
	}
	public function getUrl()
	{
		return '/'.$this->getBaseUrl().'/'.$this->id;
	}
}<commit_msg>add method for reloading an entity from the db<commit_after><?php
namespace App\Model\Entity;

use App\AuthState;
use Cake\ORM\Entity;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;

abstract class AppEntity
	extends Entity
{

	protected $_defaults = [ ];
	protected $_editAuth = [ ];
	protected $_showAuth = [ ];
	protected $_compact  = [ ];

	public function __construct($properties = [], $options = [])
	{
		parent::__construct($properties, $options);

		if($this->isNew()) {
			$this->set($this->_defaults, ['guard' => false]);
		}

		foreach($this->_editAuth as $p => $access) {
			if(is_bool($access)) {
				$this->accessible($p, $access);
				continue;
			}

			if(!is_array($access))
				$access = [$access];

			$this->accessible($p, false);
			foreach($access as $auth) {
				if(AuthState::hasRole($auth)) {
					$this->accessible($p, true);
					break;
				}
			}
		}

		foreach($this->_showAuth as $p => $access) {
			if(!is_array($access))
				$access = [$access];

			$show = false;
			foreach($access as $auth) {
				if(AuthState::hasRole($auth)) {
					$show = true;
					break;
				}
			}
			if(!$show)
				$this->_hidden[] = $p;
		}
	}

	public function getClass()
	{
		$class = get_class($this);
		return substr($class, strrpos($class, '\\') + 1);
	}

	public function compactProperties()
	{
		$props = [];
		foreach($this->visibleProperties() as $key) {
			if(in_array($key, $this->_compact))
				$props[] = $key;
		}
		return $props;
	}

	protected function getBaseUrl()
	{
		return strtolower(Inflector::pluralize($this->getClass()));
	}
	protected function getRelationUrl($first, $second, $fallback)
	{
		$a = $this->$first  ?: $fallback;
		$b = $this->$second ?: $fallback;
		return $a->getUrl().$b->getUrl();
	}
	public function getUrl()
	{
		return '/'.$this->getBaseUrl().'/'.$this->id;
	}

	public function refresh()
	{
		$table = TableRegistry::get(Inflector::pluralize($this->getClass()));
		$query = $table->find('withContain');

		$keys = $table->primaryKey();
		if(is_array($keys)) {
			foreach($keys as $i => $key) {
				$field = current($query->aliasField($key));
				$query->where([$field => $this->$key]);
			}
		} else {
			$field = current($query->aliasField($keys));
			$query->where([$field => $this->$keys]);
		}
		return $query->first();
	}
}
{'fraction_non_alphanumeric': 0.2022167487684729, 'fraction_numerical': 0.0004926108374384236, 'mean_word_length': 3.630558722919042, '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': '2338293', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1436, 'n_words': 267}