From e0e54661f76183684dca66694967a60cbb10f04e Mon Sep 17 00:00:00 2001 From: Paul Yang Date: Thu, 15 Sep 2016 11:09:01 -0700 Subject: Check in php implementation. (#2052) This pull request includes two implementation: C extension and PHP package. Both implementations support encode/decode of singular, repeated and map fields. --- php/src/Google/Protobuf/Internal/RepeatedField.php | 303 +++++++++++++++++++++ 1 file changed, 303 insertions(+) create mode 100644 php/src/Google/Protobuf/Internal/RepeatedField.php (limited to 'php/src/Google/Protobuf/Internal/RepeatedField.php') diff --git a/php/src/Google/Protobuf/Internal/RepeatedField.php b/php/src/Google/Protobuf/Internal/RepeatedField.php new file mode 100644 index 00000000..0dc5d9d2 --- /dev/null +++ b/php/src/Google/Protobuf/Internal/RepeatedField.php @@ -0,0 +1,303 @@ +position = 0; + $this->container = $container; + } + + /** + * Reset the status of the iterator + * + * @return void + */ + public function rewind() + { + $this->position = 0; + } + + /** + * Return the element at the current position. + * + * @return object The element at the current position. + */ + public function current() + { + return $this->container[$this->position]; + } + + /** + * Return the current position. + * + * @return integer The current position. + */ + public function key() + { + return $this->position; + } + + /** + * Move to the next position. + * + * @return void + */ + public function next() + { + ++$this->position; + } + + /** + * Check whether there are more elements to iterate. + * + * @return bool True if there are more elements to iterate. + */ + public function valid() + { + return isset($this->container[$this->position]); + } +} + +/** + * RepeatedField is used by generated protocol message classes to manipulate + * repeated fields. It can be used like native PHP array. + */ +class RepeatedField implements \ArrayAccess, \IteratorAggregate, \Countable +{ + + /** + * @ignore + */ + private $container; + /** + * @ignore + */ + private $type; + /** + * @ignore + */ + private $klass; + + /** + * Constructs an instance of RepeatedField. + * + * @param long $type Type of the stored element. + * @param string $klass Message/Enum class name (message/enum fields only). + * @ignore + */ + public function __construct($type, $klass = null) + { + $this->container = []; + $this->type = $type; + $this->klass = $klass; + } + + /** + * @ignore + */ + public function getType() + { + return $this->type; + } + + /** + * @ignore + */ + public function getClass() + { + return $this->klass; + } + + /** + * Return the element at the given index. + * + * This will also be called for: $ele = $arr[0] + * + * @param long $offset The index of the element to be fetched. + * @return object The stored element at given index. + * @throws ErrorException Invalid type for index. + * @throws ErrorException Non-existing index. + */ + public function offsetGet($offset) + { + return $this->container[$offset]; + } + + /** + * Assign the element at the given index. + * + * This will also be called for: $arr []= $ele and $arr[0] = ele + * + * @param long $offset The index of the element to be assigned. + * @param object $value The element to be assigned. + * @return void + * @throws ErrorException Invalid type for index. + * @throws ErrorException Non-existing index. + * @throws ErrorException Incorrect type of the element. + */ + public function offsetSet($offset, $value) + { + switch ($this->type) { + case GPBType::INT32: + GPBUtil::checkInt32($value); + break; + case GPBType::UINT32: + GPBUtil::checkUint32($value); + break; + case GPBType::INT64: + GPBUtil::checkInt64($value); + break; + case GPBType::UINT64: + GPBUtil::checkUint64($value); + break; + case GPBType::FLOAT: + GPBUtil::checkFloat($value); + break; + case GPBType::DOUBLE: + GPBUtil::checkDouble($value); + break; + case GPBType::BOOL: + GPBUtil::checkBool($value); + break; + case GPBType::STRING: + GPBUtil::checkString($value, true); + break; + case GPBType::MESSAGE: + GPBUtil::checkMessage($value, $this->klass); + break; + default: + break; + } + if (is_null($offset)) { + $this->container[] = $value; + } else { + $count = count($this->container); + if (!is_numeric($offset) || $offset < 0 || $offset >= $count) { + trigger_error( + "Cannot modify element at the given index", + E_USER_ERROR); + return; + } + $this->container[$offset] = $value; + } + } + + /** + * Remove the element at the given index. + * + * This will also be called for: unset($arr) + * + * @param long $offset The index of the element to be removed. + * @return void + * @throws ErrorException Invalid type for index. + * @throws ErrorException The element to be removed is not at the end of the + * RepeatedField. + */ + public function offsetUnset($offset) + { + $count = count($this->container); + if (!is_numeric($offset) || $count === 0 || $offset !== $count - 1) { + trigger_error( + "Cannot remove element at the given index", + E_USER_ERROR); + return; + } + array_pop($this->container); + } + + /** + * Check the existence of the element at the given index. + * + * This will also be called for: isset($arr) + * + * @param long $offset The index of the element to be removed. + * @return bool True if the element at the given offset exists. + * @throws ErrorException Invalid type for index. + */ + public function offsetExists($offset) + { + return isset($this->container[$offset]); + } + + /** + * @ignore + */ + public function getIterator() + { + return new RepeatedFieldIter($this->container); + } + + /** + * Return the number of stored elements. + * + * This will also be called for: count($arr) + * + * @return integer The number of stored elements. + */ + public function count() + { + return count($this->container); + } +} -- cgit v1.2.3