Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
thorewi committed Jan 14, 2025
1 parent 6bddfa4 commit 34854ee
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 45 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"homepage": "https://www.appsdevteam.com",
"license": ["MIT"],
"require": {
"php": ">=7.4",
"php": ">=8.0",
"doctrine/orm": "^2.4|^3.0",
"adt/nette-forms-components": "^1.0",
"symfony/property-access": "~3.3 || ^4.0 || ^5.0"
Expand Down
71 changes: 46 additions & 25 deletions src/Controls/ToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use ADT\Forms\DynamicContainer;
use ADT\Forms\StaticContainer;
use DateTimeImmutable;
use DateTimeZone;
use Doctrine\Common\Collections\ArrayCollection;
use ADT\DoctrineForms\EntityFormMapper;
use ADT\DoctrineForms\IComponentMapper;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\MappingException;
use Exception;
use Nette\ComponentModel\Component;
use Nette\Forms\Controls\DateTimeControl;
use ReflectionException;
use ReflectionProperty;

class ToMany implements IComponentMapper
{
Expand All @@ -30,10 +35,8 @@ public function __construct(EntityFormMapper $mapper)
}

/**
* @param ClassMetadata $meta
* @param Component $component
* @param $entity
* @return bool
* @throws ReflectionException
* @throws MappingException
*/
public function load(ClassMetadata $meta, Component $component, $entity): bool
{
Expand All @@ -47,19 +50,16 @@ public function load(ClassMetadata $meta, Component $component, $entity): bool
}

if ($meta->hasField($component->getName()) && $meta->getFieldMapping($component->getName())['type'] === 'json') {
$reflectionProperty = new \ReflectionProperty(get_class($entity), $component->getName());
$reflectionProperty = new ReflectionProperty(get_class($entity), $component->getName());
$reflectionProperty->setAccessible(true);
$data = $reflectionProperty->isInitialized($entity) ? $reflectionProperty->getValue($entity) : null;
foreach ($data as $row => $values) {
foreach ($values as $key => $value) {
if (isset($component[$row][$key])) {
if ($component[$row][$key] instanceof DateTimeControl) {
$component[$row][$key]->setDefaultValue(new \DateTimeImmutable($value['date'], new \DateTimeZone($value['timezone'])));

} else {
if (!$component->form->isSubmitted() || isset($component->getUntrustedValues('array')[$row])) {
foreach ($values as $key => $value) {
if (isset($component[$row][$key])) {
self::setDateTimeFromArray($value);
$component[$row][$key]->setDefaultValue($value);
}

}
}
}
Expand All @@ -74,19 +74,21 @@ public function load(ClassMetadata $meta, Component $component, $entity): bool
$UoW = $em->getUnitOfWork();

foreach ($collection as $key => $relation) {
if ($UoW->getSingleIdentifierValue($relation)) {
$this->mapper->load($relation, $component[$key]);
// mapuj jen pri neodeslanem formulari nebo pokud nebyl radek odstranen uzivatelem
if (!$component->form->isSubmitted() || isset($component->getUntrustedValues('array')[$key])) {
if ($UoW->getSingleIdentifierValue($relation)) {
$this->mapper->load($relation, $component[$key]);
// we have to fill isFilled component value
// if isFilled component is set
if ($component[$key]->getIsFilledComponent()) {
$component[$key]->getIsFilledComponent()->setDefaultValue(true);
}

// we have to fill isFilled component value
// if isFilled component is set
if ($component[$key]->getIsFilledComponent()) {
$component[$key]->getIsFilledComponent()->setDefaultValue(true);
continue;
}

continue;
$this->mapper->load($relation, $component[$key]);
}

$this->mapper->load($relation, $component[$key]);
}

return true;
Expand All @@ -97,6 +99,7 @@ public function load(ClassMetadata $meta, Component $component, $entity): bool
* @param Component $component
* @param $entity
* @return bool
* @throws MappingException
*/
public function save(ClassMetadata $meta, Component $component, $entity): bool
{
Expand All @@ -123,7 +126,7 @@ public function save(ClassMetadata $meta, Component $component, $entity): bool
/** @var StaticContainer $container */
foreach ($component->getComponents() as $container) {
// entity was added from the client
if (substr($container->getName(), 0, strlen(DynamicContainer::NEW_PREFIX)) === DynamicContainer::NEW_PREFIX) {
if (str_starts_with($container->getName(), DynamicContainer::NEW_PREFIX)) {
// we don't want to create an entity
// if adding new ones is disabled
if (! $component->isAllowAdding()) {
Expand All @@ -138,7 +141,7 @@ public function save(ClassMetadata $meta, Component $component, $entity): bool

$collection[$container->getName()] = $relation = $this->createEntity($meta, $component, $entity);
}
// container does not have a _new_ prefix and it's not in the collection
// container does not have a _new_ prefix, and it's not in the collection
elseif (!$relation = $collection->get($container->getName())) {
continue;
}
Expand All @@ -163,7 +166,7 @@ public function save(ClassMetadata $meta, Component $component, $entity): bool
* @param $field
* @return bool|ArrayCollection|mixed
*/
private function getCollection(ClassMetadata $meta, $entity, $field)
private function getCollection(ClassMetadata $meta, $entity, $field): mixed
{
if (!$meta->hasAssociation($field) || $meta->isSingleValuedAssociation($field)) {
return false;
Expand All @@ -177,4 +180,22 @@ private function getCollection(ClassMetadata $meta, $entity, $field)

return $collection;
}

private static function setDateTimeFromArray(array|string &$value): bool
{
if (!isset($value['date'], $value['timezone'], $value['timezone_type'])) {
return false;
}

if (!is_string($value['date']) || !is_string($value['timezone']) || !is_int($value['timezone_type'])) {
return false;
}

try {
$value = new DateTimeImmutable($value['date'], new DateTimeZone($value['timezone']));
return true;
} catch (Exception) {
return false;
}
}
}
31 changes: 12 additions & 19 deletions src/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace ADT\DoctrineForms;

use ADT\DoctrineForms\Exceptions\InvalidArgumentException;
use Closure;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Nette\ComponentModel\IComponent;

class Form extends \ADT\Forms\Form
Expand All @@ -15,7 +16,7 @@ class Form extends \ADT\Forms\Form
protected array $componentEntityMappers = [];
protected array $componentEntityFactories = [];

public function getEntityManager()
public function getEntityManager(): ?EntityManagerInterface
{
return $this->entityManager;
}
Expand All @@ -24,7 +25,7 @@ public function getEntityMapper(): EntityFormMapper
{
if ($this->entityMapper === NULL) {
if (!$this->getEntityManager()) {
throw new \Exception('Set entity manager first via setEntityManager() method.');
throw new Exception('Set entity manager first via setEntityManager() method.');
}

$this->entityMapper = new EntityFormMapper($this->getEntityManager(), $this);
Expand All @@ -39,12 +40,8 @@ public function setEntityManager(EntityManagerInterface $entityManager): self
return $this;
}

public function setEntity(object $entity): self
public function setEntity(Entity $entity): self
{
if (!is_object($entity)) {
throw new InvalidArgumentException('Expected object, ' . gettype($entity) . ' given.');
}

$this->entity = $entity;
return $this;
}
Expand All @@ -57,11 +54,7 @@ public function getEntity(): ?Entity
public function mapToForm(): void
{
if (!$this->entity) {
throw new \Exception('An entity is not set.');
}

if ($this->isSubmitted()) {
return;
throw new Exception('An entity is not set.');
}

$this->getEntityMapper()->load($this->entity, $this);
Expand All @@ -72,34 +65,34 @@ public function mapToEntity(): void
$this->getEntityMapper()->save($this->entity, $this);
}

public function getComponentFormMapper(IComponent $component): ?\Closure
public function getComponentFormMapper(IComponent $component): ?Closure
{
return $this->componentFormMappers[spl_object_hash($component)] ?? null;
}

public function setComponentFormMapper(IComponent $component, \Closure $formMapper): self
public function setComponentFormMapper(IComponent $component, Closure $formMapper): self
{
$this->componentFormMappers[spl_object_hash($component)] = $formMapper;
return $this;
}

public function getComponentEntityMapper(IComponent $component): ?\Closure
public function getComponentEntityMapper(IComponent $component): ?Closure
{
return $this->componentEntityMappers[spl_object_hash($component)] ?? null;
}

public function setComponentEntityMapper(IComponent $component, \Closure $entityMapper): self
public function setComponentEntityMapper(IComponent $component, Closure $entityMapper): self
{
$this->componentEntityMappers[spl_object_hash($component)] = $entityMapper;
return $this;
}

public function getComponentEntityFactory(IComponent $component): ?\Closure
public function getComponentEntityFactory(IComponent $component): ?Closure
{
return $this->componentEntityFactories[spl_object_hash($component)] ?? null;
}

public function setComponentEntityFactory(IComponent $component, \Closure $entityFactory): self
public function setComponentEntityFactory(IComponent $component, Closure $entityFactory): self
{
$this->componentEntityFactories[spl_object_hash($component)] = $entityFactory;
return $this;
Expand Down

0 comments on commit 34854ee

Please sign in to comment.