コード例 #1
0
ファイル: AkActiveRecord.php プロジェクト: joeymetal/v1
    /**
     * Generate a xml representation of the model record.
     *
     * Example result:
     *
     * <?xml version="1.0" encoding="UTF-8"?>
     * <person>
     *    <id>1</id>
     *    <first-name>Hansi</first-name>
     *    <last-name>Müller</last-name>
     *    <email>hans@mueller.com</email>
     *    <created-at type="datetime">2008-01-01 13:01:23</created-at>
     * </person>
     *
     * parameters:
     *
     * @param array $options
     *
     *              option parameters:
     *             array(
     *              'collection' => array($Person1,$Person), // array of ActiveRecords
     *              'include' => array('association1','association2'), // include the associations when exporting
     *              'exclude' => array('id','name'), // exclude the attribtues
     *              'only' => array('email','last_name') // only export these attributes
     *              )
     * @return string in Xml Format
     */
    function toXml($options = array())
    {
        if (is_array($options) && isset($options[0]) && is_a($options[0], 'AkActiveRecord')) {
            $options = array('collection'=>$options);
        }
        if (isset($options['collection']) && is_array($options['collection']) && $options['collection'][0]->_modelName == $this->_modelName) {
            $root = strtolower(AkInflector::pluralize($this->_modelName));
            $root = $this->_convert_column_to_xml_element($root);
            $xml = '';
            if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) {
                $xml .= '<?xml version="1.0" encoding="UTF-8"?>';
            }
            $xml .= '<' . $root . '>';
            $collection = $options['collection'];
            unset($options['collection']);
            $options['skip_instruct'] = true;
            foreach ($collection as $element) {
                $xml .= $element->toXml($options);
            }
            $xml .= '</' . $root .'>';
            return $xml;
        }
        /**
         * see if we need to include associations
         */
        $associatedIds = array();
        if (isset($options['include']) && !empty($options['include'])) {
            $options['include'] = is_array($options['include'])?$options['include']:preg_split('/,\s*/',$options['include']);
            foreach ($this->_associations as $key => $obj) {
                if (in_array($key,$options['include'])) {
                    if ($obj->getType()!='hasAndBelongsToMany') {
                        $associatedIds[$obj->getAssociationId() . '_id'] = array('name'=>$key,'type'=>$obj->getType());
                    } else {
                        $associatedIds[$key] = array('name'=>$key,'type'=>$obj->getType());
                    }
                }
            }
        }
        if (isset($options['only'])) {
            $options['only'] = is_array($options['only'])?$options['only']:preg_split('/,\s*/',$options['only']);
        }
        if (isset($options['except'])) {
            $options['except'] = is_array($options['except'])?$options['except']:preg_split('/,\s*/',$options['except']);
        }
        $xml = '';
        if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) {
            $xml .= '<?xml version="1.0" encoding="UTF-8"?>';
        }
        $root = $this->_convert_column_to_xml_element(strtolower($this->_modelName));

        $xml .= '<' . $root . '>';
        $xml .= "\n";
        foreach ($this->_columns as $key => $def) {

            if (isset($options['except']) && in_array($key, $options['except'])) {
                continue;
            } else if (isset($options['only']) && !in_array($key, $options['only'])) {
                continue;
            } else {
                $columnType = $def['type'];
                $elementName = $this->_convert_column_to_xml_element($key);
                $xml .= '<' . $elementName;
                $val = $this->$key;
                if (!in_array($columnType,array('string','text','serial'))) {
                    $xml .= ' type="' . $columnType . '"';
                    if ($columnType=='boolean') $val = $val?1:0;
                }
                $xml .= '>' . Ak::utf8($val) . '</' . $elementName . '>';
                $xml .= "\n";
            }
        }
        if (isset($options['include'])) {
            foreach($this->_associationIds as $key=>$val) {
                if ((in_array($key,$options['include']) || in_array($val,$options['include']))) {
                    if (is_array($this->$key)) {

                        $associationElement = $key;
                        $associationElement = AkInflector::pluralize($associationElement);
                        $associationElement = $this->_convert_column_to_xml_element($associationElement);
                        $xml .= '<'.$associationElement.'>';
                        foreach ($this->$key as $el) {
                            if (is_a($el,'AkActiveRecord')) {
                                $xml .= $el->toXml(array('skip_instruct'=>true));
                            }
                        }
                        $xml .= '</' . $associationElement .'>';
                    } else {
                        $el = &$this->$key->load();
                        if (is_a($el,'AkActiveRecord')) {
                            $xml.=$el->toXml(array('skip_instruct'=>true));
                        }
                    }
                }
            }
        }
        $xml .= '</' . $root . '>';
        return $xml;
    }
コード例 #2
0
ファイル: text_helper.php プロジェクト: joeymetal/v1
 /**
  * Recodes "$text" into utf-8 from "$input_string_encoding"
  */
 function utf8($text, $input_string_encoding = null)
 {
     return Ak::utf8($text, $input_string_encoding);
 }
