Jump to content

How to validate an object?


Recommended Posts

I have an existing MainObject which contains various sub-objects.  CategoriesCollection, SeriesCollection, and PointsCollection are basically the same and are extended from parent class Collection which implements ArrayAccess, and similarly CategoryNode, SeriesNode, and PointNode are basically the same and are extended from Node.  The main object is created by injecting the various sub-objects (i.e. new MainObject(new CategoryCollection(new CategoryNode()...); )

mainObject(NotionCommotion\App\Entities\MainObject)#229 (16) {
     ["id":protected]=>
     int(230)
     ["name":protected]=>
     string(4) "Test"
     ["categories":protected]=>
     object(NotionCommotion\App\Entities\CategoriesCollection)#228 (3) {
       ["container":protected]=>
       array(4) {
         [0]=>
         object(NotionCommotion\App\Entities\CategoryNode)#277 (3) {
           ["id":protected]=>
           int(132)
           ["name":protected]=>
           string(4) "test"
         }
         [1]=>....
         [2]=>....
         [3]=>....
       }
     }
     ["series":protected]=>
     object(NotionCommotion\App\Entities\SeriesCollection)#215 (3) {
       ["container":protected]=>
       array(4) {
         [0]=>
         object(NotionCommotion\App\Entities\SeriesNode)#268 (4) {
           ["id":protected]=>
           int(110)
           ["name":protected]=>
           string(4) "Test"
           ["data":protected]=>
           object(NotionCommotion\App\Entities\PointsCollection)#230 (3) {
             ["container":protected]=>
             array(4) {
               [0]=>
               object(NotionCommotion\App\Entities\PointNode)#239 (4) {
                 ["id":protected]=>
                 int(248)
                 ["name":protected]=>
                 string(4) "tttt"
                 ["units":protected]=>
                 string(1) "F"
               }
               [1]=>....
               [2]=>....
               [3]=>....
             }
         }
         [1]=>
         [2]=>
         [3]=>
         }
       }
     }
   }


I also have a stdClass object that is a result of a user provided JSON string.  It "should" have a similar structure regarding property types and quantities in the main object, quantity of items in the collections, and property types (i.e. integer, string, etc) in the nodes.  I need to verify that this stdClass is valid regarding above described structure and if so update the main object and its sub-object properties with the user provided values from the stdClass object.

The main object's structure as I showed it above is just one possible structure, and it could be different (i.e. different string/integer properties, no categories collection object, etc) and if so, the user provided stdClass object will need to be different to be considered valid.

I am struggling how to keep it flexible and simple.  High level, how would you recommend doing so?

I am thinking I need to add some update() method to the MainObject class as well as CategoriesCollection, SeriesCollection, PointsCollection, CategoryNode, SeriesNode, and PointNode classes.  Then inject some other object or maybe an anonymous function when creating the main and sub-objects.

This is uncharted territory for me, and I would appreciate any guidance.

Thank you

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.