XRL  latest
Simple XML-RPC Library (both client and server)
AbstractInteger.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 
22 {
24  public function __toString()
25  {
26  return (string) $this->value;
27  }
28 
30  public function set($value)
31  {
32  $size = static::INTEGER_BITS;
33 
34  if (is_object($value) && $value instanceof \GMP) {
35  $value = gmp_strval($value, 10);
36  }
37 
38  // If the value is a string, make sure it can be converted
39  // without triggering an overflow/underflow.
40  // This includes values obtained from GMP objects/resources too.
41  if (is_string($value) && $value === (string) (int) $value) {
42  $value = (int) $value;
43  }
44 
45  // base_convert() already takes care of the one's complement
46  // for us when dealing with negative values.
47  if (is_int($value) && strlen(base_convert($value, 10, 2)) < $size) {
48  $this->value = $value;
49  } else {
50  // Either the value used an incompatible type,
51  // was a string with an invalid value,
52  // or was a string or an integer whose value
53  // could not be held by the given integer size.
54  throw new \InvalidArgumentException("A $size-bit signed integer was expected");
55  }
56  }
57 
59  public function write(\XMLWriter $writer, \DateTimeZone $timezone, $stringTag)
60  {
61  if (strpos(static::XMLRPC_TYPE, '}') !== false) {
62  list($ns, $tagName) = explode('}', static::XMLRPC_TYPE, 2);
63  $ns = (string) substr($ns, 1);
64  return $writer->writeElementNS('ex', $tagName, $ns, (string) $this);
65  }
66  return $writer->writeElement(static::XMLRPC_TYPE, (string) $this);
67  }
68 }
write(\XMLWriter $writer,\DateTimeZone $timezone, $stringTag)
$value
Current value associated with this object.
A class representing an abstract XML-RPC type.
Abstract class for fixed-length integer types.