Skip to content

Commit 239a7e4

Browse files
committed
Introduce Types and refactor
1 parent 446084a commit 239a7e4

File tree

4 files changed

+78
-11
lines changed

4 files changed

+78
-11
lines changed

src/Services/DtoBuilder.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpDto\DtoSerialize;
66
use PhpDto\Enum\Types;
7+
use PhpDto\Types\Prop;
78

89
class DtoBuilder
910
{
@@ -97,16 +98,19 @@ public function getConstructorParam( array $configs ): string
9798
*/
9899
public function getConstructorProps( array $configs ): array
99100
{
100-
$keys = [];
101+
$props = [];
101102

102103
foreach ( $configs['props'] as $key => $value )
103104
{
104-
$key = $this->convertPropToSnakeCase($key);
105+
$prop = new Prop();
105106

106-
$keys[] = $key;
107+
$prop->setIsNullable(str_contains(haystack: $key, needle: $value))
108+
->setName($this->convertPropToSnakeCase($key));
109+
110+
$props[] = $prop;
107111
}
108112

109-
return $keys;
113+
return $props;
110114
}
111115

112116
/**

src/Services/Sticker.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace PhpDto\Services;
44

5+
use PhpDto\Types\Prop;
6+
57
class Sticker
68
{
79
/**
@@ -111,7 +113,7 @@ public function props( array $props ): Sticker
111113

112114
/**
113115
* @param string $param
114-
* @param array $props
116+
* @param Prop[] $props
115117
* @return Sticker
116118
*/
117119
public function constructor( string $param, array $props ): Sticker
@@ -121,10 +123,11 @@ public function constructor( string $param, array $props ): Sticker
121123

122124
foreach ($props as $prop)
123125
{
124-
$nullHandler = str_contains(haystack: $prop, needle: '?') ? ' ?? null' : '';
126+
$propName = $prop->getName();
127+
$nullHandler = $prop->isIsNullable() ? ' ?? null' : '';
125128

126-
$str .= "\t\t" . '$this->_' . $prop . ' = $' .
127-
$param . "['" . strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $prop)) . "']" .
129+
$str .= "\t\t" . '$this->_' . $propName . ' = $' .
130+
$param . "['" . strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $propName)) . "']" .
128131
$nullHandler . ";\n";
129132
}
130133

src/Types/Prop.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace PhpDto\Types;
4+
5+
class Prop
6+
{
7+
/**
8+
* @var string
9+
*/
10+
private string $_name;
11+
12+
/**
13+
* @var bool
14+
*/
15+
private bool $_nullable;
16+
17+
/**
18+
* @return string
19+
*/
20+
public function getName(): string
21+
{
22+
return $this->_name;
23+
}
24+
25+
/**
26+
* @param string $name
27+
* @return Prop
28+
*/
29+
public function setName(string $name): Prop
30+
{
31+
$this->_name = $name;
32+
return $this;
33+
}
34+
35+
/**
36+
* @return bool
37+
*/
38+
public function isIsNullable(): bool
39+
{
40+
return $this->_nullable;
41+
}
42+
43+
/**
44+
* @param bool $isNullable
45+
* @return Prop
46+
*/
47+
public function setIsNullable(bool $isNullable): Prop
48+
{
49+
$this->_nullable = $isNullable;
50+
return $this;
51+
}
52+
}

tests/Unit/Services/DtoBuilderTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpDto\Enum\Types;
66
use PhpDto\Services\DtoBuilder;
7+
use PhpDto\Types\Prop;
78
use PHPUnit\Framework\TestCase;
89

910
class DtoBuilderTest extends TestCase
@@ -135,7 +136,9 @@ public function testGetConstructorParam()
135136

136137
public function testGetConstructorProps()
137138
{
138-
$expected = [
139+
$props = $this->_builder->getConstructorProps( $this->_configs );
140+
141+
$propNames = [
139142
'id',
140143
'count',
141144
'name',
@@ -147,8 +150,13 @@ public function testGetConstructorProps()
147150
];
148151

149152
$this->assertEquals(
150-
$expected,
151-
$this->_builder->getConstructorProps( $this->_configs )
153+
expected: $propNames,
154+
actual: array_map(
155+
callback: function(Prop $prop) {
156+
return $prop->getName();
157+
},
158+
array: $props
159+
)
152160
);
153161
}
154162

0 commit comments

Comments
 (0)