/** * Reads the array in the next value. * * @param bool $returnObject Whether to return AMQPArray instance instead of plain array * @return array|AMQPArray */ public function read_array($returnObject = false) { $this->bitcount = $this->bits = 0; // Determine array length and its end position $arrayLength = $this->read_php_int(); $endOffset = $this->offset + $arrayLength; $result = $returnObject ? new AMQPArray() : array(); // Read values until we reach the end of the array while ($this->offset < $endOffset) { $fieldType = AMQPAbstractCollection::getDataTypeForSymbol($this->rawread(1)); $fieldValue = $this->read_value($fieldType, $returnObject); $returnObject ? $result->push($fieldValue, $fieldType) : ($result[] = $fieldValue); } return $result; }
/** * Write PHP array, as table. Input array format: keys are strings, * values are (type,value) tuples. * * @param AMQPTable|array $d Instance of AMQPTable or PHP array WITH format hints (unlike write_array()) * @return $this * @throws \PhpAmqpLib\Exception\AMQPInvalidArgumentException */ public function write_table($d) { $typeIsSym = !$d instanceof AMQPTable; //purely for back-compat purposes $table_data = new AMQPWriter(); foreach ($d as $k => $va) { list($ftype, $v) = $va; $table_data->write_shortstr($k); $table_data->write_value($typeIsSym ? AMQPAbstractCollection::getDataTypeForSymbol($ftype) : $ftype, $v); } $table_data = $table_data->getvalue(); $this->write_long(mb_strlen($table_data, 'ASCII')); $this->write($table_data); return $this; }
public function testIterator() { $d = array('a' => 1, 'b' => -2147, 'c' => array('foo' => 'bar'), 'd' => true, 'e' => false); $ed = array('a' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), 1), 'b' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('I'), -2147), 'c' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('F'), array('foo' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('S'), 'bar'))), 'd' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), true), 'e' => array(Wire\AMQPAbstractCollection::getDataTypeForSymbol('t'), false)); $this->setProtoVersion(Wire\AMQPAbstractCollection::PROTOCOL_091); $a = new Wire\AMQPTable($d); foreach ($a as $key => $val) { if (!isset($d[$key])) { $this->fail('Unknown key: ' . $key); } $this->assertEquals($ed[$key], $val[1] instanceof Wire\AMQPAbstractCollection ? array($val[0], $this->getEncodedRawData($val[1])) : $val); } }