fei /filer-common
应用程序文件系统 - 公共组件
v2.0.2
2018-12-13 00:30 UTC
Requires
- doctrine/common: ^2.6
- fei/entities: ^1.0.3
- league/fractal: ^0.14.0
Requires (Dev)
- dev-master
- v2.0.2
- 2.0.1
- 2.0.0
- v1.2.0
- v1.1.3
- v1.1.2
- v1.1.1
- v1.1.0
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/composer/guzzlehttp/psr7-1.9.1
- dev-dependabot/composer/guzzlehttp/guzzle-6.5.8
- dev-detached
- dev-detached2
- dev-release-V1
- dev-develop
- dev-status_enum
- dev-bck_entity
This package is auto-updated.
Last update: 2024-09-20 08:47:54 UTC
README
这是Filer Common元素包,包含以下内容
- 文件实体和转换器
- 文件实体验证器
- 上下文实体和转换器
- 上下文实体验证器
- 相关类
安装和需求
Filer客户端需要PHP 5.5或更高版本。
将此需求添加到您的composer.json
中:"fei/filer-common": "^1.0"
或在您的终端中执行composer.phar require fei/filer-common
。
用法
实体和类
文件实体
除了传统的ID和CreatedAt字段外,文件实体还有六个
重要属性
$uuid
(通用唯一标识符)是一个与文件对应的唯一ID
。其格式基于RFC4122
中定义的36个字符
,并以后端ID
开头,以:
分隔。例如:bck1:f6461366-a414-4b98-a76d-d7b190252e74
revision
是一个整数,表示文件的当前修订版本。category
是一个整数,定义文件将存储在哪个数据库中。contentType
定义了File
对象的MIME类型。data
包含文件的内容。filename
包含文件的文件名。file
是一个SplFileObject
实例。(更多详细信息请参见https://secure.php.net/manual/en/class.splfileobject.php)contexts
是一个ArrayCollection
实例,其中每个元素都是一个上下文实体
上下文实体
除了传统的ID字段外,上下文实体还有三个
重要属性
key
是一个字符串,定义上下文的键。value
是一个字符串,定义上下文的值file
是一个表示上下文相关文件的File
对象
其他工具
文件验证器
您可以使用FileValidator
类验证File
实体
<?php use Fei\Service\Filer\Validator\FileValidator; use Fei\Service\Filer\Entity\File; $fileValidator = new FileValidator(); $file = new File(); //validate returns true if your File instance is valid, or false in the other case $isFileValid = $fileValidator->validate($file); //getErrors() allows you to get an array of errors if there are some, or an empty array in the other case $errors = $fileValidator->getErrors();
默认情况下,所有File
属性都不能为空,但您也可以使用validate
方法仅验证实体的一小部分属性
<?php use Fei\Service\Filer\Validator\FileValidator; use Fei\Service\Filer\Entity\File; $fileValidator = new FileValidator(); $file = new File(); $file->setUuid('uuid'); $file->setRevision(1); $fileValidator->validateUuid($file->getUuid()); $fileValidator->validateRevision($file->getRevision()); // will return an empty array : all of our definitions are correct $errors = $fileValidator->getErrors(); echo empty($errors); // true // contentType can not be empty, let's try to set it as an empty string $file->setContentType(''); $fileValidator->validateContentType($file->getContentType()); // this time you'll get a non-empty array $errors = $fileValidator->getErrors(); echo empty($errors); // false print_r($errors); /** * print_r will return: * * Array * ( * ['contentType'] => Array * ( * 'Content-Type cannot be empty' * ) * ) **/
上下文验证器
您可以使用ContextValidator
类验证Context
实体
<?php use Fei\Service\Filer\Validator\ContextValidator; use Fei\Service\Filer\Entity\File; use Fei\Service\Filer\Entity\Context; $contextValidator = new ContextValidator(); $file = new File(); $context = new Context([ 'key' => 'my_key', 'value' => 'my_value', 'file' => $file ]); //validate returns true if your Context instance is valid, or false in the other case $isContextValid = $contextValidator->validate($context); //getErrors() allows you to get an array of errors if there are some, or an empty array in the other case $errors = $contextValidator->getErrors();
默认情况下,所有Context
属性都不能为空,但您也可以使用validate
方法仅验证实体的一小部分属性
<?php use Fei\Service\Filer\Validator\ContextValidator; use Fei\Service\Filer\Entity\Context; $contextValidator = new ContextValidator(); $context = new Context(); $context->setKey('key'); $context->setValue('value'); $contextValidator->validateKey($context->getKey()); $contextValidator->validateValue($context->getValue()); // will return an empty array : all of our definitions are correct $errors = $contextValidator->getErrors(); echo empty($errors); // true