Coder Social home page Coder Social logo

Direct Property Access about dto HOT 2 CLOSED

fireproofsocks avatar fireproofsocks commented on August 12, 2024
Direct Property Access

from dto.

Comments (2)

johannesschobel avatar johannesschobel commented on August 12, 2024

Update:

My best so far, is to override the __get(...) method from your DTO like this:

public function __get($name)
{
    $field = parent::__get($name);
    $type = $field->getStorageType();

    $value = call_user_func([$field, 'to' . Str::ucfirst($type)]);

    return $value;
}

What do you think about this?

from dto.

fireproofsocks avatar fireproofsocks commented on August 12, 2024

Sorry -- I just saw this (my Github notifications went AWOL). Oiy... no hacks like that should be required.
You can access object properties using arrow notation or using the "get" method. For example:

class GenericDTO extends \Dto\Dto
{
    protected $schema = [
        'type' => 'object',
        'properties' => [
            'additionalProperties' => true,
        ],
    ];
}

And then you may instantiate it as such:

$dto = new GenericDTO(['foo' => 'bar', 'some' => ['deep' => ['nested' => ['data']]]]);

You can retrieve an object attribute using arrow notation, or the get() method

print $dto->foo; // bar
print $dto->foo->toScalar(); // bar
print $dto->get('foo'); // bar

The trick is that each node in the DTO is also a DTO. PHP objects implicitly have a __toString method, so PHP will figure out how to handle something like $x = "The foo is " . $dto->foo; because from the context, it is clear that a string value is required. PHP doesn't have a __toInteger or __toBoolean etc. methods, so sometimes you need to really get the scalar value out of your DTO, so that's what the toScalar() method is for.

You can access the other attributes by name as well, but remember: you are always getting another DTO object in return.

So this returns a huge result with lots of recursion:

print_r($dto->some); // !!! big object with lots of recursion

But using the toArray() method gives you what you probably want:

Array
(
    [deep] => Array
        (
            [nested] => Array
                (
                    [0] => data
                )

        )

)

Similarly, toObject formats the output as an object (think of these as "views" since PHP is a bit ambiguous with its associative arrays and object syntax):

stdClass Object
(
    [deep] => stdClass Object
        (
            [nested] => stdClass Object
                (
                    [0] => data
                )

        )

)

Finally, you can access deeper items in your structure by using the arrow notation (for objects) and bracket notation for accessing elements from an array.

print $dto->some->deep->nested[0]; // prints "data"

Hope that makes things more clear. There are some examples on https://github.com/fireproofsocks/dto/wiki/GettingStarted to help get you started.

from dto.

Related Issues (17)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.