Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extend booking information #63

Merged
merged 1 commit into from
Nov 9, 2020
Merged

Extend booking information #63

merged 1 commit into from
Nov 9, 2020

Conversation

DanielSiepmann
Copy link
Contributor

Add further (optional) fields.
Add first and last name for each reservation.
Allow external code to hook into validation, to add custom validation.

Add further (optional) fields.
Add first and last name for each reservation.
Allow external code to hook into validation, to add custom validation.
@DanielSiepmann
Copy link
Contributor Author

An example how custom validation can look like:

Classes/Reserve/Validation/Order.php

<?php

namespace WerkraumMedia\Sitepackage\Reserve\Validation;

/*
 * Copyright (C) 2020 Daniel Siepmann <coding@daniel-siepmann.de>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

use JWeiland\Reserve\Domain\Model\Order as OrderEntity;
use JWeiland\Reserve\Domain\Validation\OrderValidator;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\CMS\Extbase\SignalSlot\Dispatcher;
use TYPO3\CMS\Extbase\Validation\ValidatorResolver;
use TYPO3\CMS\Extbase\Validation\Validator\EmailAddressValidator;
use TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator;
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator;
use TYPO3\CMS\Extbase\Validation\Validator\NumberRangeValidator;

class Order
{
    /**
     * @var ValidatorResolver
     */
    private $validatorResolver;

    /**
     * @var GenericObjectValidator
     */
    private $genericObjectValidator;

    public function __construct(
        ValidatorResolver $validatorResolver,
        GenericObjectValidator $genericObjectValidator
    ) {
        $this->validatorResolver = $validatorResolver;
        $this->genericObjectValidator = $genericObjectValidator;
    }

    public function validate(
        OrderEntity $order,
        ObjectStorage $errorResults
    ) {
        $this->buildValidationForOrder();
        $this->buildValidationForParticipants($order);

        $errorResults->attach($this->genericObjectValidator->validate($order));
    }

    private function buildValidationForOrder()
    {
        $rules = [
            'firstName' => [
                NotEmptyValidator::class => [],
            ],
            'lastName' => [
                NotEmptyValidator::class => [],
            ],
            'email' => [
                NotEmptyValidator::class => [],
                EmailAddressValidator::class => [],
            ],
            'address' => [
                NotEmptyValidator::class => [],
            ],
            'zip' => [
                NotEmptyValidator::class => [],
            ],
            'city' => [
                NotEmptyValidator::class => [],
            ],
        ];

        $this->attachValidatorsForRules($rules);
    }

    private function buildValidationForParticipants(OrderEntity $order): void
    {
        $rules = [];

        foreach (array_keys($order->getParticipants()->toArray()) as $position) {
            $rules['participants[' . $position . '].firstName'] = [
                NotEmptyValidator::class => [],
            ];
            $rules['participants[' . $position . '].lastName'] = [
                NotEmptyValidator::class => [],
            ];
        }

        $this->attachValidatorsForRules($rules);
    }

    private function attachValidatorsForRules(array $rules)
    {
        foreach ($rules as $propertyName => $validators) {
            foreach ($validators as $validatorClassName => $validatorOptions) {
                $this->genericObjectValidator->addPropertyValidator(
                    $propertyName,
                    $this->validatorResolver->createValidator($validatorClassName, $validatorOptions)
                );
            }
        }
    }

    public static function register(): void
    {
        /** @var Dispatcher $dispatcher */
        $dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
        $dispatcher->connect(OrderValidator::class, 'validateOrder', static::class, 'validate');
    }
}

ext_localconf.php:

    \WerkraumMedia\Sitepackage\Reserve\Validation\Order::register();

@pascal20997
Copy link
Contributor

Love it, looks very nice. Thank you

@pascal20997 pascal20997 merged commit 1d021ce into jweiland-net:master Nov 9, 2020
sfroemkenjw pushed a commit that referenced this pull request Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants