summaryrefslogtreecommitdiffstats
path: root/lib/ocs/result.php
blob: 24029051da5dfa8bca9d806f8c8ad65ed63537ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php

class OC_OCS_Result{
	
	private $data, $message, $statuscode, $items, $perpage;
	
	/**
	 * create the OCS_Result object
	 * @param $data mixed the data to return
	 */
	public function __construct($data=null, $code=100, $message=null){
		$this->data = $data;
		$this->statuscode = $code;
		$this->message = $message;
	}
	
	/**
	 * sets the statuscode
	 * @param $code int
	 */
	public function setCode(int $code){
		$this->statuscode = $code;
	}
	
	/**
	 * optionally set the total number of items available
	 * @param $items int
	 */
	public function setTotalItems(int $items){
		$this->items = $items;
	}
	
	/**
	 * optionally set the the number of items per page
	 * @param $items int
	 */
	public function setItemsPerPage(int $items){
		$this->perpage = $items;
	}
	
	/**
	 * set a custom message for the response
	 * @param $message string the message
	 */
	public function setMessage(string $message){
		$this->message = $message;
	}
	
	/**
	 * returns the data associated with the api result
	 * @return array
	 */
	public function getResult(){
		$return = array();
		$return['meta'] = array();
		$return['meta']['status'] = ($this->statuscode === 100) ? 'ok' : 'failure';
		$return['meta']['statuscode'] = $this->statuscode;
		$return['meta']['message'] = $this->message;
		if(isset($this->items)){
			$return['meta']['totalitems'] = $this->items;
		}
		if(isset($this->perpage)){
			$return['meta']['itemsperpage'] = $this->perpage;
		}
		$return['data'] = $this->data;
		// Return the result data.
		return $return;
	}
	
	
}