feat : Composite file
This commit is contained in:
89
app/Service/Test/Composite/File/DirectoryComponent.php
Normal file
89
app/Service/Test/Composite/File/DirectoryComponent.php
Normal file
@@ -0,0 +1,89 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Composite\File;
|
||||
|
||||
use App\Interface\Test\Composite\FileSystemComponentInterface;
|
||||
|
||||
class DirectoryComponent implements FileSystemComponentInterface
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private array $children = [];
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @param string $name
|
||||
*/
|
||||
public function __construct(string $name)
|
||||
{
|
||||
$this->name = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize(): int
|
||||
{
|
||||
$size = 0;
|
||||
foreach ($this->children as $child) {
|
||||
$size += $child->getSize();
|
||||
}
|
||||
return $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefix
|
||||
* @return void
|
||||
*/
|
||||
public function display(string $prefix = ''): void
|
||||
{
|
||||
echo $prefix . ":directory:" . $this->name . '(' . $this->getSize() . 'bytes)' . PHP_EOL;
|
||||
|
||||
foreach ($this->children as $child) {
|
||||
$child->display($prefix ,' ');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FileSystemComponentInterface $component
|
||||
* @return void
|
||||
*/
|
||||
public function add(FileSystemComponentInterface $component): void
|
||||
{
|
||||
$this->children[] = $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param FileSystemComponentInterface $component
|
||||
* @return void
|
||||
*/
|
||||
public function remove(FileSystemComponentInterface $component): void
|
||||
{
|
||||
$this->children = array_filter($this->children, function ($c) use ($component) {
|
||||
return $c !== $component;
|
||||
});
|
||||
}
|
||||
}
|
||||
65
app/Service/Test/Composite/File/FileComponent.php
Normal file
65
app/Service/Test/Composite/File/FileComponent.php
Normal file
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Composite\File;
|
||||
|
||||
use App\Interface\Test\Composite\FileSystemComponentInterface;
|
||||
|
||||
class FileComponent implements FileSystemComponentInterface
|
||||
{
|
||||
/**
|
||||
* 文件名字
|
||||
* @var string
|
||||
*/
|
||||
private string $name;
|
||||
|
||||
/**
|
||||
* 文件大小
|
||||
* @var int
|
||||
*/
|
||||
private int $size;
|
||||
|
||||
/**
|
||||
* 构造方法
|
||||
* @param string $name
|
||||
* @param int $size
|
||||
*/
|
||||
public function __construct(string $name, int $size)
|
||||
{
|
||||
$this->name = $name;
|
||||
$this->size = $size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getSize(): int
|
||||
{
|
||||
return $this->size;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $prefix
|
||||
* @return void
|
||||
*/
|
||||
public function display(string $prefix = ''): void
|
||||
{
|
||||
echo $prefix . ":file:" . $this->name . '(' . $this->size . 'bytes)' . PHP_EOL;
|
||||
}
|
||||
}
|
||||
40
app/Service/Test/Composite/FileService.php
Normal file
40
app/Service/Test/Composite/FileService.php
Normal file
@@ -0,0 +1,40 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
* @web_site https://ctexthuang.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Test\Composite;
|
||||
|
||||
use App\Service\Test\Composite\File\DirectoryComponent;
|
||||
use App\Service\Test\Composite\File\FileComponent;
|
||||
use App\Service\Test\TestBaseService;
|
||||
|
||||
class FileService extends TestBaseService
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
$root = new DirectoryComponent('root');
|
||||
$documents = new DirectoryComponent('documents');
|
||||
$images = new DirectoryComponent('images');
|
||||
$file1 = new FileComponent('readme.md',1024);
|
||||
$file2 = new FileComponent('report.pdf',2048);
|
||||
$file3 = new FileComponent('photo.jpg',4096);
|
||||
$root->add($documents);
|
||||
$root->add($images);
|
||||
$documents->add($file1);
|
||||
$documents->add($file2);
|
||||
$images->add($file3);
|
||||
$root->display();
|
||||
|
||||
return $this->return->success();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user