热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

php的FileData

本文由编程笔记#小编为大家整理,主要介绍了php的FileData相关的知识,希望对你有一定的参考价值。
本文由编程笔记#小编为大家整理,主要介绍了php 的FileData相关的知识,希望对你有一定的参考价值。




namespace App\Objects;
use ArrayAccess;
use JsonSerializable;
use RuntimeException;
use Illuminate\Http\UploadedFile;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
/**
* Class FileData
*
* @package App\ValueObjects
* @property-read UploadedFile $file
* @property-read string $name
* @property-read string $extension
* @property-read int $size
* @property-read string $original_name
* @property-read string $originalName
* @property-read string $mime_type
* @property-read string $mimeType
*/
class FileData implements ArrayAccess, Arrayable, Jsonable, JsonSerializable
{
/**
* @var UploadedFile
*/
protected $file;
/**
* @var string
*/
protected $name = '';
/**
* ValueObject constructor.
*
* @param UploadedFile $file
*/
public function __construct(UploadedFile $file)
{
$this->file = $file;
}
/**
* @return string
*/
public function name()
{
if (! $this->name) {
$this->name = str_random(40) . '.' . $this->extension();
}
return $this->name;
}
/**
* @return UploadedFile
*/
public function file()
{
return $this->file;
}
/**
* @return string
*/
public function extension()
{
return $this->file()->getClientOriginalExtension();
}
/**
* @return int
*/
public function size()
{
return $this->file()->getSize();
}
/**
* @return null|string
*/
public function originalName()
{
return $this->file()->getClientOriginalName();
}
/**
* @return null|string
*/
public function mimeType()
{
return $this->file()->getClientMimeType();
}
/**
* Whether a offset exists
*
* @link http://php.net/manual/en/arrayaccess.offsetexists.php
* @param mixed $offset


* An offset to check for.
*


* @return boolean true on success or false on failure.
*


*


* The return value will be casted to boolean if non-boolean was returned.
* @since 5.0.0
*/
public function offsetExists($offset)
{
return ! ! ($this->{$offset} ?? $this->{camel_case($offset)} ?? false);
}
/**
* Offset to retrieve
*
* @link http://php.net/manual/en/arrayaccess.offsetget.php
* @param mixed $offset


* The offset to retrieve.
*


* @return mixed Can return all value types.
* @since 5.0.0
*/
public function offsetGet($offset)
{
return $this->{$offset} ?? $this->{camel_case($offset)} ?? null;
}
/**
* Offset to set
*
* @link http://php.net/manual/en/arrayaccess.offsetset.php
* @param mixed $offset


* The offset to assign the value to.
*


* @param mixed $value


* The value to set.
*


* @return void
* @since 5.0.0
*/
public function offsetSet($offset, $value)
{
throw new RuntimeException('Trying to set a read-only property.');
}
/**
* Offset to unset
*
* @link http://php.net/manual/en/arrayaccess.offsetunset.php
* @param mixed $offset


* The offset to unset.
*


* @return void
* @since 5.0.0
*/
public function offsetUnset($offset)
{
throw new RuntimeException('Trying to unset a read-only property.');
}
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray()
{
return [
'file' => $this->file,
'name' => $this->name,
'extension' => $this->extension,
'size' => $this->size,
'original_name' => $this->originalName,
'mime_type' => $this->mimeType,
];
}
/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson($optiOns= 0)
{
return json_encode($this->toArray(), $options);
}
/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by json_encode,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return $this->toArray();
}
/**
* @return string
*/
public function __toString()
{
return $this->name;
}
/**
* Access to methods via magic properties
*
* @param $property
* @return mixed
*/
public function __get($property)
{
if (method_exists($this, $property)) {
return app()->call([$this, $property]);
}
}
}


if (! function_exists('file_data')) {
/**
* @param \Illuminate\Http\UploadedFile $file
* @return \App\Objects\FileData
*/
function file_data(\Illuminate\Http\UploadedFile $file)
{
return new \App\Objects\FileData($file);
}
}


// Maybe it is yours Controller action
// $file instaceof Illuminate\Http\UploadedFile
$fileData = file_data($file); // or $fileData = new FileData($file);
$fileData ->file->move(public_path('images/uploads'), $file->name);
// App\Models\File
File::create($fileData->toArray());


推荐阅读
author-avatar
周白行_170
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有