fei/filer-common

应用程序文件系统 - 公共组件

v2.0.2 2018-12-13 00:30 UTC

README

GitHub license continuousphp Build Status GitHub issues

这是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