<commit_before><?php
namespace com\mikebevz\xsd2php;
/**
 * Copyright 2010 <NAME> <<EMAIL>>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require_once 'Common.php';

/**
 * Bind XML to PHP binding
 * 
 * @author <NAME> <<EMAIL>>
 * @version 0.0.5
 */
class Bind extends Common {
    
    //protected $dom;
    //protected $namespaces;

    /**
     * __construct
     *
     * @param string  $classPrefix Prefix for generated class names
     *
     * @return void
     */
    public function __construct($classPrefix)
    {
    	parent::__construct($classPrefix); 
    }

    /**
     * Unmarshal XML string to corresponding PHP binding
     * 
     * @param string $xml XML string to unmarshal
     * 
     * @return object PHP binding object
     */
    public function unmarshal($xml) {
        // Get all namespaces
        $this->dom = new \DOMDocument();
        if ($this->debug) {
            $this->dom->formatOutput = true;
        }
        $this->dom->loadXML($xml);

        $this->namespaces =  $this->getDocNamespaces($this->dom);
        
        // Find targetNamespace
        
        $xpath = new \DOMXPath($this->dom);
        $query = ".";
        $root = $xpath->query($query);
        $ns = $code = ""; 
        foreach ($root as $rt) {
            list ($ns, $name) = $this->parseQName($rt->nodeName, true);
        }   
        
        $className = $this->urnToPhpName($ns)."\\".$this->classPrefix.$name;
        
        if (!$this->safeClassExists($className)) {
            throw new \RuntimeException('Class '.$className. ' is not found. Make sure it was included');
        }
        
        $binding = new $className();
        
        return $this->bindXml($xml, $binding);
        
        // Instantiate class
        // Assign corresponding nodes to properties/classes
        // return PHP binding
    }
    
    /**
     * Bind XML file to model
     * @param string $xml   XML source
     * @param object $model PHP Model to bind to
     * 
     * @return object PHP model
     */
    public function bindXml($xml, $model) {
        
        //print_r($xml."\n ".get_class($model));
        
        $this->dom = new \DOMDocument();
        $this->dom->loadXML($xml);
        
        $refl  = new \ReflectionClass(get_class($model));
        $xpath = new \DOMXPath($this->dom);
        
        $query = "child::*";
        $childs = $xpath->query($query);
        
        foreach ($childs as $child) {
            
            list ($ns, $name) = $this->parseQName($child->nodeName, true);
            //$className = $this->urnToPhpName($ns)."\\".$name;
            try {
                $propertyDocs = $refl->getProperty($name)->getDocComment();
            } catch (\ReflectionException $e) {
                throw new \RuntimeException($e->getMessage().". Class ".get_class($model));
            }
            $docs = $this->parseDocComments($propertyDocs);
            $className = $docs['var'];
            
            
            if ($this->hasChild($child)) {
                if (property_exists($model, $name)) {
                    $doc = new \DOMDocument();
                    $doc->appendChild($doc->importNode($child, true));<commit_msg>Fixes #3 top-level attributes being ignored during deserialize<commit_after><?php
namespace com\mikebevz\xsd2php;
/**
 * Copyright 2010 <NAME> <<EMAIL>>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
require_once 'Common.php';

/**
 * Bind XML to PHP binding
 * 
 * @author <NAME> <<EMAIL>>
 * @version 0.0.5
 */
class Bind extends Common {
    
    //protected $dom;
    //protected $namespaces;

    /**
     * __construct
     *
     * @param string  $classPrefix Prefix for generated class names
     *
     * @return void
     */
    public function __construct($classPrefix)
    {
    	parent::__construct($classPrefix); 
    }

    /**
     * Unmarshal XML string to corresponding PHP binding
     * 
     * @param string $xml XML string to unmarshal
     * 
     * @return object PHP binding object
     */
    public function unmarshal($xml) {
        // Get all namespaces
        $this->dom = new \DOMDocument();
        if ($this->debug) {
            $this->dom->formatOutput = true;
        }
        $this->dom->loadXML($xml);

        $this->namespaces =  $this->getDocNamespaces($this->dom);
        
        // Find targetNamespace
        
        $xpath = new \DOMXPath($this->dom);
        $query = ".";
        $root = $xpath->query($query);
        $ns = $code = ""; 
        foreach ($root as $rt) {
            list ($ns, $name) = $this->parseQName($rt->nodeName, true);
        }   
        
        $className = $this->urnToPhpName($ns)."\\".$this->classPrefix.$name;
        
        if (!$this->safeClassExists($className)) {
            throw new \RuntimeException('Class '.$className. ' is not found. Make sure it was included');
        }
        
        $binding = new $className();
        
        return $this->bindXml($xml, $binding);
        
        // Instantiate class
        // Assign corresponding nodes to properties/classes
        // return PHP binding
    }
    
    /**
     * Bind XML file to model
     * @param string $xml   XML source
     * @param object $model PHP Model to bind to
     * 
     * @return object PHP model
     */
    public function bindXml($xml, $model) {
        
        //print_r($xml."\n ".get_class($model));
        
        $this->dom = new \DOMDocument();
        $this->dom->loadXML($xml);
        
        $refl  = new \ReflectionClass(get_class($model));
        $xpath = new \DOMXPath($this->dom);
        
        $query = ".";
        $childs = $xpath->query($query);
        foreach( $childs->item(0)->attributes as $attribute_name => $attribute_node )
        {
            if( property_exists($model, $attribute_name) )
            {
                $model->{$attribute_name} = $attribute_node->nodeValue;
            }
        }
        
        $query = "child::*";
        $childs = $xpath->query($query);
        
        foreach ($childs as $child) {
            
            list ($ns, $name) = $this->parseQName($child->nodeName, true);
            //$className = $this->urnToPhpName($ns)."\\".$name;
            try {
                $propertyDocs = $refl->getProperty($name)->getDocComment();
            } catch (\ReflectionException $e) {
                throw new \RuntimeException($e->getMessage().". Class ".get_class($model));
            }
            $docs = $this->parseDocComments($propertyDocs);
            $className = $docs['var'];
            
            
            if ($this->hasChild($child)) {
                if (property_exists($model, $name)) {
                    $doc = new \DOMDocument();
                    $doc->appendChild($doc->importNode($child, true));
{'fraction_non_alphanumeric': 0.1378869276029675, 'fraction_numerical': 0.00332565873624968, 'mean_word_length': 1.9175373134328357, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 16}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1789292', 'n_tokens_mistral': 2224, 'n_tokens_neox': 2168, 'n_words': 587}