Skip to content

Commit 3b435f8

Browse files
authored
feat: remove type check (#4)
1 parent 522f2ee commit 3b435f8

File tree

9 files changed

+374
-704
lines changed

9 files changed

+374
-704
lines changed

README.md

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This _kind of_ works, but with several drawbacks:
3232
* One would always have to refer to the documentation (if one exists) for the "shape" of the array. This reduces both reusability and productivity.
3333
* Static code analysis and IDE auto-completion support are greatly hindered.
3434

35-
Now imagine instead of using an arbitrary array, we use an object with strongly-typed properties:
35+
Now imagine instead of using an arbitrary array, we use an object with typed properties:
3636

3737
```php
3838
// UserCreationData.php
@@ -68,6 +68,10 @@ composer require eve/dto
6868

6969
This package requires PHP ≥7.4.
7070

71+
### Migrate from v1.x
72+
73+
v1.x versions of this library include a strict type check—for example, assigning a string to a boolean property will throw an error. Though certainly useful, this feature doesn't belong in the scope of a DTO and has been removed from v2. You're encouraged to use a static analysis tool like [PHPStan](https://github.com/phpstan/phpstan) or [Psalm](https://psalm.dev) for the task instead.
74+
7175
## Usage
7276

7377
### Basic Usage
@@ -113,11 +117,10 @@ $data = UserCreationData::make()
113117
]);
114118
```
115119

116-
If any of the passed properties doesn't exist in the class definition or if the types don't match (notice that a non-type public property e.g., `public $whatever` accepts all types), an exception will be thrown:
120+
If any of the passed properties doesn't exist in the class definition, an exception will be thrown:
117121

118122
```php
119123
UserCreationData::make(['nope' => 'bar']); // throws "Public property $nope does not exist in class UserCreationData"
120-
UserCreationData::make(['email' => new Foo()]); // throws 'UserCreationData::$email must be of type string, received a value of type Foo.',
121124
```
122125

123126
Then we can call the `toArray()` method to transform the object into an associative array:
@@ -126,7 +129,7 @@ Then we can call the `toArray()` method to transform the object into an associat
126129
$arr = $data->toArray(); // ['email' => 'alice@company.tld', 'password' => 'SoSecureWow', 'age' => 30]
127130
```
128131

129-
Note that non-set properties will NOT be returned in the output array:
132+
Note that non-set properties will NOT be included in the output array:
130133

131134
```php
132135
$data = UserCreationData::make();
@@ -139,26 +142,6 @@ $arr = $data->toArray(); // ['email' => 'alice@company.tld']
139142

140143
This is especially handy e.g., if you have a method to patch a database record, as it allows the operation to be totally flexible—you can patch all properties or only a subset of them.
141144

142-
### Type Annotations with DocBlock
143-
144-
Instead of declaring your properties with built-in types, you can use type annotations with DockBlock. This is particularly useful if the property accepts multiple types—in fact, it's the only way to declare such in PHP<8. All type restrictions will be respected as normal:
145-
146-
```php
147-
use Carbon\Carbon;
148-
149-
class NewOrderData extends \Eve\DTO\DataTransferObject
150-
{
151-
/**
152-
* @var string|Carbon
153-
*/
154-
public $order_date;
155-
}
156-
157-
NewOrderData::make(['order_date' => '2021-01-02 12:34:56']); // works
158-
NewOrderData::make(['order_date' => Carbon::now()]); // works
159-
NewOrderData::make(['order_date' => false]); // throws
160-
```
161-
162145
### Nested DTOs
163146

164147
Nested DTOs will be transformed into their corresponding arrays:
@@ -223,10 +206,10 @@ $data->toArray(); // ['email' => 'alice@company.tld', 'password' => 'SoSecureWow
223206

224207
## Differences from spatie/data-transfer-object
225208

226-
eve/dto is inspired by and shares some similarities with [spatie/data-transfer-object](https://github.com/spatie/data-transfer-object) but the two packages have certain differences, the most significant of which are as follows:
209+
Though eve/dto is inspired by and shares some similarities with [spatie/data-transfer-object](https://github.com/spatie/data-transfer-object), the two packages have certain differences, the most significant of which are as follows:
227210

228-
* spatie/data-transfer-object requires all not-null properties to be supplied right from instantiation. This behavior is not always feasible or desirable (refer to the data patching example above). eve/dto opts for a much more forgiving approach, which allows a DTO to be created with any subset of properties.
229-
* spatie/data-transfer-object uses a custom RegExp to parse the docblocks. This approach is prone to bugs and has some limitations. For example, the type-hinted class must be an FQCN (Fully Qualified Class Name) i.e. `\App\Models\Author` instead of `Author`. Meanwhile, eve/dto uses the official [ReflectionDocBlock](https://github.com/phpDocumentor/ReflectionDocBlock) and [TypeResolver](https://github.com/phpDocumentor/TypeResolver) packages from phpDocumentor to deal with docblocks and therefore doesn't have these issues.
211+
* spatie/data-transfer-object requires all not-null properties to be supplied right from instantiation. This behavior is not always feasible or desirable (refer to the data patching example above). eve/dto opts for a much more forgiving approach, which allows a DTO to be created with any subset of properties.
212+
* spatie/data-transfer-object can't detect or prevent you from assigning a non-existent property directly (e.g., `$userData->non_existent = 'foo'`), which is something eve/dto does to help ensure your object's integrity.
230213
* spatie/data-transfer-object implements such features as "Data Transfer Object Collection" and "Flexible Data Transfer Objects." To keep things simple and concise, eve/dto doesn't have these implementations.
231214

232215
## License

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
"description": "Simplistic, flexible Data Transfer Object library",
44
"type": "library",
55
"require": {
6-
"php": "^7.4|~8",
7-
"phpdocumentor/reflection-docblock": "^5.2",
8-
"phpdocumentor/type-resolver": "^1.4"
6+
"php": "^7.4|~8"
97
},
108
"require-dev": {
119
"phpunit/phpunit": "^9.5",

0 commit comments

Comments
 (0)