fireproofsocks / dto Goto Github PK
View Code? Open in Web Editor NEWData Transfer Object (DTO) in PHP using JSON Schema
Data Transfer Object (DTO) in PHP using JSON Schema
For array types, it would be nice as a developer to have a simple way to define an array's maximum size.
TBD: behavior for when attempting to append or prepend elements onto a full array.
When using the $ref
keyword and pointing to a .json
file (e.g. a local file path), we should be able to specify relative file paths in a couple "expected" places.
E.g. in a PersonDto:
protected $schema = [
'$ref' => '../../person.json'
];
Or inside person.json
:
{
"type": "object",
"properties": {
"name": {"type": "string"},
"phone": {"$ref": "../partials/phone.json"}
}
}
In the first case, the $ref
should resolve relative to the location of the PersonDto.php
class. In the second, the ../partials/phone.json
should resolve relative to the location of the person.json
file.
Probably the code should be refactored to use the league/json-reference
package.
On this page https://github.com/fireproofsocks/dto/wiki/Ambiguous this sample don't work:
class BlogPostDto extends \Dto\Dto
{
protected $schema = [
'type' => 'object',
'properties' => [
'title' => ['type' => 'string'],
'content' => ['type' => 'string'],
'custom_fields' => [
'type' => 'object',
'additionalProperties' => ['type' => 'string']
]
]
];
}
$post = new BlogPostDto();
$post->title = 'New Film Released!';
$post->content = 'The story...';
$post->custom_fields->release_date = 'Next Saturday';
When i use this example it throw exception Dto\Exceptions\InvalidKeyException: The key "custom_fields" does not exist in this DTO
.
PHP 7.1.13
Hello! I have a dto class with such schema:
protected $schema = [
'type' => 'object',
'properties' => [
'place_id' => [
'type' => ['integer', 'null'],
],
],
'default' => [
'place_id' => null,
],
];
If I set place_id = 12, I get such error:
"type":"null" allows only null values. Pass a literal null or define your "type" as an array of types.
Why? I want to set this property in integer or in null, but it isn't work.If place_id in schema will be string
instead integer
(ex. - 'place_id' => [ 'type' => ['string', 'null'], ...]) - it is works.
version - 3.2.7
As a developer, I would like to have the ability to perform a prepend operation (like array_unshift) to add elements to the beginning of an array:
http://stackoverflow.com/questions/6875080/php-how-to-array-unshift-on-an-arrayobject
class ExtendedArrayObject extends ArrayObject {
public function prepend($value) {
$array = (array)$this;
array_unshift($array, $value);
$this->exchangeArray($array);
}
}
Hi I am using your great library as part of the Apiato
I have following schemas:
<?php
class School extends \Dto\Dto
{
protected $schema = [
'type' => 'object',
'properties' => [
'location' => [
'$ref' => Location::class
]
]
];
}
class Location extends \Dto\Dto
{
protected $schema = [
'type' => 'object',
'properties' => [...],
'required' => [....]
];
}
So when I initialized $location = new Location([...])
I do get Location instance.
But then trying to assign Location to School as below:
$school = new School([...])
$school->location = $location;
It raised an exception of missing required values for Location
I read through Wiki and couldn't solve this one out. It's great if anyone can give shed me some lights!
Thanks in advanced!
Sometimes you want the "dragnet" functionality: throw anything at the DTO and just keep what sticks (silently fail).
Other times you may want to raise Exceptions when something doesn't map correctly.
One possible solution is to route exceptions to a dedicated method, e.g. handleException
... then in a child class, you could re-throw them for a strict implementation.
Alternatively, maybe set something on the root object.
Hey there,
I just stumbled upon your DTO implementation and i really, really, really love it! That is so easy to use! Really great job on this one!
However, i have one question, you may be able to answer me straight away..
Consider the following example.
I have a GenericDTO
that may be used, if no other one is specified. In order to make it generic (i.e., usable in all kind of situations, i have the following schema:
protected $schema = [
'type' => 'object',
'properties' => [
'additionalProperties' => true,
],
];
Now i create a GenericDTO
with my data to be passed..
// assume, $data is a (deeply nested) array
$dto = new GenericDTO($data);
While this certainly works, how can i now access the data
of an attribute? For example, if i want to get the name
, i would like to call $dto->name
. However, calling this, outputs the object itself - with the "storage" where my data is located in. If i run $dto->name->toScalar()
i get "John Doe". However, toScalar()
is not always possible, as i may access arrays or objects as well (because it is generic).
Do you have an idea how to solve this properly? Or am I missing something crucial here?
Thanks a lot for your awesome work..
Unsetting a key should restore the default value for that location (if that location is defined), or the command should simply be ignored.
Hey fireproofsocks, I just ran into an issue with DTO when using PHP 7.2 and the JsonDecoder
class:
ERROR: Function create_function() is deprecated
The error stems from this method in JsonDecoder
:
/**
* @inheritdoc
*/
public function decodeFile($filepath)
{
// https://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-file-get-contents-function-in-php
set_error_handler(
create_function(
'$severity, $message, $file, $line',
'throw new \Dto\Exceptions\JsonDecodingException($message, $severity);'
)
);
$content = file_get_contents($filepath);
restore_error_handler();
return $this->decodeString($content);
}
After a bit of research, I found this question on SO, so the fix seems to be pretty straightforward.
I created a pull request for this issue, just wanted to properly document it as well.
Thank you so much for the package, it's amazing.
As a developer, if I am using a custom mutator for a field or type, I should be able to raise and Exception within my method to prevent that value from being mapped onto the DTO ArrayObject. Perhaps with a handleException
method, this could allow the user to have control over which exceptions are handled or which are ignored.
More thorough array implementation would be to be able to get elements off the front or back
See also http://www.onlinesmartketer.com/2009/02/07/php-clear-delete-remove-operations-on-arrayobject-class/
maxLength => 2
Input = '็็'
Length of string failed "maxLength"
As a developer, I would like support in my meta definitions to be able to specify a simple regular expression that could be used to enforce key names (in the isValidTargetLocation()
method).
Perhaps:
$meta = [
'.myhash' => [
'ambiguous' => true,
'regex' => '/A-Z/';
]
];
Better name maybe "key_regex"? Or key->regex ? See the "patternProperties" http://json-schema.org/example2.html and "additionalProperties"
This feature would only make sense for hashes with ambiguous keys, unless you were sort of deferring validation of an object against an arbitrary schema, but we have objects and schemas in one, so it's not exactly the same problem.
Hi!
I have some problems with unserialise of dto objects. It throw 'UnexpectedValueException with message 'Error at offset 0 of XX bytes''
Sample:
$ser = serialize(new Dto\Dto);
$obj = unserialize($ser);
After examining the code in the file Dto.php, I found the reason. The Dot class has an implementation of the serialize method, in which the JSON representation of the object is returned. But I did not find the method of unserialize. An example of the non-existent method:
final public function unserialize($serialized) {
return static::__construct(json_decode($serialized, true));
}
Particularly in the Json Decoding stuff.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.