Comments (2)
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.
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)
- Support for prepend
- Wrap 3rd-party exceptions in Dto namespaced Exceptions HOT 1
- Error on property chaining HOT 3
- Exception on PHP classnames used for $ref values HOT 3
- string maxLength not undersand UTF8
- Error on unserialize serialized dto object
- PHP 7.2: ERROR: Function create_function() is deprecated HOT 4
- Why I get this message?
- Support for regular expressions in key names HOT 1
- Support for Array Size, Pop and Shift
- Allow mutators to raise Exceptions in order to skip setting a value HOT 1
- Raise Exceptions if DTO object is instantiated with disallowed settings HOT 1
- Support for limiting array size HOT 1
- Support for Loose/Strict HOT 1
- Implement offsetUnset HOT 1
- Support for relative paths in JSON schemas HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dto.