1: <?php
2:
3: /**
4: * Project sberp
5: *
6: * @file Container
7: * @author Jaromír Polášek
8: * @version 0.7.1.FORM
9: * Encoding UTF-8
10: */
11:
12: namespace App\Lib;
13:
14: use Nette\Object;
15:
16: /**
17: * Základní implementace ArrayAccess, iterator a count na třídu. Tžída převede dotaz na strukturu do vlastního pole.
18: * Protože všechny položky Nette\Database\Selection a GroupedSelection jsou Read-only z důvodů implementace metody update
19: *
20: * Zdroje:
21: * @link https://www.interval.cz/clanky/oop-v-php-standard-php-library-spl-zakladni-rozhrani/ třída jako pole
22: * @link http://php.net/manual/en/class.iterator.php Iterace nad třídou
23: * @link http://php.net/manual/en/class.arrayaccess.php Přístup k třídě jako k poli
24: * @link http://php.net/manual/en/class.countable.php
25: */
26: class Container extends Object implements \ArrayAccess, \Countable, \Iterator {
27:
28: /**
29: * Kontejner na data
30: * @var array
31: */
32: protected $container = array();
33:
34: /**
35: * Klíče asociativního pole
36: * @var array pole klíčů
37: */
38:
39: private $keys;
40: /**
41: * Konstruktor Naplní Pole Hodnotami
42: * @param array $container
43: */
44: protected function __construct($container) {
45: reset($container);
46: $this->container = $container;
47: }
48:
49: /**
50: * Vkládá prvek do objektu.
51: * @param mixed $offset
52: * @param mixed $value
53: */
54: public function offsetSet($offset, $value) {
55: if (is_null($offset)) {
56: $this->container[] = $value;
57: } else {
58: $this->container[$offset] = $value;
59: }
60: }
61:
62: /**
63: * jišťuje, zda daná hodnota existuje někde v atributech třídy.
64: * Zavolá se použitím funkce isset() na objekt.
65: * @param mixed $offset
66: * @return boolean
67: */
68: public function offsetExists($offset) {
69: return isset($this->container[$offset]);
70: }
71:
72: /**
73: * Odstraňuje atribut z třídy. Metoda se zavolá použitím funkce unset() na objekt.
74: * @param mixed $offset
75: */
76: public function offsetUnset($offset) {
77: unset($this->container[$offset]);
78: }
79: /**
80: * Navrací prvek uložený v objektu.
81: * @param mixed $offset
82: * @return mixed
83: */
84: public function offsetGet($offset) {
85: return isset($this->container[$offset]) ? $this->container[$offset] : null;
86: }
87: /**
88: * Implementace Count na třídu
89: * @return int
90: */
91: public function count() {
92: return count($this->container);
93: }
94:
95: /**
96: * Vrátí itterator na začátek pole
97: */
98: function rewind() {
99: $this->keys = array_keys($this->container);
100: reset($this->keys);
101: }
102:
103: /**
104: * Vrátí aktuální element
105: * @return mixed
106: */
107: function current() {
108: if (($key = current($this->keys)) !== FALSE) {
109: return $this->container[$key];
110: } else {
111: return FALSE;
112: }
113: }
114:
115: /**
116: * Vrátí klíč aktuálníjo elementu
117: * @return mixed
118: */
119: function key() {
120: return current($this->keys);
121: }
122:
123: /**
124: * přesune ukazatel na dalši element
125: */
126: function next() {
127: next($this->keys);
128: }
129:
130: /**
131: * Ověří existenci současné pozice
132: * @return boolean
133: */
134: function valid() {
135: return current($this->keys) !== FALSE;
136: }
137: }
138: