Skip to content

Commit

Permalink
Merge branch 'master' into FixMentionsBug
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelfolaron authored Apr 2, 2024
2 parents 5d39a92 + add8c06 commit b4a806f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
40 changes: 30 additions & 10 deletions app/Core/Support/Cast.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Leantime\Core\Support;

use Carbon\CarbonImmutable;
use Illuminate\Support\Str;

/**
Expand All @@ -23,8 +24,8 @@ public function __construct(private array|object $object)

/**
* @param string $classDest
* @param array $constructParams
* @param array $mappings
* @param array $constructParams
* @param array $mappings
* @return object
* @throws \InvalidArgumentException
* @throws \RuntimeException
Expand Down Expand Up @@ -100,23 +101,25 @@ classDest: $type,
}

/**
* @param mixed $value
* @param mixed $value
* @param string $simpleType
* @return mixed
* @throws \RuntimeException
* @throws \InvalidArgumentException
**/
public static function castSimple(mixed $value, string $simpleType): mixed
{
if (is_null($castedValue = match ($simpleType) {
if (
is_null($castedValue = match ($simpleType) {
'int', 'integer' => filter_var($value, FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE),
'float' => filter_var($value, FILTER_VALIDATE_FLOAT, FILTER_NULL_ON_FAILURE),
'string', 'str' => is_array($value) || (is_object($value) && ! method_exists($value, '__toString')) ? null : (string) $value,
'bool', 'boolean' => filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
'object', 'stdClass' => is_array($value) ? (object) $value : null,
'array' => is_object($value) || is_array($value) ? (array) $value : null,
default => throw new \InvalidArgumentException(sprintf('%s is not a simple type.', $simpleType)),
})) {
})
) {
throw new \RuntimeException(sprintf('Could not cast value to type %s.', $simpleType));
}

Expand All @@ -126,7 +129,7 @@ public static function castSimple(mixed $value, string $simpleType): mixed
/**
* Cast to backed enum
*
* @param mixed $value
* @param mixed $value
* @param string $enumClass
**/
public static function castEnum(mixed $value, string $enumClass): mixed
Expand All @@ -151,13 +154,30 @@ public static function castEnum(mixed $value, string $enumClass): mixed
throw new \InvalidArgumentException(sprintf('Value cannot be casted to %s.', $enumClass));
}

/**
* Casts a string value into a datetime object.
*
* @param string $value The value to be casted into a datetime object.
* @return \DateTime The datetime object.
* @throws \InvalidArgumentException If the value is not a valid datetime string.
**/
public static function castDateTime(string $value)
{
if (is_string($value)) {
return dtHelper()->parseDbDateTime($value);
}

throw new \InvalidArgumentException('Value cannot be casted datetime');
}

/**
* @param array|object $iterator
* @param array $mappings
* @param array $mappings
* @return array|object
**/
protected function handleIterator(iterable $iterator, array $mappings = []): array|object {
$result = is_object($iterator) ? new \stdClass : [];
protected function handleIterator(iterable $iterator, array $mappings = []): array|object
{
$result = is_object($iterator) ? new \stdClass() : [];

foreach ($iterator as $key => $value) {
if (is_numeric($key)) {
Expand Down Expand Up @@ -191,7 +211,7 @@ protected function handleIterator(iterable $iterator, array $mappings = []): arr
}

/**
* @param array $mappings
* @param array $mappings
* @param string $propName
* @return array
**/
Expand Down
8 changes: 4 additions & 4 deletions app/Domain/Api/Controllers/Jsonrpc.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,15 +307,16 @@ private function prepareParameters(array $params, array $methodParams): array
}

try {
// @TODO: this is override in linge 318 before the casted value here is used?
$filtered_parameters[$position] = cast($params[$name], $type->getName());
} catch (\Throwable $e) {
error_log($e);
throw new \Exception("Could not cast parameter: $name. See server logs for more details.");
}
}

$filtered_parameters[$position] = $params[$name];
if (!isset($filtered_parameters[$position])) {
$filtered_parameters[$position] = $params[$name];
}
}

// make sure it is in the right order
Expand Down Expand Up @@ -368,13 +369,12 @@ private function returnError(string $errorMessage, int $errorcode, mixed $additi
{

//TODO: And FYI. json_encode cannot encode throwable. https://github.com/pmjones/throwable-properties
//For now we'll just return the message
return $this->tpl->displayJson([
'jsonrpc' => '2.0',
'error' => [
'code' => $errorcode,
'message' => $errorMessage,
'data' => $additional_info->getMessage(),
'data' => $additional_info instanceof \Throwable ? $additional_info->getMessage() : $additional_info,
],
'id' => $id,
]);
Expand Down
12 changes: 9 additions & 3 deletions app/helpers.php
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
<?php

use Carbon\Carbon;
use Carbon\CarbonInterface;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\View\Factory;
use Leantime\Core\Application;
use Leantime\Core\AppSettings;
use Leantime\Core\Bootloader;
use Leantime\Core\Language;
use Leantime\Core\Support\Build;
use Leantime\Core\Support\Cast;
use Leantime\Core\Support\DateTimeHelper;
use Leantime\Core\Support\Format;
use Leantime\Core\Support\FromFormat;
use Leantime\Core\Support\Cast;
use Leantime\Core\Support\Mix;
use \Leantime\Core\Support\DateTimeHelper;
use Carbon\Carbon;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

Expand Down Expand Up @@ -253,6 +254,11 @@ function cast(mixed $source, string $classOrType, array $constructParams = [], a
return Cast::castEnum($source, $classOrType);
}

// Convert string to date if required.
if (is_string($source) && is_a($classOrType, CarbonInterface::class, true)) {
return Cast::castDateTime($source);
}

return (new Cast($source))->castTo($classOrType, $constructParams, $mappings);
}
}
Expand Down

0 comments on commit b4a806f

Please sign in to comment.