<?php
    class webDB{
        private $url;
        private $db;
        private $key;
        private $dataSource;

        function __construct($url,$db,$key,$dataSource) {
            $this->url=$url;
            $this->db=$db;
            $this->key=$key;
            $this->dataSource=$dataSource;
        }

        public function get($table,$id){
            $url=$this->url;
            return $this->call("${url}/action/findOne",[
                "dataSource"=>$this->dataSource,
                "database"=>$this->db,
                "collection"=>$table,
                "filter"=>[ "_id"=>$id ],
            ]);
        }

        public function list($table,$filter=false){
            $url=$this->url;
            if ($filter===false)
                return $this->call("${url}/action/find",[
                    "dataSource"=>$this->dataSource,
                    "database"=>$this->db,
                    "collection"=>$table,
                ])["documents"];
            else
                return $this->call("${url}/action/find",[
                    "dataSource"=>$this->dataSource,
                    "database"=>$this->db,
                    "collection"=>$table,
                    "filter"=>$filter,
                ])["documents"];
        }

        public function insert($table,$data,$many=false){
            $url=$this->url;
            if ($many){
                $this->call("${url}/action/insertMany",[
                    "dataSource"=>$this->dataSource,
                    "database"=>$this->db,
                    "collection"=>$table,
                    "document"=>$data,// <- in this case $data should be a simepl array of associative arrays
                ]);
            }else{
                $this->call("${url}/action/insertOne",[
                    "dataSource"=>$this->dataSource,
                    "database"=>$this->db,
                    "collection"=>$table,
                    "document"=>$data,
                ]);
            }
        }

        public function update($table,$changes){
            $this->call("${url}/action/updateOne",[
                "dataSource"=>$this->dataSource,
                "database"=>$this->db,
                "collection"=>$table,
                "filter"=>[ "_id"=>$id ],
                "update"=>[ "$set"=>$changes ],
            ]);
        }

        public function replace($table,$data){
            $this->call("${url}/action/replaceOne",[
                "dataSource"=>$this->dataSource,
                "database"=>$this->db,
                "collection"=>$table,
                "filter"=>[ "_id"=>$id ],
                "replacement"=>$data,
            ]);
        }

        public function delete($table,$id){
            $this->call("${url}/action/deleteOne",[
                "dataSource"=>$this->dataSource,
                "database"=>$this->db,
                "collection"=>$table,
                "filter"=>[ "_id"=>$id ],
            ]);
        }

        function call($url,$data){
            $key=$this->key;
            $options = [
                'http' => [
                    'header' => "Content-type: application/json\r\nAccept: application/json\r\napiKey: ${key}\r\n",
                    'method' => 'POST',
                    'content' => json_encode($data),
                ],
            ];

            $context = stream_context_create($options);
            $result = file_get_contents($url, false, $context);
            if ($result === false) {
                /* Handle error */
                $error = error_get_last();
                throw new Exception("Mongo error: " . $error['message']);
                die();
            }
            return json_decode($result,true);
        }
    }

?>