Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Separate form and input filters #20

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
5 changes: 3 additions & 2 deletions config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@

'form_elements' => array(
'factories' => array(
'BaconUser\Form\RegistrationForm' => 'BaconUser\Form\Factory\RegistrationFormFactory'
'BaconUser\Form\UserFieldset' => 'BaconUser\Form\Factory\UserFieldsetFactory',
'BaconUser\Form\User\RegistrationForm' => 'BaconUser\Form\Factory\RegistrationFormFactory'
)
),

'hydrators' => array(
'factories' => array(
'BaconUser\Hydrator\RegistrationHydrator' => 'BaconUser\Hydrator\Factory\RegistrationHydratorFactory'
'BaconUser\Hydrator\UserHydrator' => 'BaconUser\Hydrator\Factory\UserHydrator'
)
),

Expand Down
6 changes: 2 additions & 4 deletions src/BaconUser/Form/Factory/RegistrationFormFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace BaconUser\Form\Factory;

use BaconUser\Form\RegistrationForm;
use BaconUser\Form\User\RegistrationForm;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

Expand All @@ -28,10 +28,8 @@ class RegistrationFormFactory implements FactoryInterface
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
$options = $parentLocator->get('BaconUser\Options\UserOptions');

$form = new RegistrationForm($options);
$form->setHydrator($parentLocator->get('HydratorManager')->get('BaconUser\Hydrator\RegistrationHydrator'));
$form = new RegistrationForm();
$form->setInputFilter($parentLocator->get('InputFilterManager')->get('BaconUser\InputFilter\RegistrationFilter'));

return $form;
Expand Down
38 changes: 38 additions & 0 deletions src/BaconUser/Form/Factory/UserFieldsetFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* BaconUser
*
* @link http://github.com/Bacon/BaconUser For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace BaconUser\Form\Factory;