コード例 #3
0
ファイル: utilities.php プロジェクト: bermi/akelos
 /**
  * Generate a xml representation of the model record.
  *
  * Example result:
  *
  * <?xml version="1.0" encoding="UTF-8"?>
  * <person>
  *    <id>1</id>
  *    <first-name>Hansi</first-name>
  *    <last-name>Müller</last-name>
  *    <email>hans@mueller.com</email>
  *    <created-at type="datetime">2008-01-01 13:01:23</created-at>
  * </person>
  *
  * parameters:
  *
  * @param array $options
  *
  *              option parameters:
  *             array(
  *              'collection' => array($Person1,$Person), // array of ActiveRecords
  *              'include' => array('association1','association2'), // include the associations when exporting
  *              'exclude' => array('id','name'), // exclude the attribtues
  *              'only' => array('email','last_name') // only export these attributes
  *              )
  * @return string in Xml Format
  */
 public function toXml($options = array())
 {
     $options['padding'] = empty($options['padding']) ? 0 : $options['padding'] + 1;
     $current_padding = str_repeat(' ', $options['padding']);
     if (is_array($options) && isset($options[0]) && $options[0] instanceof ArrayAccess) {
         $options = array('collection' => $options);
     }
     if (isset($options['collection']) && (is_array($options['collection']) || $options['collection'] instanceof ArrayAccess) && $options['collection'][0]->_modelName == $this->_Model->getModelName()) {
         $root = AkInflector::underscore(AkInflector::pluralize($this->_Model->getModelName()));
         $root = $this->_convertColumnToXmlElement($root);
         $xml = '';
         if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) {
             $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
         }
         $xml .= $current_padding . '<' . $root . " type=\"array\">\n";
         $collection = $options['collection'];
         unset($options['collection']);
         $options['skip_instruct'] = true;
         $options['padding']++;
         foreach ($collection as $element) {
             $xml .= $element->toXml($options);
         }
         $xml .= $current_padding . '</' . $root . ">\n";
         return $xml;
     }
     /**
      * see if we need to include associations
      */
     $associatedIds = array();
     if (isset($options['include']) && !empty($options['include'])) {
         $options['include'] = is_array($options['include']) ? $options['include'] : preg_split('/,\\s*/', $options['include']);
         foreach ($this->_Model->getAssociations() as $key => $obj) {
             if (in_array($key, $options['include'])) {
                 if ($obj->getType() != 'hasAndBelongsToMany') {
                     $associatedIds[$obj->getAssociationIds() . '_id'] = array('name' => $key, 'type' => $obj->getType());
                 } else {
                     $associatedIds[$key] = array('name' => $key, 'type' => $obj->getType());
                 }
             }
         }
     }
     if (isset($options['only'])) {
         $options['only'] = is_array($options['only']) ? $options['only'] : preg_split('/,\\s*/', $options['only']);
     }
     if (isset($options['except'])) {
         $options['except'] = is_array($options['except']) ? $options['except'] : preg_split('/,\\s*/', $options['except']);
     }
     $xml = '';
     if (!(isset($options['skip_instruct']) && $options['skip_instruct'] == true)) {
         $xml .= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
     }
     $root = $this->_convertColumnToXmlElement(AkInflector::underscore($this->_Model->getModelName()));
     $xml .= $current_padding . '<' . $root . ">\n";
     foreach ($this->_Model->getColumns() as $key => $def) {
         if (isset($options['except']) && in_array($key, $options['except'])) {
             continue;
         } else {
             if (isset($options['only']) && !in_array($key, $options['only'])) {
                 continue;
             } else {
                 $columnType = $def['type'];
                 $elementName = $this->_convertColumnToXmlElement($key);
                 $xml .= $current_padding . '  <' . $elementName;
                 $val = $this->_Model->{$key};
                 if (!in_array($columnType, array('string', 'text', 'serial'))) {
                     $xml .= ' type="' . $columnType . '"';
                     if ($columnType == 'boolean') {
                         $val = $val ? 1 : 0;
                     }
                 } elseif ($columnType == 'serial' && is_numeric($val)) {
                     $xml .= ' type="integer"';
                 }
                 if (!empty($val) && in_array($columnType, array('datetime'))) {
                     // UTC (Coordinated Universal Time) http://www.w3.org/TR/NOTE-datetime
                     $val = gmdate('Y-m-d\\TH:i:s\\Z', Ak::getTimestamp($val));
                 }
                 if (is_null($val)) {
                     $xml .= ' nil="true"';
                 }
                 $xml .= '>' . Ak::utf8($val) . '</' . $elementName . ">\n";
             }
         }
     }
     if (isset($options['include'])) {
         foreach ($this->_Model->getAssociations() as $key => $val) {
             if (in_array($key, $options['include']) || in_array($val, $options['include'])) {
                 if (is_array($this->_Model->{$key})) {
                     $associationElement = $key;
                     $associationElement = AkInflector::underscore(AkInflector::pluralize($associationElement));
                     $associationElement = $this->_convertColumnToXmlElement($associationElement);
                     $xml .= $current_padding . '<' . $associationElement . " type=\"array\">\n";
                     $options['padding']++;
                     foreach ($this->_Model->{$key} as $el) {
                         if ($el instanceof AkBaseModel) {
                             $xml .= $el->toXml(array('skip_instruct' => true));
                         }
                     }
                     $xml .= $current_padding . '</' . $associationElement . ">\n";
                 } else {
                     $el = $this->_Model->{$key}->load();
                     if ($el instanceof AkBaseModel) {
                         $xml .= $el->toXml(array('skip_instruct' => true));
                     }
                 }
             }
         }
     }
     $xml .= $current_padding . '</' . $root . ">\n";
     return $xml;
 }