Skip to content

Commit 622620d

Browse files
authored
Merge pull request #23 from picamator/development
Release 2.1.1
2 parents 5b96806 + e00334d commit 622620d

27 files changed

Lines changed: 175 additions & 82 deletions

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ Key Features
6868
* implements standard interfaces: `IteratorAggregate`, `JsonSerializable`, and `Countable`
6969
* supports embedded, collections Transfer Objects
7070
* supports PHP primitive data types
71-
* supports `BackedEnum`, `DateTime`, `DateTimeImmutable`, and `BcMath\\Number`
71+
* supports `BackedEnum`, `DateTime`, `DateTimeImmutable`, and `BcMath\Number`
7272
* supports asymmetric property visibility
7373
* integrates external Transfer Objects
7474

composer.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
},
4040
"require": {
4141
"php": ">=8.4",
42-
"ext-bcmath": "*",
4342
"composer-runtime-api": "^2.2",
4443
"psr/container": "^2.0",
4544
"symfony/console": "^7.0",
@@ -53,6 +52,9 @@
5352
"phpunit/phpunit": "^12.0",
5453
"squizlabs/php_codesniffer": "^3.11"
5554
},
55+
"suggest": {
56+
"ext-bcmath": "Required for supporting the `numberType` in Transfer Object Definition Files."
57+
},
5658
"bin": [
5759
"bin/transfer-generate",
5860
"bin/definition-generate"

composer.lock

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

docker/php/bash/.bashrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ wrap_in_green "======================================"
2525
wrap_in_yellow "XDEBUG: $XDEBUG_MODE, PHP: $PHP_VERSION"
2626

2727
# style prompt
28-
PS1="$(color_blue "\w")$ "
28+
PS1="$(color_blue "\W")$ "
2929

3030
# function to display "Bay" when the shell is terminated
3131
function say_goodbye() {

src/Shared/Exception/FileAppenderException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Picamator\TransferObject\Shared\Exception;
66

77
use Exception;
8-
use Picamator\TransferObject\Shared\Exception\TransferExceptionInterface;
98

109
class FileAppenderException extends Exception implements TransferExceptionInterface
1110
{

src/Shared/Exception/JsonReaderException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Picamator\TransferObject\Shared\Exception;
66

77
use Exception;
8-
use Picamator\TransferObject\Shared\Exception\TransferExceptionInterface;
98

109
class JsonReaderException extends Exception implements TransferExceptionInterface
1110
{

src/Transfer/AbstractTransfer.php

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ abstract class AbstractTransfer implements TransferInterface
2626

2727
private const string DATA_INDEX_SUFFIX = '_DATA_INDEX';
2828

29-
/**
30-
* @var array<string, \Picamator\TransferObject\Transfer\Attribute\PropertyTypeAttributeInterface|null>
31-
*/
32-
private array $_attributeCache = [];
33-
3429
/**
3530
* @var \SplFixedArray<mixed>
3631
*/
@@ -149,10 +144,6 @@ final protected function setData(int $index, mixed $value): mixed
149144

150145
private function getConstantAttribute(string $constantName): ?PropertyTypeAttributeInterface
151146
{
152-
if (array_key_exists($constantName, $this->_attributeCache)) {
153-
return $this->_attributeCache[$constantName];
154-
}
155-
156147
$reflection = new ReflectionClassConstant($this, $constantName);
157148
$attributeReflections = $reflection->getAttributes(
158149
name: PropertyTypeAttributeInterface::class,
@@ -161,7 +152,7 @@ private function getConstantAttribute(string $constantName): ?PropertyTypeAttrib
161152

162153
$attributeReflection = $attributeReflections[0] ?? null;
163154

164-
return $this->_attributeCache[$constantName] = $attributeReflection?->newInstance();
155+
return $attributeReflection?->newInstance();
165156
}
166157

167158
private function initData(): void

src/Transfer/TransferAdapterTrait.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ public function toArray(): array
7171

7272
$value instanceof ArrayObject => $value->getArrayCopy(),
7373

74-
$value instanceof DateTimeInterface => $value->format(static::DATE_TIME_FORMAT),
74+
$value instanceof BackedEnum => $value->value,
7575

76-
$value instanceof Number => (string)$value,
76+
$value instanceof DateTimeInterface => $value->format(static::DATE_TIME_FORMAT),
7777

7878
$value instanceof stdClass => (array)$value,
7979

80-
$value instanceof BackedEnum => $value->value,
80+
$this->isBcMathLoaded() && $value instanceof Number => (string)$value,
8181

8282
default => $value,
8383
};
@@ -119,35 +119,35 @@ public function fromArray(array $data): static
119119
$isStringOrInt = $isString || is_int($value);
120120

121121
$this->$name = match (true) {
122-
is_subclass_of($type, AbstractTransfer::class) && $isArray
122+
$isArray && is_subclass_of($type, AbstractTransfer::class)
123123
=> new $type($value),
124124

125-
is_subclass_of($type, TransferInterface::class) && $isArray
125+
$isArray && is_subclass_of($type, TransferInterface::class)
126126
// @phpstan-ignore argument.type
127127
=> new $type()->fromArray($value),
128128

129-
$type === ArrayObject::class && $isArray
129+
$isArray && $type === ArrayObject::class
130130
// @phpstan-ignore argument.type
131131
=> new ArrayObject($value),
132132

133-
$type === DateTime::class && $isString
133+
$isStringOrInt && is_subclass_of($type, BackedEnum::class)
134134
// @phpstan-ignore argument.type
135-
=> new DateTime($value),
135+
=> $type::tryFrom($value),
136136

137-
$type === DateTimeImmutable::class && $isString
137+
$isString && $type === DateTime::class
138138
// @phpstan-ignore argument.type
139-
=> new DateTimeImmutable($value),
139+
=> new DateTime($value),
140140

141-
$type === Number::class && $isStringOrInt
141+
$isString && $type === DateTimeImmutable::class
142142
// @phpstan-ignore argument.type
143-
=> new Number($value),
143+
=> new DateTimeImmutable($value),
144144

145-
$type === stdClass::class && $isArray
145+
$isArray && $type === stdClass::class
146146
=> (object)$value,
147147

148-
is_subclass_of($type, BackedEnum::class) && $isStringOrInt
148+
$isStringOrInt && $this->isBcMathLoaded() && $type === Number::class
149149
// @phpstan-ignore argument.type
150-
=> $type::tryFrom($value),
150+
=> new Number($value),
151151

152152
default => $value,
153153
};
@@ -187,4 +187,9 @@ private function getPublicProperties(): array
187187

188188
return $this->_propertyCache = $reflection->getProperties(ReflectionProperty::IS_PUBLIC);
189189
}
190+
191+
private function isBcMathLoaded(): bool
192+
{
193+
return extension_loaded('bcmath');
194+
}
190195
}

src/TransferGenerator/Definition/Validator/Property/NumberTypePropertyValidator.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,24 @@
1616
private const string INVALID_NUMBER_TYPE_ERROR_MESSAGE_TEMPLATE
1717
= 'Property "%s" type "%s" is not a BcMath\Number.';
1818

19+
private const string EXTENSION_IS_NOT_LOADED_ERROR
20+
= 'PHP extension BCMath was not loaded. Please install and load extension';
21+
1922
public function isApplicable(DefinitionPropertyTransfer $propertyTransfer): bool
2023
{
2124
return $propertyTransfer->numberType !== null;
2225
}
2326

2427
public function validate(DefinitionPropertyTransfer $propertyTransfer): ValidatorMessageTransfer
28+
{
29+
if (!$this->isBcMathLoaded()) {
30+
return $this->createErrorMessageTransfer(self::EXTENSION_IS_NOT_LOADED_ERROR);
31+
}
32+
33+
return $this->validateType($propertyTransfer);
34+
}
35+
36+
private function validateType(DefinitionPropertyTransfer $propertyTransfer): ValidatorMessageTransfer
2537
{
2638
$numberClassName = $propertyTransfer->numberType?->namespace?->withoutAlias ?: '';
2739

@@ -42,4 +54,9 @@ private function getErrorMessage(DefinitionPropertyTransfer $propertyTransfer):
4254
$propertyTransfer->numberType?->namespace->withoutAlias ?? '',
4355
);
4456
}
57+
58+
private function isBcMathLoaded(): bool
59+
{
60+
return extension_loaded('bcmath');
61+
}
4562
}

src/TransferGenerator/Definition/Validator/Property/ReservedNamePropertyValidator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
private const array RESERVED_PROPERTIES = [
1818
'_data',
19-
'_attributeCache',
2019
];
2120

2221
public function isApplicable(DefinitionPropertyTransfer $propertyTransfer): true

0 commit comments

Comments
 (0)