%PDF- %PDF-
Mini Shell

Mini Shell

Direktori : /usr/local/sakura-blog/themes/theme-simple/white/v1.1.3/lib/models/
Upload File :
Create Path :
Current File : //usr/local/sakura-blog/themes/theme-simple/white/v1.1.3/lib/models/base.php

<?php

namespace SakuraBlog\Models;

use Iterator;

class Model
{
	public int $id;
}

abstract class Collection implements Iterator
{
	private $position = 0;
	private $arr = [];
	private $index = [];

	function __construct(array $arr)
	{
		$this->arr = $arr;
		foreach ($arr as $i => $v) {
			$this->createIndex($v, $i);
		}
	}


	private function createIndex($item, $key)
	{
		if ($item instanceof Model) {
			$this->index[$item->id] = $key;
		}
	}

	function first()
	{
		return $this->arr[0];
	}
	function last()
	{
		var_dump($this->arr);
		return $this->arr[$this->count() - 1];
	}
	function count(): int
	{
		return count($this->arr);
	}
	function findByID(int $id)
	{
		if (isset($this->index[$id]) && isset($this->arr[$this->index[$id]])) {
			return $this->arr[$this->index[$id]];
		}
		return null;
	}

	function add($item)
	{
		$this->arr[] = $item;
		$key = count($this->arr) - 1;
		$this->createIndex($item, $key);
	}

	function _get($key): mixed
	{
		return $this->arr[$key];
	}

	function update($id, $item)
	{
		if (isset($this->index[$id]) && isset($this->arr[$this->index[$id]])) {
			$key = $this->index[$id];
			$this->arr[$key] = $item;
		}
	}

	function rewind(): void
	{
		$this->position = 0;
	}


	abstract public function current(): mixed;

	function _current(): mixed
	{
		return $this->arr[$this->position];
	}

	function key(): int
	{
		return $this->position;
	}

	function next(): void
	{
		$this->position++;
	}

	function valid(): bool
	{
		return isset($this->arr[$this->position]);
	}
}

Zerion Mini Shell 1.0