1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Ipag\Sdk\Model\Schema; |
4
|
|
|
|
5
|
|
|
use Ipag\Sdk\Model\Schema\Exception\SchemaAttributeParseException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @codeCoverageIgnore |
9
|
|
|
*/ |
10
|
|
|
class SchemaStringAttribute extends SchemaAttribute |
11
|
|
|
{ |
12
|
|
|
protected int $limit = 0; |
13
|
|
|
protected int $min = 0; |
14
|
|
|
protected int $max = 0; |
15
|
|
|
protected bool $truncate = false; |
16
|
|
|
|
17
|
|
|
public function limit(int $limit): self |
18
|
|
|
{ |
19
|
|
|
$this->limit = max($limit, 0); |
20
|
|
|
return $this; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function truncate(int $limit): self |
24
|
|
|
{ |
25
|
|
|
$this->truncate = true; |
26
|
|
|
return $this->limit($limit); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function between(int $min, int $max = 0): self |
30
|
|
|
{ |
31
|
|
|
if ($max === 0) { |
32
|
|
|
$this->min = $this->max = max($min, 0); |
33
|
|
|
} else { |
34
|
|
|
$this->min = max($min, 0); |
35
|
|
|
$this->max = max($max, 0); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function parseContextual($value) |
42
|
|
|
{ |
43
|
|
|
if (is_null($value) && $this->isNullable()) { |
44
|
|
|
return $value; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (!is_string($value)) { |
48
|
|
|
throw new SchemaAttributeParseException($this, "Provided value '$value' is not an string"); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($this->limit > 0 && strlen($value) > $this->limit) { |
52
|
|
|
if (!$this->truncate) { |
53
|
|
|
throw new SchemaAttributeParseException($this, "Provided value '$value' is exceeding the limit of {$this->limit} characters"); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$value = substr($value, 0, $this->limit); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$len = strlen($value); |
60
|
|
|
if ($this->min > 0 && $len < $this->min) { |
61
|
|
|
throw new SchemaAttributeParseException($this, "Provided value '$value' is shorter than the minimum of {$this->min} characters"); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->max > 0 && $len > $this->max) { |
65
|
|
|
throw new SchemaAttributeParseException($this, "Provided value '$value' is longer than the maximum of {$this->max} characters"); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return $value; |
69
|
|
|
} |
70
|
|
|
} |