1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/value-object project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Daikon\ValueObject; |
10
|
|
|
|
11
|
|
|
use Daikon\Interop\Assertion; |
12
|
|
|
use Daikon\Interop\MakeEmptyInterface; |
13
|
|
|
|
14
|
|
|
final class Text implements MakeEmptyInterface, ValueObjectInterface |
15
|
|
|
{ |
16
|
|
|
private string $value; |
17
|
|
|
|
18
|
|
|
private string $encoding; |
19
|
|
|
|
20
|
|
|
/** @param null|string $value */ |
21
|
20 |
|
public static function fromNative($value): self |
22
|
|
|
{ |
23
|
20 |
|
Assertion::nullOrString($value, 'Trying to create Text VO from unsupported value type.'); |
24
|
20 |
|
return is_null($value) ? new self : new self($value); |
25
|
|
|
} |
26
|
|
|
|
27
|
1 |
|
public static function makeEmpty(): self |
28
|
|
|
{ |
29
|
1 |
|
return new self; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** @param self $comparator */ |
33
|
1 |
|
public function equals($comparator): bool |
34
|
|
|
{ |
35
|
1 |
|
Assertion::isInstanceOf($comparator, self::class); |
36
|
1 |
|
return $this->toNative() === $comparator->toNative(); |
37
|
|
|
} |
38
|
|
|
|
39
|
12 |
|
public function toNative(): string |
40
|
|
|
{ |
41
|
12 |
|
return $this->value; |
42
|
|
|
} |
43
|
|
|
|
44
|
7 |
|
public function __toString(): string |
45
|
|
|
{ |
46
|
7 |
|
return $this->value; |
47
|
|
|
} |
48
|
|
|
|
49
|
8 |
|
public function isEmpty(): bool |
50
|
|
|
{ |
51
|
8 |
|
return $this->value === ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
public function getLength(): int |
55
|
|
|
{ |
56
|
2 |
|
return mb_strlen($this->value, $this->encoding) ?: 0; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// use mb_detect_order() to set runtime encoding detection options |
60
|
20 |
|
private function __construct(string $value = '', string $encoding = null) |
61
|
|
|
{ |
62
|
20 |
|
$this->value = $value; |
63
|
20 |
|
$this->encoding = $encoding ?? mb_detect_encoding($value); |
64
|
20 |
|
} |
65
|
|
|
} |
66
|
|
|
|