Coder Social home page Coder Social logo

dto's People

Contributors

fireproofsocks avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dto's Issues

Raise Exceptions if DTO object is instantiated with disallowed settings

  • template uses "." as an array key (conflicts with meta definitions)
  • template array keys begin with dots (would cause problems for meta definitions)
  • the "null problem": the data template uses a default value of null, but no meta data is included to disambiguate whether the location should store single or composite data types.

Support for limiting array size

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.

Support for relative paths in JSON schemas

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.

Error on property chaining

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

Why I get this message?

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

Exception on PHP classnames used for $ref values

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!

Support for Loose/Strict

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.

Direct Property Access

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..

Implement offsetUnset

Unsetting a key should restore the default value for that location (if that location is defined), or the command should simply be ignored.

PHP 7.2: ERROR: Function create_function() is deprecated

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.

Allow mutators to raise Exceptions in order to skip setting a value

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.

Support for regular expressions in key names

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.

Error on unserialize serialized dto object

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));
}

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.