Skip to content

Commit 6533b81

Browse files
committed
Composer update; Add TypeCastingHelper
1 parent 5853c17 commit 6533b81

File tree

9 files changed

+928
-819
lines changed

9 files changed

+928
-819
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@
148148
"@phpunit:all"
149149
],
150150

151-
"phpstan:run:soft": "bin/header/phpstan 'vendor/bin/phpstan analyse --level 5 --memory-limit 512M --xdebug -c phpstan.dist.neon src tests' 'run'",
152-
"phpstan:run": "bin/header/phpstan 'vendor/bin/phpstan analyse --level max --memory-limit 512M --xdebug -c phpstan.dist.neon src tests' 'run'",
151+
"phpstan:run:soft": "bin/header/phpstan 'vendor/bin/phpstan analyse --level 5 --memory-limit 512M -c phpstan.dist.neon src tests' 'run'",
152+
"phpstan:run": "bin/header/phpstan 'vendor/bin/phpstan analyse --level max --memory-limit 512M -c phpstan.dist.neon src tests' 'run'",
153153
"phpstan": [
154154
"@phpstan:run"
155155
],

composer.lock

Lines changed: 713 additions & 795 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ApiPlatform/State/Base/BaseProvider.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixnode/php-api-version-bundle project.
75
*
@@ -11,15 +9,19 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\PhpApiVersionBundle\ApiPlatform\State\Base;
1515

1616
use ApiPlatform\State\ProcessorInterface;
1717
use ApiPlatform\State\ProviderInterface;
1818
use Ixnode\PhpApiVersionBundle\ApiPlatform\Resource\Base\BasePublicResource;
1919
use Ixnode\PhpApiVersionBundle\ApiPlatform\Route\VersionRoute;
2020
use Ixnode\PhpApiVersionBundle\ApiPlatform\State\VersionProvider;
21+
use Ixnode\PhpApiVersionBundle\Utils\TypeCasting\TypeCastingHelper;
2122
use Ixnode\PhpException\ArrayType\ArrayKeyNotFoundException;
2223
use Ixnode\PhpException\Case\CaseInvalidException;
24+
use Ixnode\PhpException\Type\TypeInvalidException;
2325
use Symfony\Component\Console\Input\ArrayInput;
2426
use Symfony\Component\Console\Input\InputArgument;
2527
use Symfony\Component\Console\Input\InputDefinition;
@@ -140,13 +142,14 @@ protected function getRouteProperties(): array
140142
*
141143
* @return string
142144
* @throws ArrayKeyNotFoundException
145+
* @throws TypeInvalidException
143146
*/
144147
protected function getProjectDir(): string
145148
{
146149
if (!$this->parameterBag->has(self::NAME_KERNEL_PROJECT_DIR)) {
147150
throw new ArrayKeyNotFoundException(self::NAME_KERNEL_PROJECT_DIR);
148151
}
149152

150-
return strval($this->parameterBag->get(self::NAME_KERNEL_PROJECT_DIR));
153+
return (new TypeCastingHelper($this->parameterBag->get(self::NAME_KERNEL_PROJECT_DIR)))->strval();
151154
}
152155
}

src/Command/Base/BaseCommand.php

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixnode/php-api-version-bundle project.
75
*
@@ -11,13 +9,18 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\PhpApiVersionBundle\Command\Base;
1515

1616
use Ixnode\PhpApiVersionBundle\Utils\Db\Entity;
1717
use Ixnode\PhpApiVersionBundle\Utils\Db\Repository;
1818
use Exception;
19+
use Ixnode\PhpApiVersionBundle\Utils\TypeCasting\TypeCastingHelper;
1920
use Ixnode\PhpContainer\Json;
2021
use Ixnode\PhpException\Case\CaseInvalidException;
22+
use Ixnode\PhpException\File\FileNotFoundException;
23+
use Ixnode\PhpException\File\FileNotReadableException;
2124
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
2225
use Ixnode\PhpException\Type\TypeInvalidException;
2326
use Ixnode\PhpNamingConventions\Exception\FunctionReplaceException;
@@ -97,13 +100,14 @@ abstract protected function configureCommand(): void;
97100
*
98101
* @param mixed $value
99102
* @return string
103+
* @throws TypeInvalidException
100104
*/
101105
protected function getValue(mixed $value): string
102106
{
103107
return match (true) {
104108
is_string($value) => $value,
105109
is_bool($value) => $value ? 'Yes' : 'No',
106-
default => strval($value),
110+
default => (new TypeCastingHelper($value))->strval(),
107111
};
108112
}
109113

