XRL  latest
Simple XML-RPC Library (both client and server)
ArrayType.php
1 <?php
2 /*
3  * This file is part of XRL, a simple XML-RPC Library for PHP.
4  *
5  * Copyright (c) 2012, XRL Team. All rights reserved.
6  * XRL is licensed under the 3-clause BSD License.
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace fpoirotte\XRL\Types;
13 
21 {
23  public function get()
24  {
25  $res = array();
26  foreach ($this->value as $val) {
27  $res[] = $val->get();
28  }
29  return $res;
30  }
31 
33  public function set($value)
34  {
35  if (!is_array($value)) {
36  throw new \InvalidArgumentException('An indexed array was expected');
37  }
38 
39  $keys = array_keys($value);
40  $length = count($value);
41 
42  // Empty arrays must be handled with care.
43  if (!$length) {
44  $numeric = array();
45  } else {
46  $numeric = range(0, $length - 1);
47  sort($keys);
48  }
49 
50  // Detect associative arrays (which are invalid for this type).
51  if ($keys !== $numeric) {
52  throw new \InvalidArgumentException('An indexed array was expected');
53  }
54 
55  foreach ($value as $val) {
56  if (!($val instanceof \fpoirotte\XRL\Types\AbstractType)) {
57  throw new \InvalidArgumentException('A valid XML-RPC type was expected');
58  }
59  }
60 
61  $this->value = $value;
62  }
63 
65  public function write(\XMLWriter $writer, \DateTimeZone $timezone, $stringTag)
66  {
67  $writer->startElement('array');
68  $writer->startElement('data');
69  foreach ($this->value as $val) {
70  $writer->startElement('value');
71  $val->write($writer, $timezone, $stringTag);
72  $writer->endElement();
73  }
74  $writer->endElement();
75  $writer->endElement();
76  }
77 
79  protected static function parse($value, \DateTimeZone $timezone = null)
80  {
81  return array();
82  }
83 
96  public function offsetSet($offset, $value)
97  {
98  if ($offset === null) {
99  $this->value[] = $value;
100  return;
101  } elseif (!is_int($offset)) {
102  throw new \InvalidArgumentException('An integer offset was expected');
103  } elseif (!array_key_exists($offset, $this->value) && $offset !== count($this->value)) {
104  throw new \InvalidArgumentException('Cannot set arbitrary offset in array');
105  }
106  $this->value[$offset] = $value;
107  }
108 
118  public function offsetUnset($offset)
119  {
120  unset($this->value[$offset]);
121  // Force reindexation.
122  $this->value = array_values($value);
123  }
124 
132  public function key()
133  {
134  return $this->index;
135  }
136 }
write(\XMLWriter $writer,\DateTimeZone $timezone, $stringTag)
Definition: ArrayType.php:65
$index
Current index in the collection.
$value
Current value associated with this object.
A class representing an abstract XML-RPC type.
An abstract XML-RPC type representing a collection of values.
The XML-RPC "array" type.
Definition: ArrayType.php:20
offsetSet($offset, $value)
Definition: ArrayType.php:96
static parse($value,\DateTimeZone $timezone=null)
Definition: ArrayType.php:79