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

namespace Nextras\Orm\Entity\Embeddable;


use Nette\SmartObject;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\IEntityAwareProperty;
use Nextras\Orm\Entity\IPropertyContainer;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\Model\MetadataStorage;
use Nextras\Orm\NullValueException;
use ReflectionClass;
use function array_filter;
use function assert;
use function count;


class EmbeddableContainer implements IPropertyContainer, IEntityAwareProperty
{
	use SmartObject;


	/** @var PropertyMetadata */
	private $metadata;

	/** @var IEntity|null */
	private $entity;

	/** @var IEmbeddable|null */
	private $value;

	/** @var PropertyMetadata[] */
	private $propertiesMetadata = [];

	/** @var string */
	private $instanceType;


	public function __construct(PropertyMetadata $propertyMetadata)
	{
		assert($propertyMetadata->args !== null);
		$this->metadata = $propertyMetadata;
		$this->instanceType = $propertyMetadata->args[EmbeddableContainer::class]['class'];
		$this->propertiesMetadata = MetadataStorage::get($this->instanceType)->getProperties();
	}


	public function setPropertyEntity(IEntity $entity): void
	{
		$this->entity = $entity;
	}


	public function convertToRawValue($value)
	{
		return $value;
	}


	public function setRawValue($value): void
	{
		assert(is_array($value) || $value === null);

		$filtered = array_filter($value ?: [], function ($val) {
			return $val !== null;
		});

		if (count($filtered) !== 0) {
			// we do not use constructor to let it optional/configurable by user
			assert(class_exists($this->instanceType));
			$reflection = new ReflectionClass($this->instanceType);
			$embeddable = $reflection->newInstanceWithoutConstructor();
			assert($embeddable instanceof IEmbeddable);
			$embeddable->setRawValue($value ?: []);
		} else {
			$embeddable = null;
		}<commit_msg>embeddables: do not force null embeddable event when we have no values<commit_after><?php declare(strict_types = 1);

namespace Nextras\Orm\Entity\Embeddable;


use Nette\SmartObject;
use Nextras\Orm\Entity\IEntity;
use Nextras\Orm\Entity\IEntityAwareProperty;
use Nextras\Orm\Entity\IPropertyContainer;
use Nextras\Orm\Entity\Reflection\PropertyMetadata;
use Nextras\Orm\InvalidArgumentException;
use Nextras\Orm\Model\MetadataStorage;
use Nextras\Orm\NullValueException;
use ReflectionClass;
use function array_filter;
use function assert;
use function count;


class EmbeddableContainer implements IPropertyContainer, IEntityAwareProperty
{
	use SmartObject;


	/** @var PropertyMetadata */
	private $metadata;

	/** @var IEntity|null */
	private $entity;

	/** @var IEmbeddable|null */
	private $value;

	/** @var PropertyMetadata[] */
	private $propertiesMetadata = [];

	/** @var string */
	private $instanceType;


	public function __construct(PropertyMetadata $propertyMetadata)
	{
		assert($propertyMetadata->args !== null);
		$this->metadata = $propertyMetadata;
		$this->instanceType = $propertyMetadata->args[EmbeddableContainer::class]['class'];
		$this->propertiesMetadata = MetadataStorage::get($this->instanceType)->getProperties();
	}


	public function setPropertyEntity(IEntity $entity): void
	{
		$this->entity = $entity;
	}


	public function convertToRawValue($value)
	{
		return $value;
	}


	public function setRawValue($value): void
	{
		assert(is_array($value) || $value === null);

		if (!$this->metadata->isNullable) {
			$hasEmbeddable = true;
		} else {
			$filtered = array_filter($value ?: [], function ($val) {
				return $val !== null;
			});
			$hasEmbeddable = count($filtered) !== 0;
		}

		if ($hasEmbeddable) {
			// we do not use constructor to let it optional/configurable by user
			assert(class_exists($this->instanceType));
			$reflection = new ReflectionClass($this->instanceType);
			$embeddable = $reflection->newInstanceWithoutConstructor();
			assert($embeddable instanceof IEmbeddable);
			$embeddable->setRawValue($value ?: []);
		} else {
			$embeddable = null;
		}
{'fraction_non_alphanumeric': 0.13402315841340232, 'fraction_numerical': 0.0009854644000985464, 'mean_word_length': 5.170212765957447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '2131447', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1227, 'n_words': 266}