-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Kafka Messenger] Added GeneratorMap support
- Loading branch information
1 parent
90ae280
commit b4f8bc5
Showing
3 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FRZB\Component\Messenger\Bridge\Kafka\Exception; | ||
|
||
use Fp\Collections\Entry; | ||
use Fp\Collections\HashMap; | ||
|
||
final class DiscriminatorMapException extends \LogicException | ||
{ | ||
private const MISSING_PARAMETER_ERROR_MESSAGE = 'Missing property "%s" with class "%s"'; | ||
private const MISSING_TYPE_PROPERTY_ERROR_MESSAGE = 'Missing mapping for type "%s" in class "%s" with mapping "%s"'; | ||
|
||
public static function fromThrowable(\Throwable $previous): self | ||
{ | ||
return new self($previous->getMessage(), $previous->getCode(), $previous); | ||
} | ||
|
||
public static function missingProperty(string $className, string $propertyName, ?\Throwable $previous = null): self | ||
{ | ||
$message = sprintf(self::MISSING_PARAMETER_ERROR_MESSAGE, $propertyName, $className); | ||
|
||
return new self($message, previous: $previous); | ||
} | ||
|
||
public static function missingTypeProperty(string $className, string $typeProperty, array $mapping, ?\Throwable $previous = null): self | ||
{ | ||
$map = HashMap::collect($mapping) | ||
->map(static fn (Entry $entry) => "{$entry->key}: {$entry->value}") | ||
->toArrayList() | ||
->mkString() | ||
; | ||
|
||
$message = sprintf(self::MISSING_TYPE_PROPERTY_ERROR_MESSAGE, $typeProperty, $className, $map); | ||
|
||
return new self($message, previous: $previous); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace FRZB\Component\Messenger\Bridge\Kafka\Helper; | ||
|
||
use Fp\Collections\ArrayList; | ||
use FRZB\Component\Messenger\Bridge\Kafka\Exception\DiscriminatorMapException; | ||
use JetBrains\PhpStorm\Immutable; | ||
use Symfony\Component\Serializer\Annotation\DiscriminatorMap; | ||
|
||
#[Immutable] | ||
final class ClassHelper | ||
{ | ||
public static function getClassName(string $targetClass, array|string $targetPayload): string | ||
{ | ||
try { | ||
$targetPayload = \is_array($targetPayload) ? $targetPayload : json_decode($targetPayload, true, flags: \JSON_THROW_ON_ERROR); | ||
} catch (\JsonException) { | ||
$targetPayload = []; | ||
} | ||
|
||
if ($discriminatorMap = self::getAttribute($targetClass, DiscriminatorMap::class)) { | ||
$property = $discriminatorMap->getTypeProperty(); | ||
$mapping = $discriminatorMap->getMapping(); | ||
$parameter = $targetPayload[$property] ?? throw DiscriminatorMapException::missingProperty($targetClass, $property); | ||
$targetClass = $mapping[$parameter] ?? throw DiscriminatorMapException::missingTypeProperty($targetClass, $property, $mapping); | ||
} | ||
|
||
return $targetClass; | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $attributeClass | ||
* | ||
* @return null|T | ||
*/ | ||
public static function getAttribute(string|object $target, string $attributeClass): ?object | ||
{ | ||
return ArrayList::collect(self::getAttributes($target, $attributeClass)) | ||
->firstElement() | ||
->get() | ||
; | ||
} | ||
|
||
/** | ||
* @template T | ||
* | ||
* @param class-string<T> $attributeClass | ||
* | ||
* @return array<T> | ||
*/ | ||
public static function getAttributes(string|object $target, string $attributeClass): array | ||
{ | ||
try { | ||
$attributes = (new \ReflectionClass($target))->getAttributes($attributeClass); | ||
} catch (\ReflectionException) { | ||
$attributes = []; | ||
} | ||
|
||
return ArrayList::collect($attributes) | ||
->map(static fn (\ReflectionAttribute $a) => $a->newInstance()) | ||
->toArray() | ||
; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters