<?php

/**
 * Return the area security information
 */
namespace App\Traits;

trait AreaSecurity
{
	/**
	 * Does the security level provide read or write access
	 *
	 * @param int $sec
	 * @return bool
	 */
	public function can_access(?int $sec): bool
	{
		return $this->can_read($sec) || $this->can_write($sec);
	}

	/**
	 * Does the security level provide read access
	 *
	 * @param int $sec
	 * @return bool
	 */
	public function can_read(?int $sec): bool
	{
		return $this->active && (($sec >= ($x=$this->getSecReadAttribute())) && $x);
	}

	/**
	 * Does the security level provide write access
	 *
	 * @param int $sec
	 * @return bool
	 */
	public function can_write(?int $sec): bool
	{
		return $this->active && (($sec >= ($x=$this->getSecWriteAttribute())) && $x);
	}

	public function getSecReadAttribute(): int
	{
		return ($this->security>>3) & 0x7;
	}

	public function getSecWriteAttribute(): int
	{
		return $this->security & 0x7;
	}

	public function setRead(int $security): void
	{
		$this->security = ($this->security & ~(0x7<<3)) | (($security & 0x7) << 3);
	}

	public function setWrite(int $security): void
	{
		$this->security = ($this->security & ~(0x7)) | ($security & 0x7);
	}
}