承接 noahlvb/valueobject-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

noahlvb/valueobject-bundle

Composer 安装命令:

composer require noahlvb/valueobject-bundle

包简介

Symfony integration for my value object package

README 文档

README

Symfony intergation for my value object package

CI Latest Stable Version License

Installation

With composer, add:

$ composer require noahlvb/valueobject-bundle

Run Tests

To make sure everything works you can run tests:

$ make test

Doctrine DBAL

In order to store you own value object just make a new doctrine type extended by ValueObjectType. Also add this newly created type to your doctrine config.

class EmailAddressType extends ValueObjectType
{
    public function getClassName(): string
    {
        return EmailAddress::class;
    }

    public function getName(): string
    {
        return 'email_address';
    }
}
doctrine:
    dbal:
        types:
          email_address: SoulSurvivor\Integration\Doctrine\Persistence\Type\ValueEmailAddressType

Symfony Forms

If you want to use your value object in a Symfony Form you should make a new form type extending ValueObjectForm. And then use that formType in your own form.

class EmailAddressForm extends ValueObjectForm
{
    protected function getClassName(): string
    {
        return EmailAddress::class;
    }
}

By default ValueObjectForm will be a TextType. If you wish to override this and make it a EmailType for example, override the getParent() method with your choose.

class EmailAddressForm extends ValueObjectForm
{
    protected function getClassName(): string
    {
        return EmailAddress::class;
    }

    public function getParent()
    {
        return EmailType::class;
    }
}

Symfony Validator

By default the ValeuObjectForm will use the ValueObjectConstraint(Validator) when validating the form. This will use the build in isValid() method of the value objects. If you wish to write your own validation message or extend the validator you can do so in like this.

class EmailAddressConstraint extends ValueObjectConstraint
{
    public $message = 'This email address is not valid.';
}
class EmailAddressConstraintValidator extends ValueObjectConstraintValidator
{
}

You have the create a empty class and extend the ValueObjectConstraintValidator. This is because Symfony Validator matches the constraint and its validator based on the class names.

Lastly in your form type override the getConstraintClassName() with the class name of your newly created constraint. Like this:

class EmailAddressForm extends ValueObjectForm
{
    protected function getConstraintClassName(): string
    {
        return EmailAddressConstraint::class;
    }
}

统计信息

  • 总下载量: 2.04k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-03-22

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固