pion/laravel-logic-factory

用于简化逻辑类创建的抽象LogicFactory

v1.1 2016-01-28 18:08 UTC

This package is auto-updated.

Last update: 2024-09-23 16:57:12 UTC


README

一个支持从LogicFactory快速创建逻辑类的类。当你需要从一个表示逻辑类类的字符串中创建一个类时。

  • 完全单元测试。
  • 易于使用。

安装

composer require pion/laravel-logic-factory

内容

使用

你必须继承LogicFactory并实现自己的静态函数。完整示例可在tests/Mock/TypeFactory.php中找到

createLogicList (静态)

返回一个可用的逻辑类集合。按类名(不带命名空间)索引,标题作为值。

示例

/**
 * Create an own colleciton of types
 * @return \Illuminate\Support\Collection
 */
static function createLogicList()
{
    return new \Illuminate\Support\Collection([
        "TestType" => "Testing type"
    ]);
}

logicNamespace (静态)

返回工厂子类的当前命名空间,以加载正确的类。

示例

/**
 * A namespace where the logic classes are stored. Like
 * __NAMESPACE__."\\Types
 * @return string
 */
static function logicNamespace()
{
    return __NAMESPACE__."\\Types";
}

lists (静态)

理想地创建一个可选的类型。理想地用于数据库中的模型,使用getLogic在模型中

示例

{!! Form::select("test", TestFactory::lists()->toArray()) !!}

valide (静态)

用于快速检查传递的逻辑类字符串是否有效(不带命名空间)。使用isValide函数。

title (静态)

返回给定逻辑类字符串的标题(使用getTitle函数)

Eloquent模型特性

理想地用于在模型中快速使用工厂。支持类的缓存和函数,以便快速访问工厂和最终类。

/**
 * Returns the logic factory.
 *
 * @param string|null $attribute    the used attribute from the model to use as a class. When null, the default
 * property will be used. Enables multiple logic usage in model.
 *
 * @return LogicFactory
 */
public function getLogicInstance($attribute = null);

/**
 * Returns the logic class from the logic factory
 *
 * @param string|null $attribute    the used attribute from the model to use as a class. When null, the default
 * property will be used. Enables multiple logic usage in model.
 *
 * @return mixed
 */
public function getLogicFactory($attribute = null);

ModelLogicTrait

用于具有更多逻辑接口属性模型的模型。通过传递属性,你定义要使用的正确值。你可以设置(重写__construct方法)logicAttributeName属性以使用自己的属性名称。

你需要实现createLogicFactory函数并返回自己的工厂。更高级的示例在tests/Mock/Models/ModelLogic.php中

示例

use ModelLogicTrait;

public function createLogicFactory($classValue, $attributeName)
{
    return new LogicFactory($classValue);
}

ModelSingleLogicTrait

用于模型具有单个逻辑类的单属性。你只需要实现getLogicFactoryClass方法。

示例

use ModelSingleLogicTrait;

public function getLogicFactoryClass()
{
    return TypeFactory::class;
}

示例

基本示例

$logic = new TypeFactory("VarcharType");

// returns the class you need
$type = $logic->getLogic();

$type->customMethodYouProvide();

验证

if (!TypeFactory::valide("VarcharType")) {
	throw new Exception();
}

标题

return TypeFactory::title("VarcharType");

Eloquent模型示例

/**
 * @var OptionType|null
 */
protected $optionType = null;

protected $fillable = [
	'type'
];

/**
 * @return string
 */
public function getType() {
    return $this->type;
}

/**
 * Returns a type helper
 * @return OptionType|null
 */
public function getTypeHelper()
{
	if (is_null($this->optionType)) {
		$this->optionType = new OptionType($this->getType(), $this);
	}

	return $this->optionType;
}

待办事项

  • 重构为接口
  • 添加自动基于文件的逻辑类加载(从给定的logicNamespace定义的文件夹)以及某种形式的缓存。