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\Entry; 130b3fd2d3SAndreas Gohr 14*dad993c5SAndreas Gohruse ArrayIterator; 15*dad993c5SAndreas Gohruse Countable; 16*dad993c5SAndreas Gohruse IteratorAggregate; 17*dad993c5SAndreas Gohruse Traversable; 18*dad993c5SAndreas Gohruse function count; 19*dad993c5SAndreas Gohruse function sort; 20*dad993c5SAndreas Gohr 210b3fd2d3SAndreas Gohr/** 220b3fd2d3SAndreas Gohr * Represents a collection of attribute options. 230b3fd2d3SAndreas Gohr * 240b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com> 250b3fd2d3SAndreas Gohr */ 26*dad993c5SAndreas Gohrclass Options implements Countable, IteratorAggregate 270b3fd2d3SAndreas Gohr{ 280b3fd2d3SAndreas Gohr /** 290b3fd2d3SAndreas Gohr * @var Option[] 300b3fd2d3SAndreas Gohr */ 310b3fd2d3SAndreas Gohr protected $options; 320b3fd2d3SAndreas Gohr 330b3fd2d3SAndreas Gohr /** 340b3fd2d3SAndreas Gohr * @param string|Option ...$options 350b3fd2d3SAndreas Gohr */ 360b3fd2d3SAndreas Gohr public function __construct(...$options) 370b3fd2d3SAndreas Gohr { 380b3fd2d3SAndreas Gohr $this->set(...$options); 390b3fd2d3SAndreas Gohr } 400b3fd2d3SAndreas Gohr 410b3fd2d3SAndreas Gohr /** 420b3fd2d3SAndreas Gohr * @param string|Option ...$options 430b3fd2d3SAndreas Gohr * @return $this 440b3fd2d3SAndreas Gohr */ 450b3fd2d3SAndreas Gohr public function add(...$options) 460b3fd2d3SAndreas Gohr { 470b3fd2d3SAndreas Gohr foreach ($options as $option) { 480b3fd2d3SAndreas Gohr $this->options[] = $option instanceof Option ? $option : new Option($option); 490b3fd2d3SAndreas Gohr } 500b3fd2d3SAndreas Gohr 510b3fd2d3SAndreas Gohr return $this; 520b3fd2d3SAndreas Gohr } 530b3fd2d3SAndreas Gohr 540b3fd2d3SAndreas Gohr /** 550b3fd2d3SAndreas Gohr * @param string|Option ...$options 560b3fd2d3SAndreas Gohr * @return $this 570b3fd2d3SAndreas Gohr */ 580b3fd2d3SAndreas Gohr public function set(...$options) 590b3fd2d3SAndreas Gohr { 600b3fd2d3SAndreas Gohr $this->options = []; 610b3fd2d3SAndreas Gohr foreach ($options as $i => $option) { 620b3fd2d3SAndreas Gohr if ($option instanceof Option) { 630b3fd2d3SAndreas Gohr $this->options[] = $option; 640b3fd2d3SAndreas Gohr } else { 650b3fd2d3SAndreas Gohr $this->options[] = new Option($option); 660b3fd2d3SAndreas Gohr } 670b3fd2d3SAndreas Gohr } 680b3fd2d3SAndreas Gohr 690b3fd2d3SAndreas Gohr return $this; 700b3fd2d3SAndreas Gohr } 710b3fd2d3SAndreas Gohr 720b3fd2d3SAndreas Gohr /** 730b3fd2d3SAndreas Gohr * @param string|Option $option 740b3fd2d3SAndreas Gohr * @return bool 750b3fd2d3SAndreas Gohr */ 760b3fd2d3SAndreas Gohr public function has($option): bool 770b3fd2d3SAndreas Gohr { 780b3fd2d3SAndreas Gohr $option = $option instanceof Option ? $option : new Option($option); 790b3fd2d3SAndreas Gohr 800b3fd2d3SAndreas Gohr foreach ($this->options as $opt) { 810b3fd2d3SAndreas Gohr if ($opt->equals($option)) { 820b3fd2d3SAndreas Gohr return true; 830b3fd2d3SAndreas Gohr } 840b3fd2d3SAndreas Gohr } 850b3fd2d3SAndreas Gohr 860b3fd2d3SAndreas Gohr return false; 870b3fd2d3SAndreas Gohr } 880b3fd2d3SAndreas Gohr 890b3fd2d3SAndreas Gohr /** 900b3fd2d3SAndreas Gohr * @param string|Option ...$options 910b3fd2d3SAndreas Gohr * @return $this 920b3fd2d3SAndreas Gohr */ 930b3fd2d3SAndreas Gohr public function remove(...$options) 940b3fd2d3SAndreas Gohr { 950b3fd2d3SAndreas Gohr foreach ($options as $option) { 960b3fd2d3SAndreas Gohr $option = $option instanceof Option ? $option : new Option($option); 970b3fd2d3SAndreas Gohr foreach ($this->options as $i => $opt) { 980b3fd2d3SAndreas Gohr if ($opt->equals($option)) { 990b3fd2d3SAndreas Gohr unset($this->options[$i]); 1000b3fd2d3SAndreas Gohr } 1010b3fd2d3SAndreas Gohr } 1020b3fd2d3SAndreas Gohr } 1030b3fd2d3SAndreas Gohr 1040b3fd2d3SAndreas Gohr return $this; 1050b3fd2d3SAndreas Gohr } 1060b3fd2d3SAndreas Gohr 1070b3fd2d3SAndreas Gohr /** 1080b3fd2d3SAndreas Gohr * Retrieve the first option, if it exists. 1090b3fd2d3SAndreas Gohr * 1100b3fd2d3SAndreas Gohr * @return Option|null 1110b3fd2d3SAndreas Gohr */ 1120b3fd2d3SAndreas Gohr public function first(): ?Option 1130b3fd2d3SAndreas Gohr { 1140b3fd2d3SAndreas Gohr $option = reset($this->options); 1150b3fd2d3SAndreas Gohr 1160b3fd2d3SAndreas Gohr return $option === false ? null : $option; 1170b3fd2d3SAndreas Gohr } 1180b3fd2d3SAndreas Gohr 1190b3fd2d3SAndreas Gohr /** 1200b3fd2d3SAndreas Gohr * Retrieve the last option, if it exists. 1210b3fd2d3SAndreas Gohr * 1220b3fd2d3SAndreas Gohr * @return Option|null 1230b3fd2d3SAndreas Gohr */ 1240b3fd2d3SAndreas Gohr public function last(): ?Option 1250b3fd2d3SAndreas Gohr { 1260b3fd2d3SAndreas Gohr $option = end($this->options); 1270b3fd2d3SAndreas Gohr reset($this->options); 1280b3fd2d3SAndreas Gohr 1290b3fd2d3SAndreas Gohr return $option === false ? null : $option; 1300b3fd2d3SAndreas Gohr } 1310b3fd2d3SAndreas Gohr 1320b3fd2d3SAndreas Gohr /** 1330b3fd2d3SAndreas Gohr * @param bool $sortedlc Used for comparison, as both case and order of options are irrelevant for options equality. 1340b3fd2d3SAndreas Gohr * @return string 1350b3fd2d3SAndreas Gohr */ 1360b3fd2d3SAndreas Gohr public function toString(bool $sortedlc = false): string 1370b3fd2d3SAndreas Gohr { 1380b3fd2d3SAndreas Gohr $opts = $this->options; 1390b3fd2d3SAndreas Gohr if ($sortedlc) { 140*dad993c5SAndreas Gohr sort($opts); 1410b3fd2d3SAndreas Gohr } 1420b3fd2d3SAndreas Gohr 1430b3fd2d3SAndreas Gohr $options = ''; 1440b3fd2d3SAndreas Gohr foreach ($opts as $option) { 1450b3fd2d3SAndreas Gohr $options .= ($options === '') ? $option->toString($sortedlc) : ';' . $option->toString($sortedlc); 1460b3fd2d3SAndreas Gohr } 1470b3fd2d3SAndreas Gohr 1480b3fd2d3SAndreas Gohr return $options; 1490b3fd2d3SAndreas Gohr } 1500b3fd2d3SAndreas Gohr 1510b3fd2d3SAndreas Gohr /** 152*dad993c5SAndreas Gohr * @return Option[] 153*dad993c5SAndreas Gohr * @psalm-return array<array-key, Option> 1540b3fd2d3SAndreas Gohr */ 1550b3fd2d3SAndreas Gohr public function toArray(): array 1560b3fd2d3SAndreas Gohr { 1570b3fd2d3SAndreas Gohr return $this->options; 1580b3fd2d3SAndreas Gohr } 1590b3fd2d3SAndreas Gohr 1600b3fd2d3SAndreas Gohr /** 1610b3fd2d3SAndreas Gohr * @return string 1620b3fd2d3SAndreas Gohr */ 1630b3fd2d3SAndreas Gohr public function __toString() 1640b3fd2d3SAndreas Gohr { 1650b3fd2d3SAndreas Gohr return $this->toString(); 1660b3fd2d3SAndreas Gohr } 1670b3fd2d3SAndreas Gohr 1680b3fd2d3SAndreas Gohr /** 169*dad993c5SAndreas Gohr * @inheritDoc 170*dad993c5SAndreas Gohr * @psalm-return ArrayIterator<array-key, Option> 1710b3fd2d3SAndreas Gohr */ 172*dad993c5SAndreas Gohr public function getIterator(): Traversable 1730b3fd2d3SAndreas Gohr { 174*dad993c5SAndreas Gohr return new ArrayIterator($this->options); 1750b3fd2d3SAndreas Gohr } 1760b3fd2d3SAndreas Gohr 1770b3fd2d3SAndreas Gohr /** 178*dad993c5SAndreas Gohr * @inheritDoc 179*dad993c5SAndreas Gohr * @psalm-return 0|positive-int 1800b3fd2d3SAndreas Gohr */ 181*dad993c5SAndreas Gohr public function count(): int 1820b3fd2d3SAndreas Gohr { 183*dad993c5SAndreas Gohr return count($this->options); 1840b3fd2d3SAndreas Gohr } 1850b3fd2d3SAndreas Gohr} 186