Completed
Push — develop ( c506a8...2412f1 )
by Arkadiusz
03:02
created

Layer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 7
c 2
b 0
f 2
lcom 1
cbo 2
dl 0
loc 56
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A createNode() 0 8 2
A addNode() 0 4 1
A getNodes() 0 4 1
1
<?php
2
3
declare (strict_types = 1);
4
5
namespace Phpml\NeuralNetwork;
6
7
use Phpml\Exception\InvalidArgumentException;
8
use Phpml\NeuralNetwork\Node\Neuron;
9
10
class Layer
11
{
12
    /**
13
     * @var Node[]
14
     */
15
    private $nodes = [];
16
17
    /**
18
     * @param int                     $nodesNumber
19
     * @param string                  $nodeClass
20
     * @param ActivationFunction|null $activationFunction
21
     *
22
     * @throws InvalidArgumentException
23
     */
24
    public function __construct(int $nodesNumber = 0, string $nodeClass = Neuron::class, ActivationFunction $activationFunction = null)
25
    {
26
        if (!in_array(Node::class, class_implements($nodeClass))) {
27
            throw InvalidArgumentException::invalidLayerNodeClass();
28
        }
29
30
        for ($i = 0; $i < $nodesNumber; ++$i) {
31
            $this->nodes[] = $this->createNode($nodeClass, $activationFunction);
32
        }
33
    }
34
35
    /**
36
     * @param string                  $nodeClass
37
     * @param ActivationFunction|null $activationFunction
38
     *
39
     * @return Neuron
40
     */
41
    private function createNode(string $nodeClass, ActivationFunction $activationFunction = null)
42
    {
43
        if (Neuron::class == $nodeClass) {
44
            return new Neuron($activationFunction);
45
        }
46
47
        return new $nodeClass();
48
    }
49
50
    /**
51
     * @param Node $node
52
     */
53
    public function addNode(Node $node)
54
    {
55
        $this->nodes[] = $node;
56
    }
57
58
    /**
59
     * @return Node[]
60
     */
61
    public function getNodes()
62
    {
63
        return $this->nodes;
64
    }
65
}
66