HEX
Server: Apache
System: Linux webm010.cluster103.gra.hosting.ovh.net 6.18.42-ovh-vps-grsec-zfs+ #1 SMP PREEMPT_DYNAMIC Wed Aug 5 15:59:48 CEST 2026 x86_64
User: iestorre (46869)
PHP: 8.0.30
Disabled: _dyuweyrj4,_dyuweyrj4r,dl
Upload Files
File: /home/i/e/s/iestorre/moodleAW/moodle310/lib/mlbackend/php/phpml/src/Phpml/NeuralNetwork/Layer.php
<?php

declare(strict_types=1);

namespace Phpml\NeuralNetwork;

use Phpml\Exception\InvalidArgumentException;
use Phpml\NeuralNetwork\Node\Neuron;

class Layer
{
    /**
     * @var Node[]
     */
    private $nodes = [];

    /**
     * @param int                     $nodesNumber
     * @param string                  $nodeClass
     * @param ActivationFunction|null $activationFunction
     *
     * @throws InvalidArgumentException
     */
    public function __construct(int $nodesNumber = 0, string $nodeClass = Neuron::class, ActivationFunction $activationFunction = null)
    {
        if (!in_array(Node::class, class_implements($nodeClass))) {
            throw InvalidArgumentException::invalidLayerNodeClass();
        }

        for ($i = 0; $i < $nodesNumber; ++$i) {
            $this->nodes[] = $this->createNode($nodeClass, $activationFunction);
        }
    }

    /**
     * @param string                  $nodeClass
     * @param ActivationFunction|null $activationFunction
     *
     * @return Neuron
     */
    private function createNode(string $nodeClass, ActivationFunction $activationFunction = null)
    {
        if (Neuron::class == $nodeClass) {
            return new Neuron($activationFunction);
        }

        return new $nodeClass();
    }

    /**
     * @param Node $node
     */
    public function addNode(Node $node)
    {
        $this->nodes[] = $node;
    }

    /**
     * @return Node[]
     */
    public function getNodes()
    {
        return $this->nodes;
    }
}