Skip to content

Commit 66e4abe

Browse files
committed
extract constructor logic to individual methods
1 parent 250a5e5 commit 66e4abe

File tree

2 files changed

+59
-20
lines changed

2 files changed

+59
-20
lines changed

src/Collection/Complex/FullName.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ class FullName extends ValueObject
2525
*/
2626
public function __construct(protected ?string $name)
2727
{
28-
$this->name = format(FullNameFormatter::class, $this->name);
29-
30-
$this->split = str($this->name)->split('/\s/');
28+
$this->name = $this->format();
29+
$this->split = $this->split();
3130
}
3231

3332
/**
@@ -70,6 +69,26 @@ public function value(): string
7069
return $this->fullName();
7170
}
7271

72+
/**
73+
* Format the value.
74+
*
75+
* @return string
76+
*/
77+
protected function format(): string
78+
{
79+
return format(FullNameFormatter::class, $this->value());
80+
}
81+
82+
/**
83+
* Split the value.
84+
*
85+
* @return Collection
86+
*/
87+
protected function split(): Collection
88+
{
89+
return str($this->value())->split('/\s/');
90+
}
91+
7392
/**
7493
* Get an array representation of the value object.
7594
*

src/Collection/Complex/TaxNumber.php

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,21 @@ public function __construct(
2222
protected ?string $number = null,
2323
protected ?string $country = null,
2424
) {
25-
$this->number = format(TaxNumberFormatter::class, $this->number, $this->country);
25+
$this->number = $this->format();
2626

27-
$this->when($this->isWithCountry(), function () {
28-
$this->country = str($this->number)
29-
->substr(0, 2)
30-
->upper()
31-
->value();
27+
if ($this->isWithCountry()) {
28+
$this->split();
29+
}
30+
}
3231

33-
$this->number = str($this->number)
34-
->substr(2)
35-
->value();
36-
});
32+
/**
33+
* Check if the tax number length is less or equal two.
34+
*
35+
* @return bool
36+
*/
37+
public function isWithCountry(): bool
38+
{
39+
return strlen($this->number) >= 2 && ! is_numeric($this->number);
3740
}
3841

3942
/**
@@ -71,23 +74,40 @@ public function country(): string
7174
}
7275

7376
/**
74-
* Check if the tax number length is less or equal two.
77+
* Get the object value.
7578
*
76-
* @return bool
79+
* @return string
7780
*/
78-
public function isWithCountry(): bool
81+
public function value(): string
7982
{
80-
return strlen($this->number) >= 2 && ! is_numeric($this->number);
83+
return $this->fullTaxNumber();
8184
}
8285

8386
/**
84-
* Get the object value.
87+
* Format the value.
8588
*
8689
* @return string
8790
*/
88-
public function value(): string
91+
protected function format(): string
8992
{
90-
return $this->fullTaxNumber();
93+
return format(TaxNumberFormatter::class, $this->taxNumber(), $this->country());
94+
}
95+
96+
/**
97+
* Split the value.
98+
*
99+
* @return void
100+
*/
101+
protected function split(): void
102+
{
103+
$this->country = str($this->number)
104+
->substr(0, 2)
105+
->upper()
106+
->value();
107+
108+
$this->number = str($this->number)
109+
->substr(2)
110+
->value();
91111
}
92112

93113
/**

0 commit comments

Comments
 (0)