saleem / data-transfer-object
一个没有依赖和命令支持的简单数据传输对象
1.4
2024-08-28 06:50 UTC
Requires
- php: ^8.1
README
此包简化了处理数据传输对象的过程。它允许轻松访问单个属性并将数据转换为JSON或数组。
安装
要使用此包,请按照以下步骤操作
-
通过Composer安装
composer require saleem/data-transfer-object
-
对于配置文件发布(这将在配置下的"data-transfer-object.php"中发布文件),您可以从那里更改命名空间
php artisan vendor:publish --tag=config-file
创建新的DTO类
- 使用命令
php artisan make:dto PersonalInfo
- 不使用命令创建并从BaseDto扩展
<?php use Saleem\DataTransferObject\BaseDto; class PersonalInfoDto extends BaseDto { public ?string $firstName = null; public ?string $lastName = null; public ?string $email = null; }
这将使用命令在app/Common/DTO中创建一个新的DTO类(使用命令创建)
namespace App\Common\DTO; use Saleem\DataTransferObject\BaseDto; class PersonalInfoDto extends BaseDto { public ?string $firstName = null; public ?string $lastName = null; public ?string $email = null; } // Creating an instance of PersonalInfoDto $personalInfo = PersonalInfoDto::build([ 'firstName' => 'John', 'lastName' => 'Doe', 'email' => 'john@example.com' ]); // Accessing properties echo $personalInfo->firstName; // Output: John echo $personalInfo->lastName; // Output: Doe echo $personalInfo->email; // Output: john@example.com // Converting to JSON $jsonData = $personalInfo->json(); echo $jsonData; // Output: {"firstName":"John","lastName":"Doe","email":"john@example.com"} // Getting data as an array $dataArray = $personalInfo->data(); print_r($dataArray); // Output: // Array ( // [firstName] => John // [lastName] => Doe // [email] => john@example.com // ) //Storing data in Table $dataArray = $personalInfo->data(); PersnonalInfo::create($dataArray); // Excluding specific keys $filteredData = $personalInfo->without('email'); print_r($filteredData); // Output: // Array ( // [firstName] => John // [lastName] => Doe // ) // Including specific keys $includedData = $personalInfo->only(['firstName', 'email']); print_r($includedData); // Output: // Array ( // [firstName] => John // [email] => john@example.com // )
更改键并分配新键
$data = CreateCarrierDTO::build([ 'auth0_user_id' => '21sqwaW', 'some_id' => 1, 'email' => 'email@gmail.com', 'name' => 'name', 'mc_number' => 'qweqwe', 'dot_number' => 1, 'some_status' => 'some_status', ]); $keysPassedAsArray = [ 'auth0_user_id' => 'NewAuthId', 'some_id' => 'NewSomeId', ]; $passedAsArray = $data->changeKeys($keysPassedAsArray); // array(7) { // ["NewAuthId"]=> Changed // string(7) "21sqwaW" // ["NewSomeId"]=> Changed // int(1) // ["email"]=> // string(17) "email@gmail.com" // ["name"]=> // string(4) "name" // ["mc_number"]=> // string(6) "qweqwe" // ["dot_number"]=> // int(1) // ["some_status"]=> // string(12) "some_status" // } $passingSingleKey = $data->changeKeys('auth0_user_id', 'NewAuthId'); // array(7) { // ["NewAuthId"]=> Changed // string(7) "21sqwaW" // ["some_id"]=> // int(1) // ["email"]=> // string(17) "email@gmail.com" // ["name"]=> // string(4) "name" // ["mc_number"]=> // string(6) "qweqwe" // ["dot_number"]=> // int(1) // ["some_status"]=> // string(12) "some_status" // }
在单个容器中使用不同的DTO
假设已创建了这些类
class AddressDto extends BaseDto { public ?string $street = null; public ?string $city = null; public ?string $state = null; public ?string $zipCode = null; } class CreditCardDto extends BaseDto { public ?string $cardNumber = null; public ?string $expiryDate = null; public ?string $cardHolderName = null; } class EducationalInfoDto extends BaseDto { public ?string $schoolName = null; public ?string $degree = null; public ?int $graduationYear = null; } class PersonalInfoDto extends BaseDto { public ?string $firstName = null; public ?string $lastName = null; public ?string $email = null; }
在DTO中使用DTO数组
当在较大的DTO类中利用相同DTO的数组时,使用数组类型转换($arrayCasts)将它们转换为相关的DTO类是有益的。
优点
Structured Data: Maintain a consistent structure by ensuring arrays hold instances of specific DTO classes.
Encapsulation: Simplify data access and manipulation by grouping related elements together.
Consistency: Establish a clear and standardized format for improved readability.
Enhanced Functionality: Leverage specific functionalities defined within DTO classes.
Data Validation: Enforce type safety and validation rules for better data integrity.
将数组转换为相关的DTO类增强了可读性,保持了结构,并更好地控制了DTO中的数据管理。
现在在单个容器中使用不同的DTO
class StudentDto extends BaseDto { public ?PersonalInfoDto $personalInfo = null; public array $addresses = []; public ?EducationalInfoDto $educationalInfo = null; public array $creditCards = []; protected static array $arrayCasts = [ 'addresses' => AddressDto::class, 'creditCards' => CreditCardDto::class ]; } $studentData = [ 'personalInfo' => ['firstName' => 'John', 'lastName' => 'Doe', 'email' => 'john@example.com'], 'addresses' => [ ['street' => '123 Main St', 'city' => 'City1', 'state' => 'State1', 'zipCode' => '12345'], ['street' => '456 Second St', 'city' => 'City2', 'state' => 'State2', 'zipCode' => '67890'], ], 'educationalInfo' => ['schoolName' => 'University of Example', 'degree' => 'Computer Science', 'graduationYear' => 2023], 'creditCards' => [ ['cardNumber' => '1234-5678-9012-3456', 'expiryDate' => '12/25', 'cardHolderName' => 'John Doe'], ['cardNumber' => '9876-5432-1098-7654', 'expiryDate' => '06/24', 'cardHolderName' => 'Jane Doe'], ], ]; $studentDto = StudentDto::build($studentData); // Converting StudentDto to JSON $studentJson = $studentDto->json(); echo $studentJson; // Excluding specific keys from StudentDto $filteredStudent = $studentDto->without(['addresses', 'creditCards']); // Check if $filteredStudent is a BaseDto object if ($filteredStudent instanceof BaseDto) { // If it's a BaseDto object, use the json() method $filteredStudentJson = $filteredStudent->json(); echo $filteredStudentJson; } else { // If it's an array, convert it to JSON directly $filteredStudentJson = json_encode($filteredStudent); echo $filteredStudentJson; } // Including specific keys from StudentDto $includedStudent = $studentDto->only(['personalInfo', 'educationalInfo']); // Check if $filteredStudent is a BaseDto object if ($includedStudent instanceof BaseDto) { // If it's a BaseDto object, use the json() method $includedStudentJson = $includedStudent->json(); echo $includedStudentJson; } else { // If it's an array, convert it to JSON directly $includedStudentJson = json_encode($includedStudent); echo $includedStudentJson; } foreach ($studentDto->addresses as $address) { echo "- {$address->street}, {$address->city}, {$address->state}, {$address->zipCode}\n"; }
在请求中使用(以StudentDto的单容器为例)
JSON有效负载
{
"personalInfo": {
"firstName": "John",
"lastName": "Doe",
"email": "john@example.com"
},
"addresses": [
{
"street": "123 Main St",
"city": "City1",
"state": "State1",
"zipCode": "12345"
},
{
"street": "456 Second St",
"city": "City2",
"state": "State2",
"zipCode": "67890"
}
],
"educationalInfo": {
"schoolName": "University of Example",
"degree": "Computer Science",
"graduationYear": 2023
},
"creditCards": [
{
"cardNumber": "1234-5678-9012-3456",
"expiryDate": "12/25",
"cardHolderName": "John Doe"
},
{
"cardNumber": "9876-5432-1098-7654",
"expiryDate": "06/24",
"cardHolderName": "Jane Doe"
}
]
}
Api.web
Route::post('/test', function(Request $request){ $studentDto = StudentDto::build($request->all()); // Converting StudentDto to JSON $studentJson = $studentDto->json(); echo $studentJson; // Excluding specific keys from StudentDto $filteredStudent = $studentDto->without(['addresses', 'creditCards']); // Check if $filteredStudent is a BaseDto object if ($filteredStudent instanceof BaseDto) { // If it's a BaseDto object, use the json() method $filteredStudentJson = $filteredStudent->json(); echo $filteredStudentJson; } else { // If it's an array, convert it to JSON directly $filteredStudentJson = json_encode($filteredStudent); echo $filteredStudentJson; } // Including specific keys from StudentDto $includedStudent = $studentDto->only(['personalInfo', 'educationalInfo']); // Check if $filteredStudent is a BaseDto object if ($includedStudent instanceof BaseDto) { // If it's a BaseDto object, use the json() method $includedStudentJson = $includedStudent->json(); echo $includedStudentJson; } else { // If it's an array, convert it to JSON directly $includedStudentJson = json_encode($includedStudent); echo $includedStudentJson; } foreach ($studentDto->addresses as $address) { echo "- {$address->street}, {$address->city}, {$address->state}, {$address->zipCode}\n"; } });