| @@ 13-58 (lines=46) @@ | ||
| 10 | use Innmind\Math\Algebra\Integer; |
|
| 11 | use Innmind\Immutable\Str; |
|
| 12 | ||
| 13 | final class LongString implements Value |
|
| 14 | { |
|
| 15 | private $value; |
|
| 16 | private $original; |
|
| 17 | ||
| 18 | public function __construct(Str $string) |
|
| 19 | { |
|
| 20 | $this->original = $string; |
|
| 21 | $string = $string->toEncoding('ASCII'); |
|
| 22 | $this->value = (string) new UnsignedLongInteger( |
|
| 23 | new Integer($string->length()) |
|
| 24 | ); |
|
| 25 | $this->value .= $string; |
|
| 26 | } |
|
| 27 | ||
| 28 | public static function fromString(Str $string): Value |
|
| 29 | { |
|
| 30 | $string = $string->toEncoding('ASCII'); |
|
| 31 | $length = UnsignedLongInteger::fromString($string->substring(0, 4))->original(); |
|
| 32 | $string = $string->substring(4); |
|
| 33 | ||
| 34 | if ($string->length() !== $length->value()) { |
|
| 35 | throw new StringNotOfExpectedLength($string, $length->value()); |
|
| 36 | } |
|
| 37 | ||
| 38 | return new self($string); |
|
| 39 | } |
|
| 40 | ||
| 41 | public static function cut(Str $string): Str |
|
| 42 | { |
|
| 43 | $string = $string->toEncoding('ASCII'); |
|
| 44 | $length = UnsignedLongInteger::fromString($string->substring(0, 4))->original(); |
|
| 45 | ||
| 46 | return $string->substring(0, $length->value() + 4); |
|
| 47 | } |
|
| 48 | ||
| 49 | public function original(): Str |
|
| 50 | { |
|
| 51 | return $this->original; |
|
| 52 | } |
|
| 53 | ||
| 54 | public function __toString(): string |
|
| 55 | { |
|
| 56 | return $this->value; |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||
| @@ 13-58 (lines=46) @@ | ||
| 10 | use Innmind\Math\Algebra\Integer; |
|
| 11 | use Innmind\Immutable\Str; |
|
| 12 | ||
| 13 | final class ShortString implements Value |
|
| 14 | { |
|
| 15 | private $value; |
|
| 16 | private $original; |
|
| 17 | ||
| 18 | public function __construct(Str $string) |
|
| 19 | { |
|
| 20 | $this->original = $string; |
|
| 21 | $string = $string->toEncoding('ASCII'); |
|
| 22 | $this->value = (string) new UnsignedOctet( |
|
| 23 | new Integer($string->length()) |
|
| 24 | ); |
|
| 25 | $this->value .= $string; |
|
| 26 | } |
|
| 27 | ||
| 28 | public static function fromString(Str $string): Value |
|
| 29 | { |
|
| 30 | $string = $string->toEncoding('ASCII'); |
|
| 31 | $length = UnsignedOctet::fromString($string->substring(0, 1))->original(); |
|
| 32 | $string = $string->substring(1); |
|
| 33 | ||
| 34 | if ($string->length() !== $length->value()) { |
|
| 35 | throw new StringNotOfExpectedLength($string, $length->value()); |
|
| 36 | } |
|
| 37 | ||
| 38 | return new self($string); |
|
| 39 | } |
|
| 40 | ||
| 41 | public static function cut(Str $string): Str |
|
| 42 | { |
|
| 43 | $string = $string->toEncoding('ASCII'); |
|
| 44 | $length = UnsignedOctet::fromString($string->substring(0, 1))->original(); |
|
| 45 | ||
| 46 | return $string->substring(0, $length->value() + 1); |
|
| 47 | } |
|
| 48 | ||
| 49 | public function original(): Str |
|
| 50 | { |
|
| 51 | return $this->original; |
|
| 52 | } |
|
| 53 | ||
| 54 | public function __toString(): string |
|
| 55 | { |
|
| 56 | return $this->value; |
|
| 57 | } |
|
| 58 | } |
|
| 59 | ||