linio/input
Composer 安装命令:
composer require linio/input
包简介
Abstracts HTTP request input handling, providing an easy interface for data hydration and validation
README 文档
README
Linio Input is yet another component of the Linio Framework. It aims to abstract HTTP request input handling, allowing a seamless integration with your domain model. The component is responsible for:
- Parsing request body contents
- Validating input data
- Hydrating input data into objects
Install
The recommended way to install Linio Input is through composer.
{
"require": {
"linio/input": "dev-master"
}
}
Tests
To run the test suite, you need install the dependencies via composer, then run PHPUnit.
$ composer install
$ phpunit
Usage
The library is very easy to use: first, you have to create your input handler class. The input handlers are responsible for specifying which data you're expecting to receive from requests. Let's create one:
<?php namespace Linio\Api\Handler; use Linio\Component\Input\InputHandler; class RegistrationHandler extends InputHandler { public function define() { $this->add('referrer', 'string'); $this->add('registration_date', 'datetime'); $user = $this->add('user', 'Linio\Model\User'); $user->add('name', 'string'); $user->add('email', 'string'); $user->add('age', 'integer'); } }
Now, in your controller, you just need to bind data to the handler:
<?php namespace Linio\Api\Controller; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class RegistrationController { public function registerAction(Request $request): Response { $input = new RegistrationHandler(); $input->bind($request->request->all()); if (!$input->isValid()) { return new Response($input->getErrorsAsString()); } $data = $input->getData(); $data['referrer']; // string $data['registration_date']; // \DateTime $data['user']; // Linio\Model\User return new Response(['message' => 'Valid!']); } }
Type Handler
When you are defining the fields for your input handler, there are a few types
available: string, int, bool, datetime, etc. Those are predefined types
provided by the library, but you can also create your own. This magic is
handled by Linio\Component\Input\TypeHandler. The TypeHandler allows you to
add new types, which are extensions of the BaseNode class.
<?php class GuidNode extends BaseNode { public function __construct() { $this->addConstraint(new Linio\Component\Input\Constraint\GuidValue()); } } $typeHandler = new Linio\Component\Input\TypeHandler(); $typeHandler->addType('guid', GuidNode::class); $input = new RegistrationHandler(); $input->setTypeHandler($typeHandler);
In this example, we have created a new guid type, which has a built-in constraint
to validate contents. You can use custom types to do all sorts of things: add
predefined constraint chains, transformers, instantiators and also customize how
values are generated.
Constraints
Linio Input allows you to apply constraints to your fields. This can be done
by providing a third argument for the add() method in your input handlers:
<?php use Linio\Component\Input\Constraint\Pattern; class RegistrationHandler extends InputHandler { public function define() { $this->add('referrer', 'string', ['required' => true]); $this->add('registration_date', 'datetime'); $user = $this->add('user', 'Linio\Model\User'); $user->add('name', 'string'); $user->add('email', 'string', ['constraints' => [new Pattern('/^\S+@\S+\.\S+$/')]]); $user->add('age', 'integer'); } }
The library includes several constraints by default:
- Enum
- GuidValue
- NotNull
- Pattern
- StringSize
Transformers
Linio Input allows you to create data transformers, responsible for converting simple input data, like timestamps and unique IDs, into something meaningful, like a datetime object or the full entity (by performing a query).
<?php namespace Linio\Api\Handler\Transformer; use Doctrine\Common\Persistence\ObjectRepository; use Linio\Component\Input\Transformer\TransformerInterface; class IdTransformer implements TransformerInterface { /** * @var ObjectRepository */ protected $repository; public function transform($value) { try { $entity = $this->repository->find($value); } catch (\Exception $e) { return null; } return $entity; } public function setRepository(ObjectRepository $repository) { $this->repository = $repository; } }
Data transformers can be added on a per-field basis during the definition of your input handler:
<?php use Linio\Api\Handler\Transformer\IdTransformer; class RegistrationHandler extends InputHandler { /** * @var IdTransformer */ protected $idTransformer; public function define() { $this->add('store_id', 'string', ['transformer' => $this->idTransformer]); } public function setIdTransformer(IdTransformer $idTransformer) { $this->idTransformer = $idTransformer; } }
Instantiators
Linio Input allows you to use different object instantiators on a per-field
basis. This can be done by providing a third argument for the add() method
in your input handlers:
<?php use Linio\Component\Input\Instantiator\ConstructInstantiator; use Linio\Component\Input\Instantiator\ReflectionInstantiator; class RegistrationHandler extends InputHandler { public function define() { $this->add('foobar', 'My\Foo\Class', ['instantiator' => new ConstructInstantiator()]); $this->add('barfoo', 'My\Bar\Class', ['instantiator' => new ReflectionInstantiator()]); } }
The library includes several instantiators by default:
- ConstructInstantiator
- PropertyInstantiator
- SetInstantiator
- ReflectionInstantiator
By default, the SetInstantiator is used by Object and Collection nodes.
InputHandlers
Linio Input supports portable, reusable InputHandlers via nesting. This is accomplished
by including the handler to the options parameter when adding fields.
Suppose your application deals with mailing addresses:
<?php class OrderHandler extends InputHandler { public function define() { $address = $this->add('shipping_address', Address::class); $address->add('street', 'string'); $address->add('city', 'string'); $address->add('state', 'string'); $address->add('zip_code', 'integer'); } }
Rather than duplicating this everywhere you need to handle an address, you can extract the address into its own InputHandler and re-use it throughout your application.
<?php class AddressHandler extends InputHandler { public function define() { $address->add('street', 'string'); $address->add('city', 'string'); $address->add('state', 'string'); $address->add('zip_code', 'integer'); } } class OrderHandler extends InputHander { public function define() { $this->add('shipping_address', Address::Class, ['handler' => new AddressHandler()]); } } class RegistrationHandler extends InputHander { public function define() { $this->add('home_address', Address::Class, ['handler' => new AddressHandler()]); } }
linio/input 适用场景与选型建议
linio/input 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 56.31k 次下载、GitHub Stars 达 38, 最近一次更新时间为 2015 年 03 月 16 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「form」 「input」 「linio」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 linio/input 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 linio/input 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 linio/input 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Common library for Linio projects
Simple abstraction layer on top of PDO, providing features like driver abstraction and connection pool
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Provides a static wrapper around monolog
Widget to add a remaining character counter to text inputs and textareas
Provides a multi-layered caching abstraction
统计信息
- 总下载量: 56.31k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 38
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2015-03-16