Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      Sunshine And March Vibes (2025 Wallpapers Edition)

      May 16, 2025

      The Case For Minimal WordPress Setups: A Contrarian View On Theme Frameworks

      May 16, 2025

      How To Fix Largest Contentful Paint Issues With Subpart Analysis

      May 16, 2025

      How To Prevent WordPress SQL Injection Attacks

      May 16, 2025

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025

      Minecraft licensing robbed us of this controversial NFL schedule release video

      May 16, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      The power of generators

      May 16, 2025
      Recent

      The power of generators

      May 16, 2025

      Simplify Factory Associations with Laravel’s UseFactory Attribute

      May 16, 2025

      This Week in Laravel: React Native, PhpStorm Junie, and more

      May 16, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025
      Recent

      Microsoft has closed its “Experience Center” store in Sydney, Australia — as it ramps up a continued digital growth campaign

      May 16, 2025

      Bing Search APIs to be “decommissioned completely” as Microsoft urges developers to use its Azure agentic AI alternative

      May 16, 2025

      Microsoft might kill the Surface Laptop Studio as production is quietly halted

      May 16, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»Development»Transform Data into Type-safe DTOs with this PHP Package

    Transform Data into Type-safe DTOs with this PHP Package

    November 18, 2024

    Transform Data into Type-safe DTOs with this PHP Package

    This PHP Data Model package provides a lightweight, non-invasive way to hydrate type-safe PHP objects recursively. It uses reflection and PHP attributes to hydrate objects and instantiate them based on type hints:

    class Address
    {
        use ZerotoprodDataModelDataModel;
    
        public string $street;
        public string $city;
    }
    
    class User
    {
        use ZerotoprodDataModelDataModel;
    
        public string $username;
        public Address $address;
    }
    
    $User = User::from([
        'username' => 'John Doe',
        'address' => [
            'street' => '123 Main St',
            'city' => 'Hometown',
        ],
    ]);
    
    echo $User->address->city; // Hometown
    

    Main Features

    • Simplify Object Hydration: No more manual assignment of properties or repetitive boilerplate code. A DataModel automates the process, ensuring your objects are populated correctly based on their type declarations.
    • Ensure Type Safety: Type safety is enforced by PHP itself. With a DataModel, you can trust that your objects contain the expected types without extra validation.
    • Reduce Defensive Programming: Instead of writing defensive code to check and sanitize your data, a DataModel allows you to define how each property should be resolved before the object is hydrated.
    • Flexible Value Resolution: With the #[Describe()] attribute, you control how values are resolved for each property, including transformations, default values, and custom casting. This ensures that your data model behaves exactly as you intend.
    • Non-Invasive Integration: Simply add the DataModel trait to your classes. There’s no need to extend base classes or implement interfaces, keeping your class hierarchy clean.

    This package also includes advanced features like required properties that are enforced via a PHP attribute:

    use ZerotoprodDataModelDescribe;
    
    class User
    {
        use ZerotoprodDataModelDataModel;
    
        #[Describe(['required' => true])]
        public string $username;
    
        public string $email;
    }
    
    User::from(['email' => 'john@example.com']);
    // Throws PropertyRequiredException exception:
    // Property: username is required
    

    Additional related packages also provide helpers, factories, transformers, and more. You can get started with this package by reading the documentation; the source code is also available on GitHub at zero-to-prod/data-model.


    The post Transform Data into Type-safe DTOs with this PHP Package appeared first on Laravel News.

    Join the Laravel Newsletter to get all the latest
    Laravel articles like this directly in your inbox.

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous Articleryangjchandler/laravel-comments
    Next Article Unlock the Power of Transformational Leadership

    Related Posts

    Security

    Nmap 7.96 Launches with Lightning-Fast DNS and 612 Scripts

    May 16, 2025
    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-2305 – Apache Linux Path Traversal Vulnerability

    May 16, 2025
    Leave A Reply Cancel Reply

    Continue Reading

    How AI Agents Store, Forget, and Retrieve? A Fresh Look at Memory Operations for the Next-Gen LLMs

    Machine Learning

    Microsoft wants you and your business to ditch Office

    News & Updates

    AutoSculpt: A Pattern-based Automated Pruning Framework Designed to Enhance Efficiency and Accuracy by Leveraging Graph Learning and Deep Reinforcement Learning

    Development

    Error: Unable to find window while the window is already displayed in LDTP

    Development

    Highlights

    Development

    Big Data vs Data Warehouse

    November 19, 2024

    The rapid expansion of data in today’s era has brought with it both possibilities and…

    FM20.dll is Missing or Not Correctly Registered: 7 Quick Fixes

    January 28, 2025

    Takedown Services: A CFO’s New Line of Defense

    August 20, 2024

    Typecasting and Viewport Transitions in CSS

    February 20, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.