Passed
Branch develop (7ed98a)
by Richard
04:39
created

Field::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 6
rs 10
c 1
b 0
f 0
1
<?php
2
3
4
namespace Riclep\Storyblok;
5
6
use Exception;
7
use Illuminate\Support\Str;
8
9
abstract class Field
10
{
11
	protected $content;
12
13
	public function __construct($content)
14
	{
15
		$this->content = $content;
16
17
		if (method_exists($this, 'init')) {
18
			$this->init();
19
		}
20
	}
21
22
	public function content() {
23
		return $this->content;
24
	}
25
26
	public function has($key) {
27
		return array_key_exists($key, $this->content);
28
	}
29
30
	public function __get($key) {
31
		$accessor = 'get' . Str::studly($key) . 'Attribute';
32
33
		if (method_exists($this, $accessor)) {
34
			return $this->$accessor();
35
		}
36
37
		try {
38
			if ($this->has($key)) {
39
				return $this->content[$key];
40
			}
41
42
			return false;
43
		} catch (Exception $e) {
44
			return 'Caught exception: ' .  $e->getMessage();
45
		}
46
	}
47
48
	abstract public function __toString();
49
}