| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the PacketeryNette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
|
| 8 |
declare(strict_types=1); |
| 9 |
|
| 10 |
namespace PacketeryNette\ComponentModel; |
| 11 |
|
| 12 |
use PacketeryNette; |
| 13 |
|
| 14 |
|
| 15 |
/** |
| 16 |
* Implementation of \ArrayAccess for IContainer. |
| 17 |
*/ |
| 18 |
trait ArrayAccess |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Adds the component to the container. |
| 22 |
* @param string|int $name |
| 23 |
* @param IComponent $component |
| 24 |
*/ |
| 25 |
public function offsetSet($name, $component): void |
| 26 |
{ |
| 27 |
$name = is_int($name) ? (string) $name : $name; |
| 28 |
$this->addComponent($component, $name); |
| 29 |
} |
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* Returns component specified by name. Throws exception if component doesn't exist. |
| 34 |
* @param string|int $name |
| 35 |
* @throws PacketeryNette\InvalidArgumentException |
| 36 |
*/ |
| 37 |
public function offsetGet($name): IComponent |
| 38 |
{ |
| 39 |
$name = is_int($name) ? (string) $name : $name; |
| 40 |
return $this->getComponent($name); |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Does component specified by name exists? |
| 46 |
* @param string|int $name |
| 47 |
*/ |
| 48 |
public function offsetExists($name): bool |
| 49 |
{ |
| 50 |
$name = is_int($name) ? (string) $name : $name; |
| 51 |
return $this->getComponent($name, false) !== null; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Removes component from the container. |
| 57 |
* @param string|int $name |
| 58 |
*/ |
| 59 |
public function offsetUnset($name): void |
| 60 |
{ |
| 61 |
$name = is_int($name) ? (string) $name : $name; |
| 62 |
if ($component = $this->getComponent($name, false)) { |
| 63 |
$this->removeComponent($component); |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|