<?php

// $Id$

require_once(TANGRA_MAIN_DIR.'exceptions/tangra_module_exception.class.php');


class Visitor_Cookie extends Tangra_Class {
	private $id;
	private $cookie;
	private $notify_on_create = array();
	private $notify_on_update = array();
	private $new = false; // shows if cookie is created in this session

	public function set_id($id) {
		tangra_if_not_int_throw_e($id);

		$this->id = $id;
	}


	public function get_id() {
		return $this->id;
	}


	public function set_cookie($cookie) {
		if ($cookie) {
			if (strlen($cookie) != 32) {
				throw new Tangra_Module_Exception('Invalid cookie value length (max length = 32).');
			}
		}

		$this->cookie = $cookie;
	}


	public function get_cookie() {
		return $this->cookie;
	}


	public function add_notify_on_create($notify_on_create) {
		$this->notify_on_create[] = $notify_on_create;

	}

	public function set_notify_on_create($notify_on_create) {
		if (is_array($notify_on_create)) {
			$this->notify_on_create = $notify_on_create;
		} else {
			throw new Tangra_Module_Exception('Parameter $notify_on_create must be an array.');
		}
	}


	public function get_notify_on_create() {
		return $this->notify_on_create;
	}


	protected function notify_on_create() {
		if (is_array($this->get_notify_on_create())) {
			foreach($this->get_notify_on_create() as $object => $method) {
				call_user_func(array($object, $method), $this->get_id());
			}
		}
	}


	public function add_notify_on_update($notify_on_update) {
		$this->notify_on_update[] = $notify_on_update;

	}

	public function set_notify_on_update($notify_on_update) {
		if (is_array($notify_on_update)) {
			$this->notify_on_update = $notify_on_update;
		} else {
			throw new Tangra_Module_Exception('Parameter $notify_on_update must be an array.');
		}
	}


	public function get_notify_on_update() {
		return $this->notify_on_update;
	}


	protected function notify_on_update() {
		if (is_array($this->get_notify_on_update())) {
			foreach($this->get_notify_on_update() as $object => $method) {
				call_user_func(array($object, $method), $this->get_id());
			}
		}
	}


	public function set_new($new) {
		$this->new = $new ? true : false;
	}


	public function get_new() {
		return $this->new;
	}
}