Bridge/Symfony/Validator/Constraints/CustomerValidator.php
public function validate($customer, Constraint $constraint)
    {
        if (null === $customer) {
            return;
        }

        if (!$customer instanceof CustomerInterface) {
            throw new InvalidArgumentException('Expected instance of CustomerInterface');
        }
        if (!$constraint instanceof Customer) {
            throw new InvalidArgumentException('Expected instance of Customer (validation constraint)');
        }

        /* @var CustomerInterface $customer */
        /* @var Customer $constraint */

        // Prevent setting a parent to a customer that is already a parent (has children)
        if ($customer->hasParent()) {
            if ($customer->hasChildren() || $customer->getParent()->hasParent()) {
                $this
                    ->context
                    ->buildViolation($constraint->hierarchy_overflow)
                    ->atPath('parent')
                    ->addViolation();

                return;
            }

            // Prevent setting a parent to a customer who have non zero outstanding|credit balance
            if (0 != $customer->getOutstandingBalance() || 0 != $customer->getCreditBalance()) {
                $this
                    ->context
                    ->buildViolation($constraint->non_zero_balance)
                    ->atPath('parent')
                    ->addViolation();

                return;
            }
        }

        // A parent must have a company name.
        if ($customer->hasParent() && 0 == strlen($customer->getParent()->getCompany())) {
            $this
                ->context
                ->buildViolation($constraint->parent_company_is_mandatory)
                ->atPath('parent')
                ->addViolation();

            return;
        } elseif ($customer->hasChildren() && 0 == strlen($customer->getCompany())) {
            $this
                ->context
                ->buildViolation($constraint->company_is_mandatory)
                ->atPath('company')
                ->addViolation();

            return;
        }

        // Outstanding / Payment term
        $hasOutstanding = 0 < $customer->getOutstandingLimit();
        $hasPaymentTerm = null !== $customer->getPaymentTerm();
        if ($hasOutstanding && !$hasPaymentTerm) {
            $this
                ->context
                ->buildViolation($constraint->term_required_for_outstanding)
                ->atPath('paymentTerm')
                ->addViolation();

            return;
        } else if ($hasPaymentTerm && !$hasOutstanding) {
            $this
                ->context
                ->buildViolation($constraint->outstanding_required_for_term)
                ->atPath('outstandingLimit')
                ->addViolation();

            return;
        }
    }
public function validate($customer, Constraint $constraint)
    {
        if (null === $customer) {
            return;
        }

        if (!$customer instanceof CustomerInterface) {
            throw new InvalidArgumentException('Expected instance of CustomerInterface');
        }
        if (!$constraint instanceof Customer) {
            throw new InvalidArgumentException('Expected instance of Customer (validation constraint)');
        }

        /* @var CustomerInterface $customer */
        /* @var Customer $constraint */

        // Prevent setting a parent to a customer that is already a parent (has children)
        if ($customer->hasParent()) {
            if ($customer->hasChildren() || $customer->getParent()->hasParent()) {
                $this
                    ->context
                    ->buildViolation($constraint->hierarchy_overflow)
                    ->atPath('parent')
                    ->addViolation();

                return;
            }

            // Prevent setting a parent to a customer who have non zero outstanding|credit balance
            if (0 != $customer->getOutstandingBalance() || 0 != $customer->getCreditBalance()) {
                $this
                    ->context
                    ->buildViolation($constraint->non_zero_balance)
                    ->atPath('parent')
                    ->addViolation();

                return;
            }
        }

        // A parent must have a company name.
        if ($customer->hasParent() && 0 == strlen($customer->getParent()->getCompany())) {
            $this
                ->context
                ->buildViolation($constraint->parent_company_is_mandatory)
                ->atPath('parent')
                ->addViolation();

            return;
        } elseif ($customer->hasChildren() && 0 == strlen($customer->getCompany())) {
            $this
                ->context
                ->buildViolation($constraint->company_is_mandatory)
                ->atPath('company')
                ->addViolation();

            return;
        }

        // Outstanding / Payment term
        $hasOutstanding = 0 < $customer->getOutstandingLimit();
        $hasPaymentTerm = null !== $customer->getPaymentTerm();
        if ($hasOutstanding && !$hasPaymentTerm) {
            $this
                ->context
                ->buildViolation($constraint->term_required_for_outstanding)
                ->atPath('paymentTerm')
                ->addViolation();

            return;
        } else if ($hasPaymentTerm && !$hasOutstanding) {
            $this
                ->context
                ->buildViolation($constraint->outstanding_required_for_term)
                ->atPath('outstandingLimit')
                ->addViolation();

            return;
        }
    }
{@inheritdoc}