Completed
Push — master ( 0196bf...0df159 )
by Helmut
02:29
created

Plugin::configure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 3
cp 0
rs 9.6666
cc 3
eloc 5
nc 3
nop 1
crap 12
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
     * @param  array  $config
21 134
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
22 134
     */
23
	public function __construct($config = null) 
24
	{
25
        $this->configure($config);
0 ignored issues
show
Bug introduced by
It seems like $config defined by parameter $config on line 23 can also be of type null; however, Helmut\Forms\Plugin::configure() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
26
27
		$this->setPath();
28
	}
29 134
30
    /**
31 134
     * Configure the plugin.
32 134
     *
33 134
     * @param  array  $config
34
     * @return void
35
     */   
36
    public function configure($config)
37
    {
38
        foreach ((array) $config as $key => $value) {
39
            $method = 'set'.Str::studly($key);
40
            if (method_exists($this, $method)) {
41
                $this->{$method}($value);
42
            }
43
        }        
44
    }
45
46
	/**
47
     * Set the plugin autoload path.
48
     *
49
     * @return void
50
     */
51
	public function setPath()
52
	{
53
        $this->path = Reflect::getDirectory($this);
54
	}
55
56
	/**
57
     * Get autoload path.
58
     *
59
     * @return string
60
     */
61
	public function path($append = null) 
62
	{
63
		return is_null($append) ? $this->path 
64
			: $this->path.'/'.ltrim(rtrim($append, '/'), '/').'/';
65
	}
66
67
	/**
68
     * Get paths to templates.
69
     *
70
     * @return string
71
     */
72
	public function templatePaths() 
73
	{
74 134
		$path = $this->path('templates');
75
		return [$path];
76 134
	}
77
78 134
	/**
79 134
     * Trigger an event callback. This allows
80
     * you to hook into events from within the plugin.
81
     * - onLoad
82 134
     * - onDefine
83
     * - onRender
84
     * - onSubmitted
85
     * - onCompleted
86
     * - onValidate
87
     * - onValidated
88
     * - onValid
89
     * - onInvalid
90
     *
91
	 * @param  \Helmut\Forms\Form  $form
92
	 * @param  string  $name
93
	 * @param  array  $params
94
     * @return mixed
95
     */
96
	public function event($form, $name, $params = []) 
97
	{
98
		$name = Str::studly($name);
99
100
		if (method_exists($this, 'on'.$name)) {
101
			return call_user_func_array(array($this, 'on'.$name), [$form, $params]);
102
		}
103
104
	}
105
106
}
107