<?php

/*
 * Copyright (C) 2014, NoccyLabs
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */

namespace NoccyLabs\Gpio\Bus;

abstract class Bus implements BusInterface
{
    protected $pins = array();

    public function __construct()
    {
        $this->configure();
    }
    
    public function addPin($name)
    {
        $this->pins[$name] = null;
    }
    
    public function __set($name, $value)
    {
        if (!array_key_exists($name,$this->pins)) {
            throw new \Exception("No such pin: {$name}");
        }
        $this->pins[$name] = $value;
    }
    
    public function __get($name)
    {
        if (!array_key_exists($name,$this->pins)) {
            throw new \Exception("No such pin: {$name}");
        }
        return $this->pins[$name];
    }

    public function __isSet($name)
    {
        return array_key_exists($name,$this->pins);
    }
    
    public function __unset($name)
    {
        if (!array_key_exists($name,$this->pins)) {
            return;
        }
        $this->pins[$name] = null;
    }
}