1
\$\begingroup\$

I'm working on improving myself by teaching myself how to make classes to remove reppetitive pieces of code and create a class that does it for me.

I have the following xml document.

<?xml version="1.0" encoding="UTF-8"?>
<Attributesets>
    <Aansluitmateriaal>
        <EF023685>
            <code>default</code>
            <name>Thermostatisch voorbereid</name>
        </EF023685>
        <EF023855>
            <code>staartstuk_gebogen</code>
            <name>Met gebogen staartstuk</name>
        </EF023855>
        <EF026393>
            <code>schroefdraad</code>
            <name>Spoed schroefdraad leidingaansluiting</name>
        </EF026393>
    </Aansluitmateriaal>
</Attributesets>

And the following data array

Array(
    'EF023685' => 1,
    'EF023855' => 0,
    'EF026393' => 'spiraal',
    'EF000443' => 'Kanaaleind',
)

Both the data array and the XML document can have nodes that don't exist in the other one. if the node code has default as value the code should be discarded i.e. not returned. otherwise the code should be returned. To do that I have made the following class and would like to know what I could do better and possible concerns you see.

The script is to return an array with the array keys being the code value from the xml document. This array is later used to build up an sql update query

The LoadImportAttributes is fed the location of the xml document, the Node to look for, and the name of the node to return. (in that order)

The setEfImportValues takes The array followed by the returned array from LoadImportAttributes

I hope this clears up my question somewhat

class CustomImportClass{
    private $DataArray = Array();
    private $XmlData = Array();
    private $ReturnArray = Array();
    private $XmlDocument;
    private $DataNodeName;
    private $ReturnNodeName;
    /**
    * Object that builds an array of Value 1 and value 2
    * Expects Array 1 = Array[key] value
    * Expects Array 2 = Array[key] value
    * Expects Array 1 [key] == $Array 2 [key]
    * Ignores
    * Any data with Array 2 Value == "default"
    * Any data where Array 2 [key] == null or ""
    * Returns
    * Array[Array 2 Value] = Array 1 Value
    *
    * @return Array
    */
    public function setEfImportValues($DataArray,$XmlData){
        foreach($DataArray as $EfCode => $ProductValue){
            if(!is_null($EfCode) && (string)$EfCode!="" && !is_null($XmlData[$EfCode]) && $XmlData[$EfCode]!='default'){
                $ReturnArray[$XmlData[$EfCode]] = $ProductValue;
            }
        }
        return($ReturnArray);
    }
    /**
    * Object to get specific Node inside Nested Nodes found in the Searched Node
    * Requires SimpleXml & XPath
    * Expects XML Document Path
    * Expects Search Node Name
    * Expects Return Node Name
    * Expects Search Node Name->RandomNodeName->ReturnNodeName
    * Ignores
    * Any other nodes found
    * Returns
    * Array[$NodeName] = ReturnNodeName->Value
    *
    * @return Array
    */
    public function LoadImportAttributes($XmlDocument,$DataNodeName,$ReturnNodeName){
        $Open = new SimpleXMLElement(file_get_contents($XmlDocument));
        foreach($Open->xpath($DataNodeName) as $FoundNodes){
            foreach($FoundNodes as $NodeName => $NodeValue){
                $ReturnArray[$NodeName] = (string)$NodeValue->$ReturnNodeName;
            }
        }
        return($ReturnArray);
    }
}

If you require example arrays of the data as it's send to the function, I can provide that aswell.

\$\endgroup\$
  • 3
    \$\begingroup\$ Please add a description of what it is your code actually does and update your question title to reflect it. You are likely to get better reviews and more interest if you're question is easy to read and understand. This allows reviewers to spend more time on your actual implementation and less on figuring out what it is trying to do. \$\endgroup\$ – forsvarir Jul 29 '16 at 13:09

Your Answer

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

Browse other questions tagged or ask your own question.