<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Shop\Form;

use Laminas\Form\Form;
use Shop\Validator\BalancePayPasswordValidator;

class ChangeUserPayPasswordForm extends Form
{
    private $userInfo;

    public function __construct($userInfo, $name = 'change-user-pay-password-form', $options = [])
    {
        parent::__construct($name, $options);

        $this->setAttribute('method', 'post');

        $this->userInfo = $userInfo;

        $this->addElements();
        $this->addInputFilter();
    }

    protected function addElements()
    {
        if ($this->userInfo->getPayPassword()) {
            $this->add([
                'type'  => 'password',
                'name'  => 'originalPassword',
                'attributes'    => [
                    'id'    => 'originalPassword'
                ]
            ]);
        }

        $this->add([
            'type'  => 'password',
            'name'  => 'payPassword',
            'attributes' => [
                'id'     => 'payPassword'
            ]
        ]);

        $this->add([
            'type'  => 'password',
            'name'  => 'payComPassword',
            'attributes' => [
                'id'   => 'payComPassword'
            ]
        ]);
    }

    protected function addInputFilter()
    {
        $inputFilter = $this->getInputFilter();

        if ($this->userInfo->getPayPassword()) {
            $inputFilter->add([
                'name'      => 'originalPassword',
                'required'  => true,
                'filters'   => [
                    ['name' => 'StringTrim']
                ],
                'validators'=> [
                    [
                        'name'      => 'StringLength',
                        'options'   => [
                            'min'   => 6
                        ]
                    ],
                    [
                        'name'      => BalancePayPasswordValidator::class,
                        'options'   => [
                            'userInfo' => $this->userInfo
                        ]
                    ]
                ]
            ]);
        }


        $inputFilter->add([
            'name'      => 'payPassword',
            'required'  => true,
            'filters'   => [
                ['name' => 'StringTrim']
            ],
            'validators'=> [
                [
                    'name'      => 'StringLength',
                    'options'   => [
                        'min'   => 6
                    ]
                ]
            ]
        ]);

        $inputFilter->add([
            'name'      => 'payComPassword',
            'required'  => true,
            'filters'   => [
                ['name' => 'StringTrim']
            ],
            'validators'=> [
                [
                    'name'      => 'Identical',
                    'options'   => [
                        'token' => 'payPassword'
                    ]
                ]
            ]
        ]);
    }
}