diff --git a/src/Extension/Form.php b/src/Extension/Form.php new file mode 100644 index 0000000..7997d05 --- /dev/null +++ b/src/Extension/Form.php @@ -0,0 +1,158 @@ +mapping = $mapping; + $this->children = $children; + } + + /** + * Binds the given data to the form. + * + * @param mixed $data The submitted data + */ + public function bind($data) + { + try { + $this->data = $this->mapping->apply($data); + } catch (ValidationException $e) { + $this->errors = $e->getErrors(); + } + } + + /** + * Fills the form. + * + * @param mixed $data The data + */ + public function fill($data) + { + $this->value = $this->mapping->unapply($data); + } + + /** + * Returns true if this form is valid, otherwise false. + * + * @return bool + */ + public function isValid() + { + return 0 === count($this->errors); + } + + /** + * Sets data. + * + * @param mixed $data Some data + */ + public function setData($data) + { + $this->data = $data; + } + + /** + * Gets the data (the applied result). + * + * return mixed + */ + public function getData() + { + return $this->data; + } + + /** + * Sets value. + * + * @param mixed $value Some value + */ + public function setValue($value) + { + $this->value = $value; + } + + /** + * Gets the value (The unapplied result). + * + * @return mixed + */ + public function getValue() + { + return $this->value; + } + + /** + * Sets errors. + * + * @param Error[] $errors An array of error objects + */ + public function setErrors(array $errors) + { + $this->errors = $errors; + } + + /** + * Gets the errors. + * + * @return Error[] + */ + public function getErrors() + { + return $this->errors; + } + + /** + * {@inheritdoc} + */ + public function offsetGet($offset) + { + if ($this->offsetExists($offset)) { + return $this->children[$offset]; + } + } + + /** + * {@inheritdoc} + */ + public function offsetExists($offset) + { + return isset($this->children[$offset]); + } + + /** + * {@inheritdoc} + */ + public function offsetSet($offset, $value) + { + throw new \RuntimeException('Not implemented'); + } + + /** + * {@inheritdoc} + */ + public function offsetUnset($offset) + { + throw new \RuntimeException('Not implemented'); + } +} diff --git a/src/Extension/Forms.php b/src/Extension/Forms.php new file mode 100644 index 0000000..f664294 --- /dev/null +++ b/src/Extension/Forms.php @@ -0,0 +1,42 @@ +form(); + }, $mapping->getChildren()); + + $form = new Form($mapping, $children); + $emitter = $mapping->getEmitter(); + + $emitter->on(Events::APPLIED, function ($data) use ($form) { + $form->setData($data->getResult()); + $form->setValue($data->getInput()); + $form->setErrors($data->getErrors()); + }); + + $emitter->on(Events::UNAPPLIED, function ($data) use ($form) { + $form->setValue($data->getResult()); + }); + + return $form; + } +} diff --git a/tests/Extension/FormTest.php b/tests/Extension/FormTest.php new file mode 100644 index 0000000..718f17b --- /dev/null +++ b/tests/Extension/FormTest.php @@ -0,0 +1,47 @@ +getMockBuilder('Mapped\Mapping') + ->disableOriginalConstructor() + ->getMock(); + + $form = new Form($mapping, [ + 'foo' => new Form($mapping), + 'bar' => new Form($mapping), + ]); + + $this->assertNotNull($form['foo']); + $this->assertNull($form['baz']); + $this->assertTrue(isset($form['foo'])); + $this->assertFalse(isset($form['baz'])); + } + + public function testOffsetSet() + { + $this->setExpectedException('RuntimeException'); + $mapping = $this->getMockBuilder('Mapped\Mapping') + ->disableOriginalConstructor() + ->getMock(); + + $form = new Form($mapping); + $form['foo'] = $form; + } + + public function testOffsetUnset() + { + $this->setExpectedException('RuntimeException'); + $mapping = $this->getMockBuilder('Mapped\Mapping') + ->disableOriginalConstructor() + ->getMock(); + + $form = new Form($mapping); + unset($form['foo']); + } +} diff --git a/tests/Extension/FormsTest.php b/tests/Extension/FormsTest.php new file mode 100644 index 0000000..f5f6cc8 --- /dev/null +++ b/tests/Extension/FormsTest.php @@ -0,0 +1,67 @@ +mapping([ + 'username' => $factory->mapping(), + 'password' => $factory->mapping(), + ], function ($username, $password) { + return new User($username, $password); + }, function (User $user) { + return [ + 'username' => $user->username, + 'password' => $user->password, + ]; + }); + + $form = $mapping->form(); + $form->bind([ + 'username' => 'dennis', + 'password' => 'passwd', + ]); + + $this->assertTrue($form->isValid()); + $this->assertInstanceOf('Mapped\Tests\Fixtures\User', $form->getData()); + $this->assertSame('dennis', $form['username']->getData()); + $this->assertSame('passwd', $form['password']->getData()); + + $form->fill($form->getData()); + + $this->assertSame([ + 'username' => 'dennis', + 'password' => 'passwd', + ], $form->getValue()); + + $this->assertSame('dennis', $form['username']->getValue()); + $this->assertSame('passwd', $form['password']->getValue()); + } + + public function testB() + { + $factory = new Factory([new \Mapped\Extension\Forms]); + $mapping = $factory->mapping([ + 'username' => $factory->mapping()->notEmpty(), + 'password' => $factory->mapping(), + ], function ($username, $password) { + return new User($username, $password); + }); + + $form = $mapping->form(); + $form->bind(['username' => '']); + + $this->assertFalse($form->isValid()); + $this->assertCount(2, $form->getErrors()); + $this->assertCount(1, $form['username']->getErrors()); + $this->assertCount(1, $form['password']->getErrors()); + $this->assertSame('error.not_empty', $form['username']->getErrors()[0]->getMessage()); + $this->assertSame('error.required', $form['password']->getErrors()[0]->getMessage()); + } +}