Skip to content

Commit

Permalink
main
Browse files Browse the repository at this point in the history
  • Loading branch information
6562680 committed Jun 27, 2024
1 parent 68be524 commit 1a49739
Showing 1 changed file with 28 additions and 37 deletions.
65 changes: 28 additions & 37 deletions src/Lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,13 @@ public static function php_throwable($error = null, ...$errors) : ?object
* previousList: string[],
* messageCodeList: array[],
* messageDataList: array[],
* messageObjectList: object[],
* message: ?string,
* code: ?int,
* previous: ?string,
* messageCode: ?string,
* messageData: ?array,
* messageObject: ?object,
* unresolved: array,
* __unresolved: array,
* }
*/
public static function php_throwable_args($arg = null, ...$args) : array
Expand All @@ -126,34 +125,31 @@ public static function php_throwable_args($arg = null, ...$args) : array
$previousList = null;
$messageCodeList = null;
$messageDataList = null;
$messageObjectList = null;

$message = null;
$code = null;
$previous = null;
$messageCode = null;
$messageData = null;
$messageObject = null;

$unresolved = [];
$__unresolved = [];

for ( $i = 0; $i < $len; $i++ ) {
$a = $args[ $i ];

if (is_int($a)) {
$code = $codeList[ $i ] = $a;
$codeList[ $i ] = $a;

continue;
}

if (is_a($a, \Throwable::class)) {
$previous = $previousList[ $i ] = $a;
$previousList[ $i ] = $a;

continue;
}

if (null !== ($vString = static::filter_string($a))) {
$message = $messageList[ $i ] = $vString;
if ('' !== ($vString = (string) $a)) {
$messageList[ $i ] = $vString;

continue;
}
Expand All @@ -162,39 +158,28 @@ public static function php_throwable_args($arg = null, ...$args) : array
is_array($a)
|| is_a($a, \stdClass::class)
) {
$messageData = $messageDataList[ $i ] = (array) $a;
$messageDataList[ $i ] = (array) $a;

continue;
}
if ('' !== ($messageString = (string) $messageDataList[ $i ][ 0 ])) {
$messageList[ $i ] = $messageString;

$unresolved[ $i ] = $a;
}
unset($messageDataList[ $i ][ 0 ]);

for ( $i = $len - 1; $i >= 0; $i-- ) {
if (isset($messageDataList[ $i ][ 0 ])) {
if ($messageString = static::filter_string($messageDataList[ $i ][ 0 ])) {
$message = $messageList[ $i ] = $messageString;
if (! $messageDataList[ $i ]) {
unset($messageDataList[ $i ]);
}
}
}

if (isset($messageList[ $i ])) {
if (preg_match('/^[a-z](?!.*\s)/i', $messageList[ $i ])) {
$messageCode = $messageCodeList[ $i ] = strtoupper($messageList[ $i ]);
}
}

if (null !== $messageDataList[ $i ]) {
$messageObject = $messageObjectList[ $i ] = (object) $messageDataList[ $i ];

} elseif (null !== $messageList[ $i ]) {
$messageObject = $messageObjectList[ $i ] = (object) [ $messageList[ $i ] ];
continue;
}

if (null !== $messageDataList[ $i ]) {
unset($messageDataList[ $i ][ 0 ]);
$__unresolved[ $i ] = $a;
}

if (empty($messageDataList[ $i ])) {
$messageDataList[ $i ] = null;
for ( $i = 0; $i < $len; $i++ ) {
if (isset($messageList[ $i ])) {
if (preg_match('/^[a-z](?!.*\s)/i', $messageList[ $i ])) {
$messageCodeList[ $i ] = strtoupper($messageList[ $i ]);
}
}
}
Expand All @@ -204,16 +189,22 @@ public static function php_throwable_args($arg = null, ...$args) : array
$result[ 'messageList' ] = $messageList;
$result[ 'codeList' ] = $codeList;
$result[ 'previousList' ] = $previousList;
$result[ 'messageCodeList' ] = $messageCodeList;
$result[ 'messageDataList' ] = $messageDataList;
$result[ 'messageObjectList' ] = $messageObjectList;

$messageDataList = $messageDataList ?? [];
$messageData = array_replace(...$messageDataList);
$messageObject = (object) ([ $message ] + $messageData);

$result[ 'message' ] = $message;
$result[ 'code' ] = $code;
$result[ 'previous' ] = $previous;
$result[ 'messageCode' ] = $messageCode;
$result[ 'messageData' ] = $messageData;

$result[ 'messageObject' ] = $messageObject;

$result[ 'unresolved' ] = $unresolved;
$result[ '__unresolved' ] = $__unresolved;

return $result;
}
Expand Down

0 comments on commit 1a49739

Please sign in to comment.