xref: /plugin/struct/meta/SchemaImporter.php (revision f2e141a0254a80bdb2492f5df1ceed95fa8b6a92)
18ddf87afSAndreas Gohr<?php
28ddf87afSAndreas Gohr
38ddf87afSAndreas Gohrnamespace plugin\struct\meta;
48ddf87afSAndreas Gohr
545560cc7SAndreas Gohrif(!defined('JSON_PRETTY_PRINT')) define('JSON_PRETTY_PRINT', 0); // PHP 5.3 compatibility
645560cc7SAndreas Gohr
78ddf87afSAndreas Gohr/**
88ddf87afSAndreas Gohr * Class SchemaImporter
98ddf87afSAndreas Gohr *
108ddf87afSAndreas Gohr * This works just like the schema builder, except that it expects a JSON structure as input
118ddf87afSAndreas Gohr *
128ddf87afSAndreas Gohr * @package plugin\struct\meta
138ddf87afSAndreas Gohr */
148ddf87afSAndreas Gohrclass SchemaImporter extends SchemaBuilder {
158ddf87afSAndreas Gohr
168ddf87afSAndreas Gohr    /**
178ddf87afSAndreas Gohr     * Import a schema using JSON
188ddf87afSAndreas Gohr     *
198ddf87afSAndreas Gohr     * @todo sanity checking of the input data should be added
208ddf87afSAndreas Gohr     *
218ddf87afSAndreas Gohr     * @param string $table
228ddf87afSAndreas Gohr     * @param string $json
238ddf87afSAndreas Gohr     */
248ddf87afSAndreas Gohr    public function __construct($table, $json) {
258ddf87afSAndreas Gohr        parent::__construct($table, array());
268ddf87afSAndreas Gohr
278ddf87afSAndreas Gohr        // number of existing columns
288ddf87afSAndreas Gohr        $existing = count($this->oldschema->getColumns());
298ddf87afSAndreas Gohr
308ddf87afSAndreas Gohr        $input = json_decode($json, true);
31*f2e141a0SAndreas Gohr        if($input === null) {
32*f2e141a0SAndreas Gohr            switch(json_last_error()) {
33*f2e141a0SAndreas Gohr                case JSON_ERROR_NONE:
34*f2e141a0SAndreas Gohr                    $error = 'No errors';
35*f2e141a0SAndreas Gohr                    break;
36*f2e141a0SAndreas Gohr                case JSON_ERROR_DEPTH:
37*f2e141a0SAndreas Gohr                    $error = 'Maximum stack depth exceeded';
38*f2e141a0SAndreas Gohr                    break;
39*f2e141a0SAndreas Gohr                case JSON_ERROR_STATE_MISMATCH:
40*f2e141a0SAndreas Gohr                    $error = 'Underflow or the modes mismatch';
41*f2e141a0SAndreas Gohr                    break;
42*f2e141a0SAndreas Gohr                case JSON_ERROR_CTRL_CHAR:
43*f2e141a0SAndreas Gohr                    $error = 'Unexpected control character found';
44*f2e141a0SAndreas Gohr                    break;
45*f2e141a0SAndreas Gohr                case JSON_ERROR_SYNTAX:
46*f2e141a0SAndreas Gohr                    $error = 'Syntax error, malformed JSON';
47*f2e141a0SAndreas Gohr                    break;
48*f2e141a0SAndreas Gohr                case JSON_ERROR_UTF8:
49*f2e141a0SAndreas Gohr                    $error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
50*f2e141a0SAndreas Gohr                    break;
51*f2e141a0SAndreas Gohr                default:
52*f2e141a0SAndreas Gohr                    $error = 'Unknown error';
53*f2e141a0SAndreas Gohr                    break;
54*f2e141a0SAndreas Gohr            }
55*f2e141a0SAndreas Gohr
56*f2e141a0SAndreas Gohr            throw new StructException('JSON couldn\'t be decoded: ' . $error);
57*f2e141a0SAndreas Gohr        }
588ddf87afSAndreas Gohr        $data = array(
598ddf87afSAndreas Gohr            'cols' => array(),
608ddf87afSAndreas Gohr            'new' => array()
618ddf87afSAndreas Gohr        );
628ddf87afSAndreas Gohr
638ddf87afSAndreas Gohr        foreach($input['columns'] as $column) {
648ddf87afSAndreas Gohr            // config has to stay json
658ddf87afSAndreas Gohr            $column['config'] = json_encode($column['config'], JSON_PRETTY_PRINT);
668ddf87afSAndreas Gohr
678ddf87afSAndreas Gohr            if(!empty($column['colref']) && $column['colref'] <= $existing) {
688ddf87afSAndreas Gohr                // update existing column
698ddf87afSAndreas Gohr                $data['cols'][$column['colref']] = $column;
708ddf87afSAndreas Gohr            } else {
718ddf87afSAndreas Gohr                // add new column
728ddf87afSAndreas Gohr                $data['new'][] = $column;
738ddf87afSAndreas Gohr            }
748ddf87afSAndreas Gohr        }
758ddf87afSAndreas Gohr
768ddf87afSAndreas Gohr        $this->data = $data;
778ddf87afSAndreas Gohr    }
788ddf87afSAndreas Gohr
798ddf87afSAndreas Gohr}
80