-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAtom.php
executable file
·72 lines (64 loc) · 1.45 KB
/
Atom.php
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
72
<?php namespace Locker\XApi;
abstract class Atom {
protected $value;
/**
* Constructs a new instance of Atom from the $value.
* @param mixed $value
*/
public function __construct($value = null) {
if ($value === null) return;
$this->setValue($value);
}
/**
* Outputs the value of $this as a JSON string.
* @return string
*/
public function toJson() {
return json_encode($this->getValue());
}
/**
* Adds properties from JSON.
* @param string $json
* @return Atom $this
*/
public function fromJson($json) {
Helpers::checkType('json', 'string', $json);
$decoded_value = json_decode($json);
if ($decoded_value === null && ($json !== 'null' || $json !== '')) {
throw new \Exception('Invalid JSON.');
}
return $this->setValue($decoded_value);
}
/**
* Creates a new instance of an Atom object from $json.
* @param string $json
* @return Atom
*/
public static function createFromJson($json) {
$new_instance = new static();
return $new_instance->fromJson($json);
}
/**
* Sets the $value.
* @param mixed $new_value
* @return Atom $this
*/
public function setValue($new_value) {
$this->value = $new_value;
return $this;
}
/**
* Outputs the $value.
* @return mixed
*/
public function getValue() {
return $this->value;
}
/**
* Validates $this.
* @return [Error]
*/
public function validate() {
return [];
}
}