-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIdCodeObject.php
More file actions
51 lines (40 loc) · 1.29 KB
/
Copy pathIdCodeObject.php
File metadata and controls
51 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
declare(strict_types=1);
namespace Echron\DataTypes;
use Echron\DataTypes\Observable\Context\PropertyChangeContext;
use Echron\Tools\Normalize\NormalizeConfig;
use Echron\Tools\Normalize\Normalizer;
class IdCodeObject extends IdObject
{
protected int|null $code_max_length = null;
private string|null $code = null;
protected NormalizeConfig|null $keyFormatConfig = null;
public function __construct(int $id, string $code)
{
parent::__construct($id);
$this->setCode($code);
}
public function __toString(): string
{
return $this->getCode() . ' (id: ' . $this->getId() . ')';
}
public function getCode(): string
{
return $this->code;
}
public function setCode(string $code): void
{
$before = $this->code;
if ($this->keyFormatConfig === null) {
$this->keyFormatConfig = new NormalizeConfig();
if ($this->code_max_length !== null) {
$this->keyFormatConfig->setMaxLength($this->code_max_length);
}
}
$code = Normalizer::normalize($code, $this->keyFormatConfig);
if ($code !== $before) {
$this->code = $code;
$this->notify(new PropertyChangeContext('code', $before, $this->getCode()));
}
}
}