Factory   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 11
c 1
b 0
f 1
dl 0
loc 75
rs 10
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getAvailable() 0 3 1
A __construct() 0 3 1
A extend() 0 5 1
A getInstance() 0 7 2
A make() 0 3 1
1
<?php
2
3
namespace Kontrolio;
4
5
/**
6
 * Validation service factory.
7
 *
8
 * @package Kontrolio
9
 */
10
class Factory implements FactoryInterface
11
{
12
    /**
13
     * Cached instance of the validation factory.
14
     *
15
     * @var static
16
     */
17
    private static $instance;
18
19
    /**
20
     * All available validation rules.
21
     *
22
     * @var array
23
     */
24
    protected $available = [];
25
26
    /**
27
     * Validation service factory constructor.
28
     */
29
    public function __construct()
30
    {
31
        $this->extend(require __DIR__ . '/../config/aliases.php');
32
    }
33
34
    /**
35
     * Returns singleton instance of the validation factory.
36
     * It's supposed to use only in container unaware environments.
37
     *
38
     * @return static
39
     */
40
    public static function getInstance()
41
    {
42
        if (static::$instance === null) {
0 ignored issues
show
Bug introduced by
Since $instance is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $instance to at least protected.
Loading history...
43
            return static::$instance = new static;
44
        }
45
46
        return static::$instance;
47
    }
48
49
    /**
50
     * Creates new validator instance.
51
     *
52
     * @param array $data
53
     * @param array $rules
54
     * @param array $messages
55
     *
56
     * @return Validator
57
     */
58
    public function make(array $data, array $rules, array $messages = [])
59
    {
60
        return (new Validator($data, $rules, $messages))->extend($this->available);
61
    }
62
63
    /**
64
     * Extends available rules with new ones.
65
     *
66
     * @param array $rules
67
     *
68
     * @return $this
69
     */
70
    public function extend(array $rules)
71
    {
72
        $this->available = array_merge($this->available, $rules);
73
74
        return $this;
75
    }
76
77
    /**
78
     * Returns all available validation rules.
79
     *
80
     * @return array
81
     */
82
    public function getAvailable()
83
    {
84
        return $this->available;
85
    }
86
}
87