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 140b3fd2d3SAndreas Gohruse FreeDSx\Asn1\Asn1; 15*dad993c5SAndreas Gohruse FreeDSx\Asn1\Exception\EncoderException; 16*dad993c5SAndreas Gohruse FreeDSx\Asn1\Exception\PartialPduException; 170b3fd2d3SAndreas Gohruse FreeDSx\Asn1\Type\AbstractType; 180b3fd2d3SAndreas Gohruse FreeDSx\Asn1\Type\IntegerType; 190b3fd2d3SAndreas Gohruse FreeDSx\Asn1\Type\OctetStringType; 200b3fd2d3SAndreas Gohruse FreeDSx\Asn1\Type\SequenceType; 210b3fd2d3SAndreas Gohruse FreeDSx\Ldap\Exception\ProtocolException; 220b3fd2d3SAndreas Gohr 230b3fd2d3SAndreas Gohr/** 240b3fd2d3SAndreas Gohr * Represents a paged results control request value. RFC 2696. 250b3fd2d3SAndreas Gohr * 260b3fd2d3SAndreas Gohr * realSearchControlValue ::= SEQUENCE { 270b3fd2d3SAndreas Gohr * size INTEGER (0..maxInt), 280b3fd2d3SAndreas Gohr * -- requested page size from client 290b3fd2d3SAndreas Gohr * -- result set size estimate from server 300b3fd2d3SAndreas Gohr * cookie OCTET STRING } 310b3fd2d3SAndreas Gohr * 320b3fd2d3SAndreas Gohr * @author Chad Sikorra <Chad.Sikorra@gmail.com> 330b3fd2d3SAndreas Gohr */ 340b3fd2d3SAndreas Gohrclass PagingControl extends Control 350b3fd2d3SAndreas Gohr{ 360b3fd2d3SAndreas Gohr /** 370b3fd2d3SAndreas Gohr * @var string 380b3fd2d3SAndreas Gohr */ 390b3fd2d3SAndreas Gohr protected $cookie; 400b3fd2d3SAndreas Gohr 410b3fd2d3SAndreas Gohr /** 420b3fd2d3SAndreas Gohr * @var int 430b3fd2d3SAndreas Gohr */ 440b3fd2d3SAndreas Gohr protected $size; 450b3fd2d3SAndreas Gohr 460b3fd2d3SAndreas Gohr /** 470b3fd2d3SAndreas Gohr * @param int $size 480b3fd2d3SAndreas Gohr * @param string $cookie 490b3fd2d3SAndreas Gohr */ 500b3fd2d3SAndreas Gohr public function __construct(int $size, string $cookie = '') 510b3fd2d3SAndreas Gohr { 520b3fd2d3SAndreas Gohr $this->size = $size; 530b3fd2d3SAndreas Gohr $this->cookie = $cookie; 540b3fd2d3SAndreas Gohr parent::__construct(self::OID_PAGING); 550b3fd2d3SAndreas Gohr } 560b3fd2d3SAndreas Gohr 570b3fd2d3SAndreas Gohr /** 580b3fd2d3SAndreas Gohr * @return string 590b3fd2d3SAndreas Gohr */ 600b3fd2d3SAndreas Gohr public function getCookie(): string 610b3fd2d3SAndreas Gohr { 620b3fd2d3SAndreas Gohr return $this->cookie; 630b3fd2d3SAndreas Gohr } 640b3fd2d3SAndreas Gohr 650b3fd2d3SAndreas Gohr /** 660b3fd2d3SAndreas Gohr * @return int 670b3fd2d3SAndreas Gohr */ 680b3fd2d3SAndreas Gohr public function getSize(): int 690b3fd2d3SAndreas Gohr { 700b3fd2d3SAndreas Gohr return $this->size; 710b3fd2d3SAndreas Gohr } 720b3fd2d3SAndreas Gohr 730b3fd2d3SAndreas Gohr /** 740b3fd2d3SAndreas Gohr * @param string $cookie 750b3fd2d3SAndreas Gohr * @return $this 760b3fd2d3SAndreas Gohr */ 770b3fd2d3SAndreas Gohr public function setCookie(string $cookie) 780b3fd2d3SAndreas Gohr { 790b3fd2d3SAndreas Gohr $this->cookie = $cookie; 800b3fd2d3SAndreas Gohr 810b3fd2d3SAndreas Gohr return $this; 820b3fd2d3SAndreas Gohr } 830b3fd2d3SAndreas Gohr 840b3fd2d3SAndreas Gohr /** 850b3fd2d3SAndreas Gohr * @param int $size 860b3fd2d3SAndreas Gohr * @return $this 870b3fd2d3SAndreas Gohr */ 880b3fd2d3SAndreas Gohr public function setSize(int $size) 890b3fd2d3SAndreas Gohr { 900b3fd2d3SAndreas Gohr $this->size = $size; 910b3fd2d3SAndreas Gohr 920b3fd2d3SAndreas Gohr return $this; 930b3fd2d3SAndreas Gohr } 940b3fd2d3SAndreas Gohr 950b3fd2d3SAndreas Gohr /** 96*dad993c5SAndreas Gohr * {@inheritDoc} 97*dad993c5SAndreas Gohr * @param AbstractType $type 98*dad993c5SAndreas Gohr * @return Control 99*dad993c5SAndreas Gohr * @throws EncoderException 100*dad993c5SAndreas Gohr * @throws PartialPduException 1010b3fd2d3SAndreas Gohr */ 1020b3fd2d3SAndreas Gohr public static function fromAsn1(AbstractType $type) 1030b3fd2d3SAndreas Gohr { 1040b3fd2d3SAndreas Gohr $paging = self::decodeEncodedValue($type); 1050b3fd2d3SAndreas Gohr if (!$paging instanceof SequenceType) { 1060b3fd2d3SAndreas Gohr throw new ProtocolException('A paged control value must be a sequence type with 2 children.'); 1070b3fd2d3SAndreas Gohr } 1080b3fd2d3SAndreas Gohr $count = $paging->getChild(0); 1090b3fd2d3SAndreas Gohr $cookie = $paging->getChild(1); 1100b3fd2d3SAndreas Gohr if (!$count instanceof IntegerType) { 1110b3fd2d3SAndreas Gohr throw new ProtocolException('A paged control value sequence 0 must be an integer type.'); 1120b3fd2d3SAndreas Gohr } 1130b3fd2d3SAndreas Gohr if (!$cookie instanceof OctetStringType) { 1140b3fd2d3SAndreas Gohr throw new ProtocolException('A paged control value sequence 1 must be an octet string type.'); 1150b3fd2d3SAndreas Gohr } 1160b3fd2d3SAndreas Gohr $control = new self($count->getValue(), $cookie->getValue()); 1170b3fd2d3SAndreas Gohr 1180b3fd2d3SAndreas Gohr return self::mergeControlData($control, $type); 1190b3fd2d3SAndreas Gohr } 1200b3fd2d3SAndreas Gohr 1210b3fd2d3SAndreas Gohr /** 1220b3fd2d3SAndreas Gohr * {@inheritdoc} 1230b3fd2d3SAndreas Gohr */ 1240b3fd2d3SAndreas Gohr public function toAsn1(): AbstractType 1250b3fd2d3SAndreas Gohr { 1260b3fd2d3SAndreas Gohr $this->controlValue = Asn1::sequence( 1270b3fd2d3SAndreas Gohr Asn1::integer($this->size), 1280b3fd2d3SAndreas Gohr Asn1::octetString($this->cookie) 1290b3fd2d3SAndreas Gohr ); 1300b3fd2d3SAndreas Gohr 1310b3fd2d3SAndreas Gohr return parent::toAsn1(); 1320b3fd2d3SAndreas Gohr } 1330b3fd2d3SAndreas Gohr} 134