-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathContentBuilder.php
More file actions
52 lines (42 loc) · 1.68 KB
/
ContentBuilder.php
File metadata and controls
52 lines (42 loc) · 1.68 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
52
<?php
declare(strict_types=1);
namespace Picamator\TransferObject\DefinitionGenerator\Content\Builder;
use Picamator\TransferObject\DefinitionGenerator\Content\Enum\GetTypeEnum;
use Picamator\TransferObject\DefinitionGenerator\Exception\DefinitionGeneratorException;
use Picamator\TransferObject\Shared\Validator\VariableValidatorTrait;
readonly class ContentBuilder implements ContentBuilderInterface
{
use VariableValidatorTrait;
public function createBuilderContent(string $propertyName, mixed $propertyValue): ContentInterface
{
$this->assertPropertyName($propertyName);
$typeEnum = $this->getTypeEnum($propertyName, $propertyValue);
return new Content($typeEnum, $propertyName, $propertyValue);
}
/**
* @throws \Picamator\TransferObject\DefinitionGenerator\Exception\DefinitionGeneratorException
*/
private function getTypeEnum(string $propertyName, mixed $propertyValue): GetTypeEnum
{
$propertyType = gettype($propertyValue);
$typeEnum = GetTypeEnum::tryFrom($propertyType);
if ($typeEnum !== null) {
return $typeEnum;
}
throw new DefinitionGeneratorException(
sprintf('Property "%s" with type "%s" is not supported.', $propertyName, $propertyType),
);
}
/**
* @throws \Picamator\TransferObject\DefinitionGenerator\Exception\DefinitionGeneratorException
*/
private function assertPropertyName(string $propertyName): void
{
if ($this->isValidVariable($propertyName)) {
return;
}
throw new DefinitionGeneratorException(
sprintf('Invalid property name "%s".', $propertyName),
);
}
}