doctrineum/scalar 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

doctrineum/scalar

最新稳定版本:4.0.0

Composer 安装命令:

composer require doctrineum/scalar

包简介

Enumeration types and value dependent sub-types for Doctrine

README 文档

README

Build Status Test Coverage Latest Stable Version License

Customizable enumeration type for Doctrine 2.4+

About custom Doctrine types, see the official documentation. For default types see the official documentation as well.

Usage

  1. Installation
  2. Custom type registration
  3. Map property as an enum
  4. Create enum
  5. Register subtype enum
  6. NULL is NULL, not Enum
  7. Understand the basics
  8. Exceptions philosophy

Installation

composer.phar require doctrineum/scalar

or manually edit composer.json at your project and "require": block (extend existing)

    "require": {
        "doctrineum/scalar": "dev-master"
    }

Custom type registration

By helper method

ScalarEnum::registerSelf(); // quick self-registration

Or manually using "magic" class::class constant

use Doctrineum\Scalar\ScalarEnumType;
// ...
Type::addType(ScalarEnumType::getTypeName(), ScalarEnumType::class);
Type::addType(BarScalarEnumType::getTypeName(), BarScalarEnumType::class);

Or manually by old fashion way

<?php
// in bootstrapping code
// ...
use Doctrine\DBAL\Types\Type;
use Doctrineum\Scalar\ScalarEnumType;
// ...
// Register type
Type::addType(ScalarEnumType::getTypeName(), '\Doctrineum\ScalarEnumType');
Type::addType(BarScalarEnumType::getTypeName(), '\Foo\BarScalarEnumType');

Or if your project uses Symfony2

# app/config/config.yml
doctrine:
    dbal:
        # ...
        types:
            scalar_enum: Doctrineum\Scalar\ScalarEnumType
            bar: Foo\BarScalarEnumType
            #...

Map property as an enum

<?php
class Foo
{
    /** @Column(type="scalar_enum") */
    protected $field;
}

Create enum

<?php
use Doctrineum\Scalar\ScalarEnum;
$enum = ScalarEnum::getEnum('foo bar');

Register subtype enum

You can register infinite number of enums, which are built according to a regexp of your choice.

<?php
use Doctrineum\Scalar\ScalarEnumType;
ScalarEnumType::addSubTypeEnum('\Foo\Bar\YourSubTypeEnum', '~get me different enum for this value~');
// ...
$enum = $ScalarEnumType->convertToPHPValue('foo');
get_class($enum) === '\Doctrineum\Scalar\ScalarEnum'; // true
get_class($enum) === '\Foo\Bar\YourSubTypeEnum'; // false
$byRegexpDeterminedEnum = $ScalarEnumType->convertToPHPValue('And now get me different enum for this value.');
get_class($byRegexpDeterminedEnum) === '\Foo\Bar\YourSubTypeEnum'; // true

NULL is NULL, Enum can not hold it

You can not create ScalarEnum with NULL value. Just use NULL directly for such column value.

Beware on using subtypes only when main enum is an abstract class. You have to resolve from-database-NULL->to-PHP-value conversion, or register subtype explicitly for NULL value (empty string respectively), otherwise fatal error by abstract class instance creation occurs.

Understand the basics

There are two roles - the factory and the value.

  • ScalarEnumType is the factory (as part of the Doctrine\DBAL\Types\Type family), building an ScalarEnum by following ScalarEnumType rules.
  • ScalarEnum is the value holder, de facto singleton, represented by a class. And class, as you know, can do a lot of things, which makes enum more sexy then whole scalar value.
  • Subtype is an ScalarEnumType, but ruled not just by type, but also by current value itself. One type can has any number of subtypes, in dependence on your imagination and used enum values.
Exceptions philosophy

Doctrineum adopts Granam exception hierarchy ideas. That means every exceptionable state is probably by a logic mistake, rather than a runtime situation.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-03-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固