diff --git a/Entity/AddressEmbeddable.php b/Entity/AddressEmbeddable.php index 8b34a9d..10f51dc 100644 --- a/Entity/AddressEmbeddable.php +++ b/Entity/AddressEmbeddable.php @@ -11,69 +11,74 @@ class AddressEmbeddable extends Address { /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $locale; + protected string $locale = 'und'; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $givenName; + protected string $givenName = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $additionalName; + protected string $additionalName = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $familyName; + protected string $familyName = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $organization; + protected string $organization = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $addressLine1; + protected string $addressLine1 = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $addressLine2; + protected string $addressLine2 = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $postalCode; + protected string $addressLine3 = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $sortingCode; + protected string $postalCode = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $locality; + protected string $sortingCode = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $dependentLocality; + protected string $locality = ''; /** - * @ORM\Column(type="string", nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $administrativeArea; + protected string $dependentLocality = ''; /** - * @ORM\Column(type="string", length=2, nullable=true) + * @ORM\Column(type="string", nullable=false) */ - protected $countryCode; + protected string $administrativeArea = ''; + + /** + * @ORM\Column(type="string", length=2, nullable=false) + */ + protected string $countryCode = ''; public function __toString(): string { @@ -93,212 +98,73 @@ public function __toString(): string ])); } - /** - * @return string - */ - public function getLocale(): ?string - { - return $this->locale; - } - - /** - * @param string $locale - */ - public function setLocale(string $locale = null): void + public function setLocale(string $locale): void { $this->locale = $locale; } - /** - * @return string - */ - public function getAdditionalName(): ?string - { - return $this->additionalName; - } - - /** - * @param string $additionalName - */ - public function setAdditionalName(string $additionalName = null): void + public function setAdditionalName(string $additionalName): void { $this->additionalName = $additionalName; } - /** - * @return string - */ - public function getGivenName(): ?string - { - return $this->givenName; - } - - /** - * @param string $givenName - */ - public function setGivenName(string $givenName = null): void + public function setGivenName(string $givenName): void { $this->givenName = $givenName; } - /** - * @return string - */ - public function getFamilyName(): ?string - { - return $this->familyName; - } - - /** - * @param string $familyName - */ - public function setFamilyName(string $familyName = null): void + public function setFamilyName(string $familyName): void { $this->familyName = $familyName; } - /** - * @return string - */ - public function getOrganization(): ?string - { - return $this->organization; - } - - /** - * @param string $organization - */ - public function setOrganization(string $organization = null): void + public function setOrganization(string $organization): void { $this->organization = $organization; } - /** - * @return string - */ - public function getAddressLine1(): ?string - { - return $this->addressLine1; - } - - /** - * @param string $addressLine1 - */ - public function setAddressLine1($addressLine1 = null): void + public function setAddressLine1(string $addressLine1): void { $this->addressLine1 = $addressLine1; } - /** - * @return string - */ - public function getAddressLine2(): ?string - { - return $this->addressLine2; - } - - /** - * @param string $addressLine2 - */ - public function setAddressLine2(string $addressLine2 = null): void + public function setAddressLine2(string $addressLine2): void { $this->addressLine2 = $addressLine2; } - /** - * @return string - */ - public function getPostalCode(): ?string + public function setAddressLine3(string $addressLine3): void { - return $this->postalCode; + $this->addressLine3 = $addressLine3; } - /** - * @param string $postalCode - */ - public function setPostalCode(string $postalCode = null): void + public function setPostalCode(string $postalCode): void { $this->postalCode = $postalCode; } - /** - * @return string - */ - public function getSortingCode(): ?string - { - return $this->sortingCode; - } - - /** - * @param string $sortingCode - */ - public function setSortingCode(string $sortingCode = null): void + public function setSortingCode(string $sortingCode): void { $this->sortingCode = $sortingCode; } - /** - * @return string - */ - public function getLocality(): ?string - { - return $this->locality; - } - - /** - * @param string $locality - */ - public function setLocality(string $locality = null): void + public function setLocality(string $locality): void { $this->locality = $locality; } - /** - * @return string - */ - public function getDependentLocality(): ?string - { - return $this->dependentLocality; - } - - /** - * @param string $dependentLocality - */ - public function setDependentLocality(string $dependentLocality = null): void + public function setDependentLocality(string $dependentLocality): void { $this->dependentLocality = $dependentLocality; } - /** - * @return string - */ - public function getAdministrativeArea(): ?string - { - return $this->administrativeArea; - } - - /** - * @param string $administrativeArea - */ - public function setAdministrativeArea(string $administrativeArea = null): void + public function setAdministrativeArea(string $administrativeArea): void { $this->administrativeArea = $administrativeArea; } - /** - * @return string - */ - public function getCountryCode(): ?string - { - return $this->countryCode; - } - - /** - * @param string $countryCode - */ - public function setCountryCode(string $countryCode = NULL): void + public function setCountryCode(string $countryCode): void { $this->countryCode = $countryCode; } - } diff --git a/Form/EventListener/AddressEmbeddableTypeSubscriber.php b/Form/EventListener/AddressEmbeddableTypeSubscriber.php index f78e63b..3ee1f70 100644 --- a/Form/EventListener/AddressEmbeddableTypeSubscriber.php +++ b/Form/EventListener/AddressEmbeddableTypeSubscriber.php @@ -6,6 +6,7 @@ use CommerceGuys\Addressing\AddressFormat\AddressField; use CommerceGuys\Addressing\AddressFormat\AddressFormatRepository; use CommerceGuys\Addressing\AddressFormat\AddressFormatRepositoryInterface; +use CommerceGuys\Addressing\AddressFormat\FieldOverrides; use CommerceGuys\Addressing\Subdivision\SubdivisionRepository; use CommerceGuys\Addressing\Subdivision\SubdivisionRepositoryInterface; use CommerceGuys\Addressing\Country\CountryRepository; @@ -113,8 +114,18 @@ public function preSetData(FormEvent $event): void ], ]; } - foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat(), $this->getFieldOverrides($form)) as $line_index => $line_fields) { + + $fieldOverrides = $this->getFieldOverrides($form); + $requiredFields = AddressFormatHelper::getRequiredFields($addressFormat, $fieldOverrides); + + foreach (AddressFormatHelper::getGroupedFields($addressFormat->getFormat(), $fieldOverrides) as $line_index => $line_fields) { foreach ($line_fields as $field_index => $field) { + if (in_array($field, $requiredFields)) { + $element_options['required'] = true; + } elseif (isset($element_options['required'])) { + unset($element_options['required']); + } + $form->add( $field, null, @@ -164,28 +175,39 @@ public function preSubmit(FormEvent $event): void foreach ($unused_fields as $field) { $form->remove($field); } + + if ($form->getData() !== $data) { + $addressEmbeddable = new AddressEmbeddable(); + foreach ($data as $field => $value) { + $method = 'set'.ucfirst($field); + if (method_exists($addressEmbeddable, $method)) { + $addressEmbeddable->{$method}($value); + } + } + $form->setData($addressEmbeddable); + } } - private function getFieldOverrides(FormInterface $form) + private function getFieldOverrides(FormInterface $form): FieldOverrides { if (!$this->validator) { - return null; + return new FieldOverrides([]); } $formParent = $form->getParent(); if (!$formParent) { - return null; + return new FieldOverrides([]); } $parentEntity = $formParent->getData(); if (!is_object($parentEntity)) { - return null; + return new FieldOverrides([]); } try { $metadata = $this->validator->getMetadataFor(get_class($parentEntity)); } catch (NoSuchMetadataException $e) { - return null; + return new FieldOverrides([]); } $propertyMetadatas = $metadata->getPropertyMetadata($form->getName()); @@ -199,6 +221,6 @@ private function getFieldOverrides(FormInterface $form) } } - return null; + return new FieldOverrides([]); } } diff --git a/README.md b/README.md index b2173e6..d4f7871 100644 --- a/README.md +++ b/README.md @@ -69,32 +69,37 @@ You need to add an address field as an ORM Embedded property. namespace App\Entity; +use App\Repository\InstallationAddressRepository; use Daften\Bundle\AddressingBundle\Entity\AddressEmbeddable; use Daften\Bundle\AddressingBundle\Validator\Constraints as AddressingBundleAssert; use Doctrine\ORM\Mapping as ORM; -/** - * @ORM\Entity(repositoryClass="App\Repository\InstallationAddressRepository") - */ +#[ORM\Entity(repositoryClass: InstallationAddressRepository::class)] class AddressExample { - /** - * @ORM\Embedded(class="Daften\Bundle\AddressingBundle\Entity\AddressEmbeddable") - * @AddressingBundleAssert\EmbeddedAddressFormatConstraint(fields={ - * "locale", - * "addressLine1", - * "postalCode", - * "locality", - * "countryCode", - * }) - */ - private $address; + + #[ORM\Embedded(class: AddressEmbeddable::class)] + #[AddressingBundleAssert\EmbeddedAddressFormatConstraint(fields: [ + 'addressLine1' + 'postalCode' + 'locality' + 'organization' + 'givenName' + 'familyName' + 'addressLine2' + 'additionalName' + 'administrativeArea' + 'dependentLocality' + 'sortingCode' + ])] + private AddressEmbeddable $address; /** * AddressExample constructor. */ public function __construct() { + $this->address = new AddressEmbeddable(); } /** diff --git a/composer.json b/composer.json index b004316..eb9bf0e 100644 --- a/composer.json +++ b/composer.json @@ -11,14 +11,14 @@ } ], "require": { - "php": "^7.2.8", - "commerceguys/addressing": "^1", + "php": "^7.4 | ^8.0", + "commerceguys/addressing": "^2", "doctrine/orm": "^2.6", - "symfony/form": "^4.4 | ^5.3", - "symfony/framework-bundle": "^4.4 | ^5.3" + "symfony/form": "^4.4 | ^5.3 | ^6.0", + "symfony/framework-bundle": "^4.4 | ^5.3 | ^6.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^2.10", + "friendsofphp/php-cs-fixer": "^3.0", "symfony/phpunit-bridge": "^4.0" }, "config": { @@ -29,10 +29,5 @@ }, "autoload": { "psr-4": { "Daften\\Bundle\\AddressingBundle\\": "" } - }, - "extra": { - "branch-alias": { - "dev-develop": "1.0-dev" - } } }