89 lines
1.8 KiB
PHP
89 lines
1.8 KiB
PHP
<?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;
|
|
});
|
|
}
|
|
} |