Skip to content

Commit c5b18ff

Browse files
committed
feat: add core attribute, contract, and exception classes for transformer
- Implemented `Transform` attribute in `src/Attribute/Transform.php`: - Extends `BaseProcessorAttribute` to mark properties for transformation processing. - Defined `TransformationResult` interface in `src/Contract/TransformationResult.php`: - Provides contract methods for result validation, error handling, and transformed data retrieval. - Created `TransformerException` in `src/Exception/TransformerException.php`: - Extends `AbstractException` to handle transformer-specific errors. - Added methods `invalidInput`, `invalidFormat`, and `invalidType` with detailed error codes and messages for input type, format, and type validation.
1 parent 2bb6c62 commit c5b18ff

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

src/Attribute/Transform.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Transformer\Attribute;
6+
7+
use KaririCode\Contract\Processor\Attribute\BaseProcessorAttribute;
8+
9+
#[\Attribute(\Attribute::TARGET_PROPERTY)]
10+
final class Transform extends BaseProcessorAttribute
11+
{
12+
}

src/Contract/TransformationResult.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Transformer\Contract;
6+
7+
interface TransformationResult
8+
{
9+
public function isValid(): bool;
10+
11+
public function getErrors(): array;
12+
13+
public function getTransformedData(): array;
14+
15+
public function toArray(): array;
16+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KaririCode\Transformer\Exception;
6+
7+
use KaririCode\Exception\AbstractException;
8+
9+
final class TransformerException extends AbstractException
10+
{
11+
private const CODE_INVALID_INPUT = 5001;
12+
private const CODE_INVALID_FORMAT = 5002;
13+
private const CODE_INVALID_TYPE = 5003;
14+
15+
public static function invalidInput(string $expectedType, string $actualType): self
16+
{
17+
$message = sprintf(
18+
'Invalid input type. Expected %s, got %s.',
19+
$expectedType,
20+
$actualType
21+
);
22+
23+
return self::createException(
24+
self::CODE_INVALID_INPUT,
25+
'INVALID_INPUT_TYPE',
26+
$message
27+
);
28+
}
29+
30+
public static function invalidFormat(string $format, string $value): self
31+
{
32+
$message = sprintf(
33+
'Invalid format. Expected format %s, got %s.',
34+
$format,
35+
$value
36+
);
37+
38+
return self::createException(
39+
self::CODE_INVALID_FORMAT,
40+
'INVALID_FORMAT',
41+
$message
42+
);
43+
}
44+
45+
public static function invalidType(string $expectedType): self
46+
{
47+
$message = sprintf(
48+
'Invalid type. Expected %s.',
49+
$expectedType
50+
);
51+
52+
return self::createException(
53+
self::CODE_INVALID_TYPE,
54+
'INVALID_TYPE',
55+
$message
56+
);
57+
}
58+
}

0 commit comments

Comments
 (0)