Passed
Push — master ( 0076ce...72e11f )
by Richard
10:31 queued 15s
created

Field::__get()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 9
nc 5
nop 1
dl 0
loc 15
rs 9.9666
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
	/**
12
	 * @var array|string the cotent of the field
13
	 */
14
	protected $content;
15
16
	/**
17
	 * @var Block reference to the parent block
18
	 */
19
	protected $block;
20
21
	/**
22
	 * Creates the new field taking it’s content and a reference
23
	 * to the parent Block
24
	 *
25
	 * @param $content
26
	 * @param $block
27
	 */
28
	public function __construct($content, $block)
29
	{
30
		$this->content = $content;
31
		$this->block = $block;
32
33
		if (method_exists($this, 'init')) {
34
			$this->init();
35
		}
36
	}
37
38
	public function content() {
39
		return $this->content;
40
	}
41
42
	public function block() {
43
		return $this->block;
44
	}
45
46
	public function has($key) {
47
		return array_key_exists($key, $this->content);
0 ignored issues
show
Bug introduced by
It seems like $this->content can also be of type string; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

47
		return array_key_exists($key, /** @scrutinizer ignore-type */ $this->content);
Loading history...
48
	}
49
50
	public function __get($key) {
51
		$accessor = 'get' . Str::studly($key) . 'Attribute';
52
53
		if (method_exists($this, $accessor)) {
54
			return $this->$accessor();
55
		}
56
57
		try {
58
			if ($this->has($key)) {
59
				return $this->content[$key];
60
			}
61
62
			return false;
63
		} catch (Exception $e) {
64
			return 'Caught exception: ' .  $e->getMessage();
65
		}
66
	}
67
68
	abstract public function __toString();
69
}