|
| 1 | +<?php declare(strict_types=1); |
| 2 | +/** |
| 3 | + * This file is part of toolkit/stdlib. |
| 4 | + * |
| 5 | + * @author https://github.com/inhere |
| 6 | + * @link https://github.com/php-toolkit/stdlib |
| 7 | + * @license MIT |
| 8 | + */ |
| 9 | + |
| 10 | +namespace Toolkit\Stdlib\Arr; |
| 11 | + |
| 12 | +/** |
| 13 | + * Class ArrBuffer |
| 14 | + * |
| 15 | + * @package Toolkit\Stdlib\Arr |
| 16 | + */ |
| 17 | +final class ArrBuffer |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var string[] |
| 21 | + */ |
| 22 | + private $body = []; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var string |
| 26 | + */ |
| 27 | + private $delimiter = ''; // '/' ':' |
| 28 | + |
| 29 | + /** |
| 30 | + * @param string $first |
| 31 | + * |
| 32 | + * @return ArrBuffer |
| 33 | + */ |
| 34 | + public static function new(string $first = ''): ArrBuffer |
| 35 | + { |
| 36 | + return new self($first); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * constructor. |
| 41 | + * |
| 42 | + * @param string $content |
| 43 | + */ |
| 44 | + public function __construct(string $content = '') |
| 45 | + { |
| 46 | + if ($content) { |
| 47 | + $this->body[] = $content; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * @param string $content |
| 53 | + */ |
| 54 | + public function write(string $content): void |
| 55 | + { |
| 56 | + $this->body[] = $content; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @param string $content |
| 61 | + */ |
| 62 | + public function append(string $content): void |
| 63 | + { |
| 64 | + $this->write($content); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * @param string $content |
| 69 | + */ |
| 70 | + public function prepend(string $content): void |
| 71 | + { |
| 72 | + array_unshift($this->body, $content); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * clear |
| 77 | + */ |
| 78 | + public function clear(): void |
| 79 | + { |
| 80 | + $this->body = []; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return string[] |
| 85 | + */ |
| 86 | + public function getBody(): array |
| 87 | + { |
| 88 | + return $this->body; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @param string[] $body |
| 93 | + */ |
| 94 | + public function setBody(array $body): void |
| 95 | + { |
| 96 | + $this->body = $body; |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @return string |
| 101 | + */ |
| 102 | + public function toString(): string |
| 103 | + { |
| 104 | + return implode($this->delimiter, $this->body); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @return string |
| 109 | + */ |
| 110 | + public function __toString() |
| 111 | + { |
| 112 | + return $this->toString(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @return string |
| 117 | + */ |
| 118 | + public function getDelimiter(): string |
| 119 | + { |
| 120 | + return $this->delimiter; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * @param string $delimiter |
| 125 | + */ |
| 126 | + public function setDelimiter(string $delimiter): void |
| 127 | + { |
| 128 | + $this->delimiter = $delimiter; |
| 129 | + } |
| 130 | +} |
0 commit comments