22
33namespace SolutionForest \OcppPhp \Ocpp \Messages ;
44
5+ use SolutionForest \OcppPhp \Ocpp \Exceptions \FormatViolationError ;
6+ use SolutionForest \OcppPhp \Ocpp \Exceptions \FormationViolationError ;
7+ use SolutionForest \OcppPhp \Ocpp \Exceptions \GenericError ;
8+ use SolutionForest \OcppPhp \Ocpp \Exceptions \InternalError ;
9+ use SolutionForest \OcppPhp \Ocpp \Exceptions \NotImplementedError ;
10+ use SolutionForest \OcppPhp \Ocpp \Exceptions \NotSupportedError ;
11+ use SolutionForest \OcppPhp \Ocpp \Exceptions \OccurenceConstraintViolationError ;
12+ use SolutionForest \OcppPhp \Ocpp \Exceptions \OccurrenceConstraintViolationError ;
13+ use SolutionForest \OcppPhp \Ocpp \Exceptions \PropertyConstraintViolationError ;
14+ use SolutionForest \OcppPhp \Ocpp \Exceptions \ProtocolError ;
15+ use SolutionForest \OcppPhp \Ocpp \Exceptions \SecurityError ;
16+ use SolutionForest \OcppPhp \Ocpp \Exceptions \TypeConstraintViolationError ;
17+ use SolutionForest \OcppPhp \Ocpp \Exceptions \UnknownCallErrorCodeError ;
18+
519abstract class CallError extends Message
620{
721 public int $ messageTypeID = 4 ;
@@ -12,6 +26,27 @@ abstract class CallError extends Message
1226
1327 public array $ errorDetails = [];
1428
29+ /**
30+ * Map of OCPP error code strings to their corresponding CallError exception classes.
31+ *
32+ * @var array<string, class-string<CallError>>
33+ */
34+ private const ERROR_CODE_MAP = [
35+ 'NotImplemented ' => NotImplementedError::class,
36+ 'NotSupported ' => NotSupportedError::class,
37+ 'InternalError ' => InternalError::class,
38+ 'ProtocolError ' => ProtocolError::class,
39+ 'SecurityError ' => SecurityError::class,
40+ 'FormatViolation ' => FormatViolationError::class,
41+ 'FormationViolation ' => FormationViolationError::class,
42+ 'GenericError ' => GenericError::class,
43+ 'TypeConstraintViolation ' => TypeConstraintViolationError::class,
44+ 'OccurrenceConstraintViolation ' => OccurrenceConstraintViolationError::class,
45+ 'OccurenceConstraintViolation ' => OccurenceConstraintViolationError::class,
46+ 'PropertyConstraintViolation ' => PropertyConstraintViolationError::class,
47+ ];
48+
49+
1550 public function __construct (string $ uniqueId , ?string $ errorCode = null , ?string $ errorDescription = null , ?array $ errorDetails = null )
1651 {
1752 $ this ->uniqueId = $ uniqueId ;
@@ -27,6 +62,34 @@ public function __construct(string $uniqueId, ?string $errorCode = null, ?string
2762 }
2863 }
2964
65+ /**
66+ * Create a CallError object from a raw OCPP message array.
67+ *
68+ * @param array $message The raw OCPP message [4, UniqueId, errorCode, errorDescription, errorDetails]
69+ * @return static
70+ */
71+ public static function fromArray (array $ message ): static
72+ {
73+ if ($ message [0 ] !== 4 ) {
74+ throw new \Exception ("Invalid message type for CallError, expected 4 got {$ message [0 ]}" );
75+ }
76+
77+ $ uniqueId = $ message [1 ];
78+ $ errorCode = $ message [2 ] ?? '' ;
79+ $ errorDescription = $ message [3 ] ?? '' ;
80+ $ errorDetails = $ message [4 ] ?? [];
81+
82+ $ class = self ::ERROR_CODE_MAP [$ errorCode ] ?? UnknownCallErrorCodeError::class;
83+
84+ /** @var static $instance */
85+ $ instance = new $ class ($ uniqueId );
86+ $ instance ->errorCode = $ errorCode ;
87+ $ instance ->errorDescription = $ errorDescription ;
88+ $ instance ->errorDetails = (array ) $ errorDetails ;
89+
90+ return $ instance ;
91+ }
92+
3093 public function toArray (): array
3194 {
3295 return [
0 commit comments