Factory::extend()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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