|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +require_once __DIR__ . '/../vendor/autoload.php'; |
| 6 | +require_once __DIR__ . '/UserProfile.php'; |
| 7 | +require_once __DIR__ . '/BlogPost.php'; |
| 8 | +require_once __DIR__ . '/BlogController.php'; |
| 9 | + |
| 10 | +use Ray\Di\AbstractModule; |
| 11 | +use Ray\Di\Injector; |
| 12 | +use Ray\InputQuery\Demo\BlogController; |
| 13 | +use Ray\InputQuery\Demo\BlogPost; |
| 14 | +use Ray\InputQuery\Demo\UserProfile; |
| 15 | +use Ray\InputQuery\InputQuery; |
| 16 | + |
| 17 | +echo "=== Ray.InputQuery Demo ===\n\n"; |
| 18 | + |
| 19 | +// Setup dependency injection |
| 20 | +$injector = new Injector(new class extends AbstractModule { |
| 21 | + protected function configure(): void |
| 22 | + { |
| 23 | + $this->bind()->annotatedWith('app.version')->toInstance('1.0.0'); |
| 24 | + } |
| 25 | +}); |
| 26 | + |
| 27 | +$inputQuery = new InputQuery($injector); |
| 28 | + |
| 29 | +echo "1. Simple User Profile Creation\n"; |
| 30 | +echo "================================\n"; |
| 31 | + |
| 32 | +// Simulate form data for user profile |
| 33 | +$userFormData = [ |
| 34 | + 'name' => 'John Doe', |
| 35 | + 'email' => 'john@example.com', |
| 36 | + 'age' => '30', |
| 37 | + 'bio' => 'Full-stack developer passionate about clean code', |
| 38 | + 'isPublic' => '1' |
| 39 | +]; |
| 40 | + |
| 41 | +$userProfile = $inputQuery->create(UserProfile::class, $userFormData); |
| 42 | +echo $userProfile->getDisplayInfo() . "\n\n"; |
| 43 | + |
| 44 | +echo "2. Nested Object Creation (Blog Post with Author)\n"; |
| 45 | +echo "=================================================\n"; |
| 46 | + |
| 47 | +// Simulate form data with nested author information |
| 48 | +$blogFormData = [ |
| 49 | + 'title' => 'Understanding Ray.InputQuery', |
| 50 | + 'content' => 'Ray.InputQuery is a powerful library that transforms flat query data into type-safe PHP objects. It provides seamless integration with dependency injection and supports complex nested object creation...', |
| 51 | + 'category' => 'Technology', |
| 52 | + 'published' => '1', |
| 53 | + 'authorName' => 'Jane Smith', |
| 54 | + 'authorEmail' => 'jane@example.com', |
| 55 | + 'authorId' => 'user123' |
| 56 | +]; |
| 57 | + |
| 58 | +$blogPost = $inputQuery->create(BlogPost::class, $blogFormData); |
| 59 | +echo $blogPost->getPostSummary() . "\n\n"; |
| 60 | + |
| 61 | +echo "3. Controller Method Arguments with DI\n"; |
| 62 | +echo "======================================\n"; |
| 63 | + |
| 64 | +// Test controller method with mixed Input and DI parameters |
| 65 | +$controller = new BlogController(); |
| 66 | + |
| 67 | +// Blog post creation |
| 68 | +$createMethod = new ReflectionMethod(BlogController::class, 'createPost'); |
| 69 | +$createArgs = $inputQuery->getArguments($createMethod, $blogFormData); |
| 70 | +$result1 = $createMethod->invokeArgs($controller, $createArgs); |
| 71 | +echo $result1 . "\n\n"; |
| 72 | + |
| 73 | +// Profile update |
| 74 | +$updateMethod = new ReflectionMethod(BlogController::class, 'updateProfile'); |
| 75 | +$updateArgs = $inputQuery->getArguments($updateMethod, $userFormData); |
| 76 | +$result2 = $updateMethod->invokeArgs($controller, $updateArgs); |
| 77 | +echo $result2 . "\n\n"; |
| 78 | + |
| 79 | +echo "4. Scalar Type Conversions\n"; |
| 80 | +echo "==========================\n"; |
| 81 | + |
| 82 | +// Demonstrate automatic type conversion |
| 83 | +$scalarData = [ |
| 84 | + 'name' => 'Test User', |
| 85 | + 'email' => 'test@example.com', |
| 86 | + 'age' => '25', // string -> int |
| 87 | + 'isPublic' => 'true' // string -> bool |
| 88 | +]; |
| 89 | + |
| 90 | +$profile = $inputQuery->create(UserProfile::class, $scalarData); |
| 91 | +echo "Converted types:\n"; |
| 92 | +echo "- age (string '25' -> int): " . var_export($profile->age, true) . " (" . gettype($profile->age) . ")\n"; |
| 93 | +echo "- isPublic (string 'true' -> bool): " . var_export($profile->isPublic, true) . " (" . gettype($profile->isPublic) . ")\n\n"; |
| 94 | + |
| 95 | +echo "5. Default Values\n"; |
| 96 | +echo "=================\n"; |
| 97 | + |
| 98 | +// Minimal data - other fields will use defaults |
| 99 | +$minimalData = [ |
| 100 | + 'name' => 'Minimal User', |
| 101 | + 'email' => 'minimal@example.com' |
| 102 | +]; |
| 103 | + |
| 104 | +$minimalProfile = $inputQuery->create(UserProfile::class, $minimalData); |
| 105 | +echo "Profile with defaults:\n"; |
| 106 | +echo $minimalProfile->getDisplayInfo() . "\n\n"; |
| 107 | + |
| 108 | +echo "6. Key Normalization (snake_case to camelCase)\n"; |
| 109 | +echo "===============================================\n"; |
| 110 | + |
| 111 | +// Form data with snake_case keys |
| 112 | +$snakeCaseData = [ |
| 113 | + 'title' => 'Snake Case Test', |
| 114 | + 'content' => 'Testing snake_case to camelCase conversion', |
| 115 | + 'author_name' => 'Snake Author', // author_name -> authorName |
| 116 | + 'author_email' => 'snake@example.com', // author_email -> authorEmail |
| 117 | + 'author_id' => 'snake123' // author_id -> authorId |
| 118 | +]; |
| 119 | + |
| 120 | +$snakeCasePost = $inputQuery->create(BlogPost::class, $snakeCaseData); |
| 121 | +echo "Successfully converted snake_case keys:\n"; |
| 122 | +echo $snakeCasePost->getPostSummary() . "\n\n"; |
| 123 | + |
| 124 | +echo "Demo completed successfully! 🎉\n"; |
0 commit comments