<commit_before><?php declare(strict_types = 1);

namespace Apitte\Core\Mapping\Request;

use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Mapping\TReflectionProperties;
use Apitte\Core\Schema\Endpoint;

abstract class BasicEntity extends AbstractEntity
{

	use TReflectionProperties;

	/**
	 * @return mixed[]
	 */
	public function getRequestProperties(): array
	{
		return $this->getProperties();
	}

	/**
	 * @return BasicEntity|null
	 */
	public function fromRequest(ApiRequest $request): ?IRequestEntity
	{
		if (in_array($request->getMethod(), [Endpoint::METHOD_POST, Endpoint::METHOD_PUT, Endpoint::METHOD_PATCH], true)) {
			return $this->fromBodyRequest($request);
		}

		if ($request->getMethod() === Endpoint::METHOD_GET) {
			return $this->fromGetRequest($request);
		}

		return null;
	}

	/**
	 * @param mixed[] $data
	 */
	public function factory(array $data): self
	{
		$inst = new static();

		// Fill properties with real data
		$properties = $inst->getRequestProperties();
		foreach ($properties as $property) {
			if (!array_key_exists($property['name'], $data)) continue;

			$value = $data[$property['name']];

			// Normalize & convert value (only not null values)
			if ($value !== null) {
				$value = $this->normalize($property['name'], $value);
			}

			// Fill single property
			$inst->{$property['name']} = $value;
		}

		return $inst;
	}

	/**
	 * @param mixed $value
	 * @return mixed
	 */
	protected function normalize(string $property, $value)
	{
		return $value;
	}

	protected function fromBodyRequest(ApiRequest $request): self
	{
		return $this->factory($request->getJsonBody(true));
	}

	protected function fromGetRequest(ApiRequest $request): self
	{
		return $this->factory($request->getQueryParams());
	}

}<commit_msg>Request could return empty body<commit_after><?php declare(strict_types = 1);

namespace Apitte\Core\Mapping\Request;

use Apitte\Core\Http\ApiRequest;
use Apitte\Core\Mapping\TReflectionProperties;
use Apitte\Core\Schema\Endpoint;

abstract class BasicEntity extends AbstractEntity
{

	use TReflectionProperties;

	/**
	 * @return mixed[]
	 */
	public function getRequestProperties(): array
	{
		return $this->getProperties();
	}

	/**
	 * @return BasicEntity|null
	 */
	public function fromRequest(ApiRequest $request): ?IRequestEntity
	{
		if (in_array($request->getMethod(), [Endpoint::METHOD_POST, Endpoint::METHOD_PUT, Endpoint::METHOD_PATCH], true)) {
			return $this->fromBodyRequest($request);
		}

		if ($request->getMethod() === Endpoint::METHOD_GET) {
			return $this->fromGetRequest($request);
		}

		return null;
	}

	/**
	 * @param mixed[] $data
	 */
	public function factory(array $data): self
	{
		$inst = new static();

		// Fill properties with real data
		$properties = $inst->getRequestProperties();
		foreach ($properties as $property) {
			if (!array_key_exists($property['name'], $data)) continue;

			$value = $data[$property['name']];

			// Normalize & convert value (only not null values)
			if ($value !== null) {
				$value = $this->normalize($property['name'], $value);
			}

			// Fill single property
			$inst->{$property['name']} = $value;
		}

		return $inst;
	}

	/**
	 * @param mixed $value
	 * @return mixed
	 */
	protected function normalize(string $property, $value)
	{
		return $value;
	}

	protected function fromBodyRequest(ApiRequest $request): self
	{
		return $this->factory((array) $request->getJsonBody(true));
	}

	protected function fromGetRequest(ApiRequest $request): self
	{
		return $this->factory($request->getQueryParams());
	}

}
{'fraction_non_alphanumeric': 0.1551043429216018, 'fraction_numerical': 0.0005640157924421884, 'mean_word_length': 4.390577507598784, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '4314714', 'n_tokens_mistral': 1200, 'n_tokens_neox': 1080, 'n_words': 249}