1

I am trying to store the user's response for a detailed fitness report questionnaire in MongoDB. Once the form is submitted I would parse the form data and create an array/object which has a predefined structure.

I was thinking of creating a class with the whole object hierarchy with values set to empty string ''. The response I want to store looks like this:

{
    "_basic": {
        "_response_type": "text",
        "_response": {
            "name": "eNeMetchH",
            "gender": "male",
            "age": 20,
            "contact": "9876543210",
            "email": "[email protected]",
            "dob": "1997-03-20",
            "package": {
                "id": "1001",
                "name": "Alpha Monthly",
                "start_date": "2017-01-01",
                "end_date": "2017-01-31"
            }
        }
    },
    "_parq": {
        "footnotes": "If yes to one or more questions, please talk to your doctor before you start becoming more physically active or before you have a fitness appraisal. Tell your doctor about the PAR-Q and which questions you have answered yes.",
        "response_type": "radio",
        "response": [
            {
                "question": "Has your doctor ever said you have a heart condition and you should only do physical activity that is described by doctor?",
                "response": "NO"
            },
            {
                "question": "Do you feel pain in your chest when you do physical activity?",
                "response": "NO"
            },
            {
                "question": "In the past month, have you had chest pain when you were not doing physical activity?",
                "response": "NO"
            },
            {
                "question": "Do you lose your balance because of dizziness or do you ever lose consciousness?",
                "response": "NO"
            },
            {
                "question": "Do you have a bone or joint problem that could not be made worse by a change in your physical activity?",
                "response": "NO"
            },
            {
                "question": "Is your doctor currently prescribing drugs for your BP & Heart conditions?",
                "response": "NO"
            },
            {
                "question": "Do you know any other reason why you should not do physical activity?",
                "response": "NO"
            }
        ]
    },
    "_conditioning_level": "_intermediate_1_3",
    "_occupation": "_professional",
    "_primary_goal": "_weight_gain",
    "_secondary_goal": "_muscular_strength",
    "_workout_intensity": "_mild",
    "_availability": {
        "_days_per_week": "6",
        "_min_per_day": "90"
    },
    "_sleep": "_insomnia",
    "_physical_activity": "_sedentary",
    "_stress_level": "_mild",
    "_medical_history": [
        "_knee_pain",
        "_hypertension",
        "_high_cholesterol"
    ],
    "_alcohol": "_alcoholic",
    "_smoking": "YES",
    "_tobacco": "NO",
    "_shoe_analysis": {
        "_toe_box": "_normal",
        "_torsion": "_normal",
        "_heel_support": "_abnormal",
        "_arch": "_normal"
    },
    "_postural_analysis": {
        "_head_neck": "Blah blah",
        "_shoulder": "Blah blah",
        "_thoracic_spine": "Blah blah",
        "_lumbar_spine": "Blah blah",
        "_knee": "Blah blah",
        "_feet": "Blah blah"
    },
    "_anthropometry": {
        "_bp": "160/90 mm/Hg",
        "_resting_heart_rate": "90 bpm",
        "_weight": "82",
        "_height": "183",
        "_bmi": "20"
    },
    "_functional_movement": {
        "_ssn": "Blah blah",
        "_height": "183",
        "_weight": "82",
        "_primary_score": "Dono",
        "_primary_position": "Dono",
        "_hand_leg_dominance": "Dono",
        "_previous_test_score": "Dono",
        "tests": {
            "_deep_squat": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_hurdle_step": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_inline_lunge": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_shoulder_mobility": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_impingement_clearing_test": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_active_straight_leg_raise": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_trunk_push_up": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_press_up_test": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_rotary_stability": {
                "L": {
                    "_raw_score": "30",
                    "_final_score": "40"
                },
                "R": {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            },
            "_posterior_rocking_test": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ],
            "_total": [
                {
                    "_raw_score": "30",
                    "_final_score": "40"
                }
            ]
        }
    },
    "_fitness_test": {
        "_aerobic_endurance": {
            "_distance_covered": "2 km",
            "_pre_heart_rate": "72",
            "_post_heart_rate": "95",
            "_vo2_max": "90",
            "_mets": "30"
        },
        "_muscular_strength": {
            "_upper_body": "6",
            "_lower_body": "7"
        },
        "_muscular_endurance": {
            "_ab_crunches": "6",
            "_free_squats": "7"
        },
        "_flexibility": {
            "_sit_reach": "5",
            "_cobra_stretch": "6"
        },
        "_proprioception": {
            "_lt_side": "10",
            "_rt_side": "10"
        },
        "_core_strength": {
            "_iron_man": "55"
        }
    },
    "_assessment_summary": {
        "_age": "20",
        "_sex": "_male",
        "_conditioning_level": "_intermediate_1_3",
        "_goal": "_weight_gain",
        "_occupation": "_professional",
        "_stress_level": "_mild",
        "_health_problem": "None",
        "_test_result": "Need fat loss and Increment in lung capacity",
        "_preferences": "None whatsoever"
    },
    "_remarks": "Blah blah blah"
}

I need to create a template with the above structure intact and empty values. Adding anymore keys or removing keys should not be allowed.

What is the best approach for this?

2
  • 2
    you should take advantage of the object oriented programming in PHP to create a class with the properties you need, this is the only way to be sure that no field is added or removed Commented Jul 27, 2017 at 9:56
  • @jiboulex How would you go about it? Could you please show it for a simpler structure. Commented Jul 27, 2017 at 10:06

1 Answer 1

1

If you keep the variable as protected, then it can be modified only by member functions and cannot be accessed directly. The __set function will prevent addition of new variable.

create a class

class Foo{
// if you keep the variable as protected, then it can be modified only
// by member functions so noone will be able to remove the variable. 
protected $v1;

// this set function will prevent addition of new variable.
public function __set($name, $value) {
    throw new Exception("Cannot add new property \$$name to instance of " .   __CLASS__);
}

}

Example for a property such as occupation, you have to define it as

protected $occupation = "professional";
Sign up to request clarification or add additional context in comments.

2 Comments

How do I define the structure of $v1?
@eNeMetcH, I have edited my ans to answer your comment(if thats what you were asking for)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.