<?php

if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

/**
 * Description of RT_Model
 *
 * @author Igor Ratsun
 */
class RT_Model extends CI_Model implements \SplSubject
{

    protected $event_name = '';
    protected $event_data = [];
    private $observers = array();

    /**
     * Add observer
     * @param \SplObserver $observer
     */
    public function attach(\SplObserver $observer)
    {
        $this->observers[] = $observer;
    }

    /**
     * Remove observer
     * @param \SplObserver $observer
     */
    public function detach(\SplObserver $observer)
    {

        $key = array_search($observer, $this->observers, true);
        if ($key !== false) {
            unset($this->observers[$key]);
        }
    }

    /**
     * Notify observers
     * @param string $event_name
     * @param mixed $event_data
     */
    public function notify($event_name = '', $event_data = [])
    {
        if (!empty($event_name)) {
            $this->event_name = $event_name;
        }
        if (!empty($event_data)) {
            $this->event_data = $event_data;
        }

        foreach ($this->observers as $value) {
            $value->update($this);
        }
    }

    /**
     * Get event name
     * @return string
     */
    public function get_event_name()
    {
        return $this->event_name;
    }

    /**
     * Get event data
     * @return mixed
     */
    public function get_event_data()
    {
        return $this->event_data;
    }

}