use BaconUser\Form\UserFieldset;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Service factory that instantiates {@see UserFieldset}.
*/
class UserFieldsetFactory implements FactoryInterface
{
/**
* createService(): defined by FactoryInterface.
*
* @see FactoryInterface::createService()
* @param ServiceLocatorInterface $serviceLocator
* @return UserFieldset
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();
$options = $parentLocator->get('BaconUser\Options\UserOptions');
$hydrator = $parentLocator->get('HydratorManager')->get('BaconUser\Hydrator\UserHydrator');

$fieldset = new UserFieldset($hydrator, $options);

return $fieldset;
}
}
59 changes: 59 additions & 0 deletions src/BaconUser/Form/User/RegistrationForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php
/**
* BaconUser
*
* @link http://github.com/Bacon/BaconUser For the canonical source repository
* @copyright 2013 Ben Scholzen 'DASPRiD'
* @license http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
*/

namespace BaconUser\Form\User;

use BaconUser\Options\UserOptionsInterface;
use Zend\Form\Form;

/**
* Generic registration form.
*/
class RegistrationForm extends Form
{
public function init()
{
$this->setName('registration-form');

$this->add(array(
'type' => 'BaconUser\Form\UserFieldset',
'name' => 'user',
'options' => array(
'use_as_base_fieldset' => true
)
));

$this->add(array(
'type' => 'Csrf',
'name' => 'csrf'
));

$this->add(array(
'type' => 'Submit',
'name' => 'submit',
'options' => array(
'label' => 'Register',
)
));

// Add specific registration elements

$userFieldset = $this->get('user');
$userFieldset->add(array(
'type' => 'Password',
'name' => 'passwordVerification',
'options' => array(
'label' => 'Verify password'
),
'attributes' => array(
'required' => 'required'
)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,24 @@
namespace BaconUser\Form;

use BaconUser\Options\UserOptionsInterface;
use Zend\Form\Form;
use Zend\Form\Fieldset;
use Zend\Stdlib\Hydrator\HydratorInterface;

/**
* Generic registration form.
* Base fieldset for user
*/
class RegistrationForm extends Form
class UserFieldset extends Fieldset
{
public function __construct(UserOptionsInterface $options)
/**
* Constructor
*
* @param HydratorInterface $hydrator
* @param UserOptionsInterface $options
*/
public function __construct(HydratorInterface $hydrator, UserOptionsInterface $options)
{
parent::__construct(null);
parent::__construct('user');
$this->setHydrator($hydrator);

if ($options->getEnableUsername()) {
$this->add(array(
Expand All @@ -28,51 +36,31 @@ public function __construct(UserOptionsInterface $options)
'label' => 'Username',
),
'attributes' => array(
'type' => 'text',
'required' => 'required',
),
));
}

$this->add(array(
'type' => 'Email',
'name' => 'email',
'options' => array(
'label' => 'Email',
),
'attributes' => array(
'type' => 'text'
),
'required' => 'required'
)
));

$this->add(array(
'type' => 'Password',
'name' => 'password',
'options' => array(
'label' => 'Password',
),
'attributes' => array(
'type' => 'password'
),
));

$this->add(array(
'name' => 'password_verification',
'options' => array(
'label' => 'Verify password',
),
'attributes' => array(
'type' => 'password'
),
));

$this->add(array(
'name' => 'submit',
'options' => array(
'label' => 'Register',
),
'attributes' => array(
'type' => 'submit',
),
), array(
'priority' => -100
'required' => 'required'
)
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,27 @@
namespace BaconUser\Hydrator\Factory;

use BaconUser\Form\PasswordHashingStrategy;
use BaconUser\Hydrator\RegistrationHydrator;
use BaconUser\Hydrator\UserHydrator;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Service factory that instantiates {@see RegistrationHydrator}.
* Service factory that instantiates {@see UserHydrator}.
*/
class RegistrationHydratorFactory implements FactoryInterface
class UserHydratorFactory implements FactoryInterface
{
/**
* createService(): defined by FactoryInterface.
*
* @see FactoryInterface::createService()
* @param ServiceLocatorInterface $serviceLocator
* @return RegistrationHydrator
* @return UserHydrator
*/
public function createService(ServiceLocatorInterface $serviceLocator)
{
$parentLocator = $serviceLocator->getServiceLocator();

$hydrator = new RegistrationHydrator(
$hydrator = new UserHydrator(
new PasswordHashingStrategy(
$parentLocator->get('BaconUser\Password\HandlerInterface')
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,33 @@

namespace BaconUser\Hydrator;

use Zend\Stdlib\Exception;
use Zend\Stdlib\Hydrator\ClassMethods;
use Zend\Stdlib\Hydrator\Strategy\StrategyInterface;

/**
* Hydrator for the {@see RegistrationForm}.
*/
class RegistrationHydrator extends ClassMethods
class UserHydrator extends ClassMethods
{
/**
* @param StrategyInterface $passwordHashingStrategy
*/
public function __construct(StrategyInterface $passwordHashingStrategy)
{
parent::__construct(true);
$this->addStrategy('password_hash', $passwordHashingStrategy);
parent::__construct(false);
$this->addStrategy('passwordHash', $passwordHashingStrategy);
}

/**
* hydrate(): defined by ClassMethods.
*
* @see ClassMethods::hydrate()
* @param array $data
* @param array $data
* @param object $object
* @return object
*/
public function hydrate(array $data, $object)
{
if (isset($data['password'])) {
$data['password_hash'] = $data['password'];
$data['passwordHash'] = $data['password'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this one happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly for hydrators, as most hydrators often do "set" . $key.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, I don't like all of those naming confusions. Sometimes underscore_separated, sometimes camelCase... But the problem is complex and if we want something coherent we really should establish a standard for common components, so that each component by default can assume something about how data is named :/.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We go with underscore_separated in all arrays so far, shouldn't be different here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may be a problem for most hydrators that inflect the setter/getter based on the key. For instance DoctrineModule hydrator's assume that setter is "set" . ucfirst($key). So if we keep this underscore_separated people should define setFirst_Name method. I just went with the situation that will fit most situations without having to override each hydrator.

}

return parent::hydrate($data, $object);
Expand Down
22 changes: 10 additions & 12 deletions src/BaconUser/InputFilter/Factory/RegistrationFilterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,16 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$userRepository = $parentLocator->get('BaconUser\Repository\UserRepository');
$options = $parentLocator->get('BaconUser\Options\UserOptions');

$inputFilter = new RegistrationFilter(
new NoObjectExists(array(
'object_repository' => $userRepository,
'fields' => 'email',
)),
new NoObjectExists(array(
'object_repository' => $userRepository,
'fields' => 'username',
)),
$options
);
$usernameValidator = new NoObjectExists(array(
'object_repository' => $userRepository,
'fields' => 'username'
));

return $inputFilter;
$emailValidator = new NoObjectExists(array(
'object_repository' => $userRepository,
'fields' => 'email'
));

return new RegistrationFilter($userRepository, $usernameValidator, $emailValidator, $options);
}
}
Loading