10b3fd2d3SAndreas Gohr<?php 2*dad993c5SAndreas Gohr 30b3fd2d3SAndreas Gohr/** 40b3fd2d3SAndreas Gohr * This file is part of the FreeDSx LDAP package. 50b3fd2d3SAndreas Gohr * 60b3fd2d3SAndreas Gohr * (c) Chad Sikorra <Chad.Sikorra@gmail.com> 70b3fd2d3SAndreas Gohr * 80b3fd2d3SAndreas Gohr * For the full copyright and license information, please view the LICENSE 90b3fd2d3SAndreas Gohr * file that was distributed with this source code. 100b3fd2d3SAndreas Gohr */ 110b3fd2d3SAndreas Gohr 120b3fd2d3SAndreas Gohrnamespace FreeDSx\Ldap\Control; 130b3fd2d3SAndreas Gohr 14*dad993c5SAndreas Gohruse ArrayIterator; 15*dad993c5SAndreas Gohruse Countable; 16*dad993c5SAndreas Gohruse IteratorAggregate; 17*dad993c5SAndreas Gohruse Traversable; 18*dad993c5SAndreas Gohruse function array_search; 19*dad993c5SAndreas Gohruse function count; 20*dad993c5SAndreas Gohruse function is_string; 210b3fd2d3SAndreas Gohr 220b3fd2d3SAndreas Gohr/** 230b3fd2d3SAndreas Gohr * Represents a set of controls. 240b3fd2d3SAndreas Gohr * 250b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com> 260b3fd2d3SAndreas Gohr */ 27*dad993c5SAndreas Gohrclass ControlBag implements IteratorAggregate, Countable 280b3fd2d3SAndreas Gohr{ 290b3fd2d3SAndreas Gohr /** 300b3fd2d3SAndreas Gohr * @var Control[] 310b3fd2d3SAndreas Gohr */ 320b3fd2d3SAndreas Gohr protected $controls; 330b3fd2d3SAndreas Gohr 340b3fd2d3SAndreas Gohr /** 350b3fd2d3SAndreas Gohr * ControlBag constructor. 360b3fd2d3SAndreas Gohr * @param Control ...$controls 370b3fd2d3SAndreas Gohr */ 380b3fd2d3SAndreas Gohr public function __construct(Control ...$controls) 390b3fd2d3SAndreas Gohr { 400b3fd2d3SAndreas Gohr $this->controls = $controls; 410b3fd2d3SAndreas Gohr } 420b3fd2d3SAndreas Gohr 430b3fd2d3SAndreas Gohr /** 440b3fd2d3SAndreas Gohr * Check if a specific control exists by either the OID string or the Control object (strict check). 450b3fd2d3SAndreas Gohr * 460b3fd2d3SAndreas Gohr * @param string|Control $control 470b3fd2d3SAndreas Gohr * @return bool 480b3fd2d3SAndreas Gohr */ 490b3fd2d3SAndreas Gohr public function has($control): bool 500b3fd2d3SAndreas Gohr { 510b3fd2d3SAndreas Gohr if (is_string($control)) { 520b3fd2d3SAndreas Gohr foreach ($this->controls as $ctrl) { 530b3fd2d3SAndreas Gohr if ($ctrl->getTypeOid() === $control) { 540b3fd2d3SAndreas Gohr return true; 550b3fd2d3SAndreas Gohr } 560b3fd2d3SAndreas Gohr } 570b3fd2d3SAndreas Gohr 580b3fd2d3SAndreas Gohr return false; 590b3fd2d3SAndreas Gohr } 600b3fd2d3SAndreas Gohr 61*dad993c5SAndreas Gohr return array_search($control, $this->controls, true) !== false; 620b3fd2d3SAndreas Gohr } 630b3fd2d3SAndreas Gohr 640b3fd2d3SAndreas Gohr /** 650b3fd2d3SAndreas Gohr * Get a control object by the string OID type. If none is found it will return null. Can check first with has. 660b3fd2d3SAndreas Gohr * 670b3fd2d3SAndreas Gohr * @param string $oid 680b3fd2d3SAndreas Gohr * @return null|Control 690b3fd2d3SAndreas Gohr */ 700b3fd2d3SAndreas Gohr public function get(string $oid): ?Control 710b3fd2d3SAndreas Gohr { 720b3fd2d3SAndreas Gohr foreach ($this->controls as $control) { 730b3fd2d3SAndreas Gohr if ($oid === $control->getTypeOid()) { 740b3fd2d3SAndreas Gohr return $control; 750b3fd2d3SAndreas Gohr } 760b3fd2d3SAndreas Gohr } 770b3fd2d3SAndreas Gohr 780b3fd2d3SAndreas Gohr return null; 790b3fd2d3SAndreas Gohr } 800b3fd2d3SAndreas Gohr 810b3fd2d3SAndreas Gohr /** 820b3fd2d3SAndreas Gohr * Add more controls. 830b3fd2d3SAndreas Gohr * 840b3fd2d3SAndreas Gohr * @param Control ...$controls 850b3fd2d3SAndreas Gohr * @return $this 860b3fd2d3SAndreas Gohr */ 870b3fd2d3SAndreas Gohr public function add(Control ...$controls) 880b3fd2d3SAndreas Gohr { 890b3fd2d3SAndreas Gohr foreach ($controls as $control) { 900b3fd2d3SAndreas Gohr $this->controls[] = $control; 910b3fd2d3SAndreas Gohr } 920b3fd2d3SAndreas Gohr 930b3fd2d3SAndreas Gohr return $this; 940b3fd2d3SAndreas Gohr } 950b3fd2d3SAndreas Gohr 960b3fd2d3SAndreas Gohr /** 970b3fd2d3SAndreas Gohr * Set the controls. 980b3fd2d3SAndreas Gohr * 990b3fd2d3SAndreas Gohr * @param Control ...$controls 1000b3fd2d3SAndreas Gohr * @return $this 1010b3fd2d3SAndreas Gohr */ 1020b3fd2d3SAndreas Gohr public function set(Control ...$controls) 1030b3fd2d3SAndreas Gohr { 1040b3fd2d3SAndreas Gohr $this->controls = $controls; 1050b3fd2d3SAndreas Gohr 1060b3fd2d3SAndreas Gohr return $this; 1070b3fd2d3SAndreas Gohr } 1080b3fd2d3SAndreas Gohr 1090b3fd2d3SAndreas Gohr /** 1100b3fd2d3SAndreas Gohr * Remove controls by OID or Control object (strict check). 1110b3fd2d3SAndreas Gohr * 112*dad993c5SAndreas Gohr * @param Control|string ...$controls 1130b3fd2d3SAndreas Gohr * @return $this 1140b3fd2d3SAndreas Gohr */ 1150b3fd2d3SAndreas Gohr public function remove(...$controls) 1160b3fd2d3SAndreas Gohr { 117*dad993c5SAndreas Gohr /** @var Control|string $control */ 1180b3fd2d3SAndreas Gohr foreach ($controls as $control) { 119*dad993c5SAndreas Gohr if (is_string($control)) { 1200b3fd2d3SAndreas Gohr foreach ($this->controls as $i => $ctrl) { 1210b3fd2d3SAndreas Gohr if ($ctrl->getTypeOid() === $control) { 1220b3fd2d3SAndreas Gohr unset($this->controls[$i]); 1230b3fd2d3SAndreas Gohr } 1240b3fd2d3SAndreas Gohr } 1250b3fd2d3SAndreas Gohr } else { 126*dad993c5SAndreas Gohr if (($i = array_search($control, $this->controls, true)) !== false) { 1270b3fd2d3SAndreas Gohr unset($this->controls[$i]); 1280b3fd2d3SAndreas Gohr } 1290b3fd2d3SAndreas Gohr } 1300b3fd2d3SAndreas Gohr } 1310b3fd2d3SAndreas Gohr 1320b3fd2d3SAndreas Gohr return $this; 1330b3fd2d3SAndreas Gohr } 1340b3fd2d3SAndreas Gohr 1350b3fd2d3SAndreas Gohr /** 1360b3fd2d3SAndreas Gohr * Remove all of the controls. 1370b3fd2d3SAndreas Gohr * 1380b3fd2d3SAndreas Gohr * @return $this 1390b3fd2d3SAndreas Gohr */ 1400b3fd2d3SAndreas Gohr public function reset() 1410b3fd2d3SAndreas Gohr { 1420b3fd2d3SAndreas Gohr $this->controls = []; 1430b3fd2d3SAndreas Gohr 1440b3fd2d3SAndreas Gohr return $this; 1450b3fd2d3SAndreas Gohr } 1460b3fd2d3SAndreas Gohr 1470b3fd2d3SAndreas Gohr /** 1480b3fd2d3SAndreas Gohr * Get the array of Control objects. 1490b3fd2d3SAndreas Gohr * 1500b3fd2d3SAndreas Gohr * @return Control[] 1510b3fd2d3SAndreas Gohr */ 1520b3fd2d3SAndreas Gohr public function toArray(): array 1530b3fd2d3SAndreas Gohr { 1540b3fd2d3SAndreas Gohr return $this->controls; 1550b3fd2d3SAndreas Gohr } 1560b3fd2d3SAndreas Gohr 1570b3fd2d3SAndreas Gohr /** 158*dad993c5SAndreas Gohr * @inheritDoc 1590b3fd2d3SAndreas Gohr */ 160*dad993c5SAndreas Gohr public function count(): int 1610b3fd2d3SAndreas Gohr { 162*dad993c5SAndreas Gohr return count($this->controls); 1630b3fd2d3SAndreas Gohr } 1640b3fd2d3SAndreas Gohr 1650b3fd2d3SAndreas Gohr /** 166*dad993c5SAndreas Gohr * @inheritDoc 167*dad993c5SAndreas Gohr * @psalm-return \ArrayIterator<array-key, Control> 1680b3fd2d3SAndreas Gohr */ 169*dad993c5SAndreas Gohr public function getIterator(): Traversable 1700b3fd2d3SAndreas Gohr { 171*dad993c5SAndreas Gohr return new ArrayIterator($this->controls); 1720b3fd2d3SAndreas Gohr } 1730b3fd2d3SAndreas Gohr} 174