0

I am trying to figure out the best way to accomplish this so feel free to criticize my thought process.

My tool has different locales and each locale can have different settings for the tool. For example, Arizona could have the top 5 stats turned on where as California does not.

I pull all of the settings based on the locale and have an array of the data:

    SimpleXMLElement Object
(
    [settings] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [settingName] => Home Page Stats
                    [settingDescription] => Show the Top 5 Teammate / Teamleader stats?
                    [requireValue] => 0
                    [localeID] => 14
                    [status] => 1
                    [value] => SimpleXMLElement Object
                        (
                        )

                    [settingID] => 3
                )

            [1] => SimpleXMLElement Object
                (
                    [settingName] => Test
                    [settingDescription] => Testing
                    [requireValue] => 1
                    [localeID] => 14
                    [status] => 1
                    [value] => 66
                    [settingID] => 5
                )

        )

)

What I need to be able to do though is get information based on a current setting. They will never be in the same order so I can't do it by location in the array. I need to be able to access the data based on the setting name.

I have tried a few things with iterating over the array and getting the value I need at that time but it seems like its not a great way to do it.

Is there a better way to store / access this data?

I just need to be able to say If setting name "Home page stats has a status of 1 then then get me its value.

I feel like I may be making this more complicating than it should be.

2 Answers 2

2

Have you tried setting a key in an associative array? Each site get's a unique identifier and you grab the site specific value with $o->setting[$siteID] would give you the proper setting object? Granted it would be possible for code running on a site to potentially query the settings for a different site. If [settings] was also a SimpleXMLElement Object you could use and xpath query to find the right element.

SimpleXMLElement Object
(
    [settings] => Array
        (
            [Home Page] => SimpleXMLElement Object
                (
                    [settingName] => Home Page Stats
                    [settingDescription] => Show the Top 5 Teammate / Teamleader stats?
                    [requireValue] => 0
                    [localeID] => 14
                    [status] => 1
                    [value] => SimpleXMLElement Object
                        (
                        )

                    [settingID] => 3
                )

            [Test] => SimpleXMLElement Object
                (
                    [settingName] => Test
                    [settingDescription] => Testing
                    [requireValue] => 1
                    [localeID] => 14
                    [status] => 1
                    [value] => 66
                    [settingID] => 5
                )
            [Another Site] => SimpleXMLElement Object
                (
                    [settingName] => Another Site
                    [settingDescription] => Another Site
                    [requireValue] => 1
                    [localeID] => 14
                    [status] => 1
                    [value] => 66
                    [settingID] => 5
                )
        )
)

Beyond that, have you thought about having a small config file (.ini, .json, or .xml) for each site and just read it as you load the site? Or move to a pure SimpleXML structure instead of intermixing an array into it? It would make the whole structure traversable with xpath (though it may be more process intensive than the functionality is worth).

0

This seems like the perfect application of the strategy pattern. The basic idea is that you have a bunch of different strategies (these are database connection settings in you scenario) and based on a condition (this is the locale in your scenario) you execute a specific strategy.

The best example I've seen to describe this pattern has to do with a checkout application that needs to calculate tax based on your country of origin. There are lots of ways to implement this. The example below uses an if statement based on your country. If you needed to support 2 or 3 countries, then this would suffice. If you needed to support 20 countries, then you would want to put the logic into different objects.

class TaxStrategy {
    public double CalculateTaxByCountry(String country, double amount) {
       if (country equals "Canada") {
           return 0.12 * amount;
       }
       else if (country equals ("USA")) {
           return 0.05 * amount;
       }
    }
}

class Main {
    public static void main(String[] args) {
        TaxStrategy taxStrategy = new TaxStrategy();
        String countryOfOrigin = "Canada"; // you would get this from the user
        double amount = 15.84;
        double tax = taxStrategy.CalculateTaxByCountry(countryOfOrigin);
    }
}

More information on this pattern can be found here: http://www.oodesign.com/strategy-pattern.html There is an example in this link that shows how to implement it with classes. The example I provided shows the basic idea of how the pattern works on its simplest level.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.