@@ -113,18 +117,21 @@ protected function getValue(mixed $value): string
113117
* @param mixed $key
114118
* @param string|null $recursiveName
115119
* @return string
120+
* @throws TypeInvalidException
116121
*/
117122
protected function getKey(mixed $key, ?string $recursiveName): string
118123
{
119124
if (is_numeric($key)) {
120125
$key = sprintf('%d:', $key);
121126
}
122127

128+
$key = (new TypeCastingHelper($key))->strval();
129+
123130
if ($recursiveName === null) {
124-
return strval($key);
131+
return $key;
125132
}
126133

127-
return sprintf('%s_%s', $recursiveName, strval($key));
134+
return sprintf('%s_%s', $recursiveName, $key);
128135
}
129136

130137
/**
@@ -168,9 +175,11 @@ protected function printText(OutputInterface $output, array $outputArray, ?strin
168175
* @param OutputInterface $output
169176
* @param array<int|string, mixed> $array
170177
* @return void
171-
* @throws JsonException
172178
* @throws FunctionJsonEncodeException
179+
* @throws JsonException
173180
* @throws TypeInvalidException
181+
* @throws FileNotFoundException
182+
* @throws FileNotReadableException
174183
*/
175184
protected function printJson(OutputInterface $output, array $array): void
176185
{
@@ -229,7 +238,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
229238
$this->input = $input;
230239
$this->output = $output;
231240

232-
$format = strval($this->input->getOption(self::NAME_OPTION_FORMAT));
241+
$format = (new TypeCastingHelper($this->input->getOption(self::NAME_OPTION_FORMAT)))->strval();
233242

234243
match ($format) {
235244
self::OPTION_FORMAT_TEXT => $this->printText($output, $this->executeCommand()),

src/Command/Version/VersionCommand.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixnode/php-api-version-bundle project.
75
*
@@ -11,12 +9,17 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\PhpApiVersionBundle\Command\Version;
1515

1616
use Exception;
1717
use Ixnode\BashVersionManager\Version;
18+
use Ixnode\PhpApiVersionBundle\Utils\TypeCasting\TypeCastingHelper;
1819
use Ixnode\PhpContainer\Json;
1920
use Ixnode\PhpException\Case\CaseInvalidException;
21+
use Ixnode\PhpException\File\FileNotFoundException;
22+
use Ixnode\PhpException\File\FileNotReadableException;
2023
use Ixnode\PhpException\Function\FunctionJsonEncodeException;
2124
use Ixnode\PhpException\Type\TypeInvalidException;
2225
use Ixnode\PhpNamingConventions\NamingConventions;
@@ -162,9 +165,11 @@ protected function printText(OutputInterface $output, array $versionArray): void
162165
* @param OutputInterface $output
163166
* @param array{version: string, license: string, authors: string[], php-version: string, symfony-version: string} $versionArray
164167
* @return void
165-
* @throws TypeInvalidException
166168
* @throws FunctionJsonEncodeException
167169
* @throws JsonException
170+
* @throws TypeInvalidException
171+
* @throws FileNotFoundException
172+
* @throws FileNotReadableException
168173
*/
169174
protected function printJson(OutputInterface $output, array $versionArray): void
170175
{
@@ -185,7 +190,7 @@ protected function printJson(OutputInterface $output, array $versionArray): void
185190
*/
186191
protected function execute(InputInterface $input, OutputInterface $output): int
187192
{
188-
$format = strval($input->getOption(self::NAME_OPTION_FORMAT));
193+
$format = (new TypeCastingHelper($input->getOption(self::NAME_OPTION_FORMAT)))->strval();
189194

190195
match ($format) {
191196
self::OPTION_FORMAT_TEXT => $this->printText($output, $this->getVersionArray()),

src/Constants/Api/ApiRoute.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
/*
64
* This file is part of the ixnode/php-api-version-bundle project.
75
*
@@ -11,6 +9,8 @@
119
* file that was distributed with this source code.
1210
*/
1311

12+
declare(strict_types=1);
13+
1414
namespace Ixnode\PhpApiVersionBundle\Constants\Api;
1515

1616
/**
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the ixnode/php-api-version-bundle project.
5+
*
6+
* (c) Björn Hempel <https://www.hempel.li/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.md
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Ixnode\PhpApiVersionBundle\Utils\TypeCasting;
15+
16+
use Ixnode\PhpException\Type\TypeInvalidException;
17+
18+
/**
19+
* Class TypeCastingHelper.
20+
*
21+
* @author Björn Hempel <bjoern@hempel.li>
22+
* @version 0.1.0 (2023-06-24)
23+
* @since 0.1.0 (2023-06-24) First version.
24+
*/
25+
class TypeCastingHelper
26+
{
27+
/**
28+
* @param mixed $value
29+
*/
30+
public function __construct(protected mixed $value)
31+
{
32+
}
33+
34+
/**
35+
* Checks if the specified value can be converted to a string (with exception).
36+
*
37+
* @param string|null $default
38+
* @return string
39+
* @throws TypeInvalidException
40+
*/
41+
public function strval(?string $default = null): string
42+
{
43+
if (
44+
is_bool($this->value) ||
45+
is_float($this->value) ||
46+
is_int($this->value) ||
47+
is_resource($this->value) ||
48+
is_string($this->value) ||
49+
is_null($this->value)
50+
) {
51+
return (string) $this->value;
52+
}
53+
54+
/* Use default if not convertible. '' (empty string) for example. */
55+
if (is_string($default)) {
56+
return $default;
57+
}
58+
59+
throw new TypeInvalidException('string', gettype($this->value));
60+
}
61+
62+
/**
63+
* Checks if the specified value can be converted to a string (without exception).
64+
*
65+
* @param string|null $default
66+
* @return string
67+
*/
68+
public function strvalNE(?string $default = null): string
69+
{
70+
$default = $default ?: '';
71+
72+
try {
73+
return $this->strval($default);
74+
} catch (TypeInvalidException) {
75+
return $default;
76+
}
77+
}
78+
79+
/**
80+
* Checks if the specified value can be converted to an integer (with exception).
81+
*
82+
* @param int|null $default
83+
* @return int
84+
* @throws TypeInvalidException
85+
*/
86+
public function intval(?int $default = null): int
87+
{
88+
if (
89+
is_array($this->value) ||
90+
is_bool($this->value) ||
91+
is_float($this->value) ||
92+
is_int($this->value) ||
93+
is_resource($this->value) ||
94+
is_string($this->value) ||
95+
is_null($this->value)
96+
) {
97+
return (int) $this->value;
98+
}
99+
100+
/* Use default if not convertible. '' (empty string) for example. */
101+
if (is_int($default)) {
102+
return $default;
103+
}
104+
105+
throw new TypeInvalidException('int', gettype($this->value));
106+
}
107+
108+
/**
109+
* Checks if the specified value can be converted to an integer (without exception).
110+
*
111+
* @param int|null $default
112+
* @return int
113+
*/
114+
public function intvalNE(?int $default = null): int
115+
{
116+
$default = $default ?: 0;
117+
118+
try {
119+
return $this->intval($default);
120+
} catch (TypeInvalidException) {
121+
return $default;
122+
}
123+
}
124+
125+
/**
126+
* Checks if the specified value can be converted to a float (with exception).
127+
*
128+
* @param float|null $default
129+
* @return float
130+
* @throws TypeInvalidException
131+
*/
132+
public function floatval(?float $default = null): float
133+
{
134+
if (
135+
is_array($this->value) ||
136+
is_bool($this->value) ||
137+
is_float($this->value) ||
138+
is_int($this->value) ||
139+
is_string($this->value) ||
140+
is_null($this->value)
141+
) {
142+
return (float) $this->value;
143+
}
144+
145+
/* Use default if not convertible. '' (empty string) for example. */
146+
if (is_float($default)) {
147+
return $default;
148+
}
149+
150+
throw new TypeInvalidException('float', gettype($this->value));
151+
}
152+
153+
/**
154+
* Checks if the specified value can be converted to a float (without exception).
155+
*
156+
* @param float|null $default
157+
* @return float
158+
*/
159+
public function floatvalNE(?float $default = null): float
160+
{
161+
$default = $default ?: 0;
162+
163+
try {
164+
return $this->floatval($default);
165+
} catch (TypeInvalidException) {
166+
return $default;
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)