Completed
Push — master ( 04f3fb...1e991f )
by Helmut
02:48
created

Plugin::__construct()   B

Complexity

Conditions 5
Paths 2

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 13
ccs 6
cts 6
cp 1
rs 8.8571
cc 5
eloc 7
nc 2
nop 1
crap 5
1
<?php 
2
3
namespace Helmut\Forms;
4
5
use Helmut\Forms\Utility\Str;
6
use Helmut\Forms\Utility\Reflect;
7
8
abstract class Plugin {
9
	
10
    /**
11
     * Autoload path for plugin.
12
     *
13
     * @var string
14
     */
15
	protected $path;
16
17
	/**
18
     * Create a new plugin instance.
19 134
     *
20
     */
21 134
	public function __construct($config = null) 
22 134
	{
23
        if (is_array($config) && count($config)) {
24
            foreach ($config as $key => $value) {
25
                $method = 'set'.Str::studly($key);
26
                if (method_exists($this, $method)) {
27
                    $this->{$method}($value);
28
                }
29 134
            }
30
        }
31 134
32 134
		$this->setPath();
33 134
	}
34
35
	/**
36
     * Set the plugin autoload path.
37
     *
38
     * @return void
39
     */
40
	public function setPath()
41
	{
42
        $this->path = Reflect::getDirectory($this);
43
	}
44
45
	/**
46
     * Get autoload path.
47
     *
48
     * @return string
49
     */
50
	public function path($append = null) 
51
	{
52
		return is_null($append) ? $this->path 
53
			: $this->path.'/'.ltrim(rtrim($append, '/'), '/').'/';
54
	}
55
56
	/**
57
     * Get paths to templates.
58
     *
59
     * @return string
60
     */
61
	public function templatePaths() 
62
	{
63
		$path = $this->path('templates');
64
		return [$path];
65
	}
66
67
	/**
68
     * Trigger an event callback. 
69
     * - onLoad
70
     * - onDefine
71
     * - onRender
72
     * - onSubmitted
73
     * - onCompleted
74 134
     * - onValidate
75
     * - onValidated
76 134
     * - onValid
77
     * - onInvalid
78 134
     *
79 134
	 * @param  \Helmut\Forms\Form  $form
80
	 * @param  string  $name
81
	 * @param  array  $params
82 134
     * @return mixed
83
     */
84
	public function event($form, $name, $params = []) 
85
	{
86
		$name = Str::studly($name);
87
88
		if (method_exists($this, 'on'.$name)) {
89
			return call_user_func_array(array($this, 'on'.$name), [$form, $params]);
90
		}
91
92
	}
93
94
}
95