XRL  3.0.0
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 gmp_strval($this->value);
27  }
28 
30  public function set($value)
31  {
32  $size = static::INTEGER_BITS;
33 
34  // Versions before PHP 5.6 used resources to represent big numbers
35  // while new versions use objects instead.
36  if ((is_resource($value) && get_resource_type($value) === 'GMP integer') ||
37  ($value instanceof \GMP)) {
38  // It is already a GMP integer.
39  } else {
40  $value = @gmp_init($value, 10);
41  }
42  if ($value === false) {
43  throw new \InvalidArgumentException("Expected a signed $size-bits integer value");
44  }
45 
46  // Check type bounds.
47  $binval = gmp_strval($value, 2);
48  if (!strncmp($binval, '-1', 2)) {
49  $binval = (string) substr($binval, 2);
50  }
51  if (strlen($binval) >= $size) {
52  throw new \InvalidArgumentException("Expected a signed $size-bits integer value");
53  }
54 
55  $this->value = ($size <= 32) ? gmp_intval($value) : $value;
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, gmp_strval($this->value));
65  }
66  return $writer->writeElement(static::XMLRPC_TYPE, gmp_strval($this->value));
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.