1: <?php
2:
3: namespace Magyarjeti\Loripsum;
4:
5: use Magyarjeti\Loripsum\Http\AdapterInterface;
6:
7: 8: 9:
10: class Client
11: {
12: 13: 14:
15: const API_URL = 'http://loripsum.net/api/';
16:
17: 18: 19:
20: protected $capabilities = [
21: 'short',
22: 'medium',
23: 'long',
24: 'verylong',
25: 'decorate',
26: 'link',
27: 'ul',
28: 'ol',
29: 'dl',
30: 'bq',
31: 'code',
32: 'headers',
33: 'allcaps',
34: 'prude'
35: ];
36:
37: 38: 39:
40: protected $params = [];
41:
42: 43: 44:
45: protected $paragraphs;
46:
47: 48: 49:
50: protected $conn;
51:
52: 53: 54: 55: 56:
57: public function __construct(AdapterInterface $conn)
58: {
59: $this->conn = $conn;
60: }
61:
62: 63: 64: 65: 66: 67:
68: public function html($paragraphs = null)
69: {
70: $this->paragraphs = $paragraphs;
71:
72: unset($this->params['plaintext']);
73:
74: return $this->generate();
75: }
76:
77: 78: 79: 80: 81: 82:
83: public function text($paragraphs = null)
84: {
85: $this->paragraphs = $paragraphs;
86:
87: $this->params['plaintext'] = true;
88:
89: return $this->generate();
90: }
91:
92: 93: 94: 95: 96: 97: 98: 99:
100: public function __call($name, array $params)
101: {
102: if (!in_array($name, $this->capabilities)) {
103: throw new \RuntimeException("Unknown parameter: $name");
104: }
105:
106: $this->params[$name] = true;
107:
108: return $this;
109: }
110:
111: 112: 113: 114: 115:
116: protected function generate()
117: {
118: $params = array_keys($this->params);
119:
120: if ($this->paragraphs) {
121: $params[] = $this->paragraphs;
122: }
123:
124: $url = self::API_URL . implode('/', $params);
125:
126: return $this->conn->request($url);
127: }
128: }
129: