Skip to content

Commit

Permalink
fix(file-storage): 修复 FileMeta 不支持 json 序列化接口
Browse files Browse the repository at this point in the history
zhiyicx/thinksns-plus-android#2377
  • Loading branch information
medz committed Sep 19, 2018
1 parent b7ed878 commit 566fffc
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 58 deletions.
67 changes: 67 additions & 0 deletions app/FileStorage/FileMetaAbstract.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

/*
* +----------------------------------------------------------------------+
* | ThinkSNS Plus |
* +----------------------------------------------------------------------+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
* +----------------------------------------------------------------------+
* | This source file is subject to version 2.0 of the Apache license, |
* | that is bundled with this package in the file LICENSE, and is |
* | available through the world-wide-web at the following url: |
* | http://www.apache.org/licenses/LICENSE-2.0.html |
* +----------------------------------------------------------------------+
* | Author: Slim Kit Group <master@zhiyicx.com> |
* | Homepage: www.thinksns.com |
* +----------------------------------------------------------------------+
*/

namespace Zhiyi\Plus\FileStorage;

abstract class FileMetaAbstract implements FileMetaInterface
{
/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array
{
$baseArr = [
'url' => $this->url(),
'vendor' => $this->getVendorName(),
'mime' => $this->getMimeType(),
'size' => $this->getSize(),
];
if ($this->hasImage()) {
$baseArr['dimension'] = [
'width' => $this->getImageDimension()->getWidth(),
'height' => $this->getImageDimension()->getHeight(),
];
}

return $baseArr;
}

/**
* Convert the object to its JSON representation.
*
* @param int $options
* @return string
*/
public function toJson($options = 0): string
{
return json_encode($this->toArray(), $options);
}

/**
* Convert the object to its JSON representation.
* @return string
*/
public function jsonSerialize(): string
{
return $this->toJson();
}
}
11 changes: 3 additions & 8 deletions app/FileStorage/FileMetaInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

namespace Zhiyi\Plus\FileStorage;

use JsonSerializable;
use Zhiyi\Plus\Models\User;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Arrayable;
use Zhiyi\Plus\FileStorage\Pay\PayInterface;

interface FileMetaInterface extends Arrayable
interface FileMetaInterface extends Arrayable, JsonSerializable, Jsonable
{
/**
* Has the file is image.
Expand Down Expand Up @@ -68,11 +70,4 @@ public function getPay(User $user): ?PayInterface;
* @return string
*/
public function url(): string;

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array;
}
27 changes: 2 additions & 25 deletions app/FileStorage/Filesystems/AliyunOss/FileMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
use Zhiyi\Plus\Models\User;
use Zhiyi\Plus\FileStorage\ImageDimension;
use Zhiyi\Plus\FileStorage\Pay\PayInterface;
use Zhiyi\Plus\FileStorage\FileMetaInterface;
use Zhiyi\Plus\FileStorage\FileMetaAbstract;
use Zhiyi\Plus\FileStorage\ResourceInterface;
use Zhiyi\Plus\FileStorage\Traits\HasImageTrait;
use Zhiyi\Plus\FileStorage\ImageDimensionInterface;

class FileMeta implements FileMetaInterface
class FileMeta extends FileMetaAbstract
{
use HasImageTrait;

Expand Down Expand Up @@ -139,29 +139,6 @@ public function url(): string
]);
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array
{
$baseArr = [
'url' => $this->url(),
'vendor' => $this->getVendorName(),
'mime' => $this->getMimeType(),
'size' => $this->getSize(),
];
if ($this->hasImage()) {
$baseArr['dimension'] = [
'width' => $this->getImageDimension()->getWidth(),
'height' => $this->getImageDimension()->getHeight(),
];
}

return $baseArr;
}

/**
* Custom using MIME types.
* @return null\Closure
Expand Down
27 changes: 2 additions & 25 deletions app/FileStorage/Filesystems/Local/FileMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
use Zhiyi\Plus\Models\User;
use Zhiyi\Plus\FileStorage\ImageDimension;
use Zhiyi\Plus\FileStorage\Pay\PayInterface;
use Zhiyi\Plus\FileStorage\FileMetaInterface;
use Zhiyi\Plus\FileStorage\FileMetaAbstract;
use Zhiyi\Plus\FileStorage\ResourceInterface;
use Zhiyi\Plus\FileStorage\Traits\HasImageTrait;
use Zhiyi\Plus\FileStorage\ImageDimensionInterface;
use Illuminate\Contracts\Filesystem\Filesystem as FilesystemContract;

class FileMeta implements FileMetaInterface
class FileMeta extends FileMetaAbstract
{
use HasImageTrait;

Expand Down Expand Up @@ -164,27 +164,4 @@ public function url(): string
'path' => base64_encode($this->resource->getPath()),
]);
}

/**
* Get the instance as an array.
*
* @return array
*/
public function toArray(): array
{
$baseArr = [
'url' => $this->url(),
'vendor' => $this->getVendorName(),
'mime' => $this->getMimeType(),
'size' => $this->getSize(),
];
if ($this->hasImage()) {
$baseArr['dimension'] = [
'width' => $this->getImageDimension()->getWidth(),
'height' => $this->getImageDimension()->getHeight(),
];
}

return $baseArr;
}
}

0 comments on commit 566fffc

Please sign in to comment.