/**
  * ArrayList::map() extension
  *
  * @param   lang.types.ArrayList self
  * @return  lang.types.ArrayList mapped
  */
 public static function map(ArrayList $self, $block)
 {
     $mapped = ArrayList::newInstance($self->length);
     foreach ($self->values as $i => $value) {
         $mapped[$i] = $block($value);
     }
     return $mapped;
 }
 public function emptyArray()
 {
     $this->assertEquals(\lang\types\ArrayList::newInstance(0), Arrays::$EMPTY);
 }
 /**
  * Main method
  *
  * @param   string[] args
  */
 public static function main($args)
 {
     $sorted = ArrayList::newInstance($args)->sorted();
     Console::writeLine('(new ArrayList([', implode(', ', $args), ']))->sorted()= ', $sorted);
 }
 /**
  * Deserialize a single node
  *
  * @param   xml.Node child
  * @param   string context default NULL
  * @param   webservices.soap.xp.XPSoapMapping mapping
  * @return  var result
  */
 public function unmarshall($child, $context = null)
 {
     // DEBUG Console::writeLine('Unmarshalling ', $child->name, ' (', var_export($child->attribute, 1), ') >>> ', $child->content, '<<<', "\n"); // DEBUG
     if ($child->hasAttribute($this->namespaces[XMLNS_XSI] . ':null') or $child->hasAttribute($this->namespaces[XMLNS_XSI] . ':nil')) {
         return null;
     }
     // References
     if ($child->hasAttribute('href')) {
         $body = $this->_bodyElement();
         foreach (array_keys($body->getChildren()) as $idx) {
             if (0 != strcasecmp(@$body->nodeAt($idx)->getAttribute('id'), substr($child->getAttribute('href'), 1))) {
                 continue;
             }
             // Create a copy and pass name to it
             $c = $body->nodeAt($idx);
             $c->setName($child->getName());
             return $this->unmarshall($c, $context);
             break;
         }
     }
     // Update namespaces list
     foreach ($child->getAttributes() as $key => $val) {
         if (0 == strncmp('xmlns:', $key, 6)) {
             $this->namespaces[$val] = substr($key, 6);
         }
     }
     // Type dependant
     if (!$child->hasAttribute($this->namespaces[XMLNS_XSI] . ':type') || !preg_match('#^([^:]+):([^\\[]+)(\\[[0-9+]\\])?$#', $child->getAttribute($this->namespaces[XMLNS_XSI] . ':type'), $regs)) {
         // E.g.: SOAP-ENV:Fault
         $regs = [0, 'xsd', 'string'];
     }
     // SOAP-ENC:arrayType="xsd:anyType[4]"
     if ($child->hasAttribute($this->namespaces[XMLNS_SOAPENC] . ':arrayType')) {
         $regs[1] = $child->getAttribute($this->namespaces[XMLNS_SOAPENC] . ':arrayType');
         $regs[2] = 'Array';
     }
     switch (strtolower($regs[2])) {
         case 'array':
             // Check for specific type information
             @(list($ns, $typeSpec) = explode(':', $regs[1]));
             if (2 == sscanf($typeSpec, '%[^[][%d]', $childType, $length) && 'anyType' != $childType) {
                 // Arrays of XP objects
                 // ~~~~~~~~~~~~~~~~~~~~
                 // <item
                 //  xsi:type="SOAP-ENC:Array"
                 //  xmlns:xp="http://xp-framework.net/xmlns/xp"
                 //  SOAP-ENC:arrayType="xp:de.schlund.db.irc.IrcChannel[2]"
                 // >
                 //
                 // vs.
                 //
                 // Arrays of other types
                 // ~~~~~~~~~~~~~~~~~~~~~
                 // <item SOAP-ENC:arrayType="xsd:int[4]"/>
                 if ($this->namespaces[XMLNS_XP] == $ns) {
                     $result = ArrayList::newInstance($length);
                     foreach ($this->_recurseData($child, false, 'ARRAY') as $i => $val) {
                         $result[$i] = $val;
                     }
                     break;
                 } else {
                     for ($i = 0; $i < $length; ++$i) {
                         $child->nodeAt($i)->setAttribute($this->namespaces[XMLNS_XSI] . ':type', $ns . ':' . $childType);
                     }
                 }
             }
             // Break missing intentionally
         // Break missing intentionally
         case 'vector':
             $result = $this->_recurseData($child, false, 'ARRAY');
             break;
         case 'map':
             // <old_data xmlns:ns4="http://xml.apache.org/xml-soap" xsi:type="ns4:Map">
             // <item>
             // <key xsi:type="xsd:string">Nachname</key>
             // <value xsi:type="xsd:string">Braun</value>
             // </item>
             // <item>
             // <key xsi:type="xsd:string">PLZ</key>
             // <value xsi:type="xsd:string">76135</value>
             // </item>
             // <item>
             if (!$child->hasChildren()) {
                 break;
             }
             foreach ($child->getChildren() as $item) {
                 $key = $item->nodeAt(0)->getContent($this->getEncoding(), $this->namespaces);
                 $result[$key] = !$item->nodeAt(1)->hasChildren() && !$item->nodeAt(1)->hasAttribute('href') ? $item->nodeAt(1)->getContent($this->getEncoding(), $this->namespaces) : $this->unmarshall($item->nodeAt(1), 'MAP');
             }
             break;
         case 'soapstruct':
         case 'struct':
         case 'ur-type':
             $result = $this->_recurseData($child, true, 'HASHMAP');
             break;
         default:
             if ($child->hasChildren()) {
                 if ($this->namespaces[XMLNS_XSD] == $regs[1]) {
                     $result = $this->_recurseData($child, true, 'STRUCT');
                     break;
                 }
                 // Check for mapping
                 //
                 // XP objects
                 // ~~~~~~~~~~
                 // <item xmlns:xp="http://xp-framework.net/xmlns/xp" xsi:type="xp:de.schlund.db.irc.IrcChannel"/>
                 //
                 // For other objects, check SOAPMapping registry
                 if (isset($this->namespaces[XMLNS_XP]) && $this->namespaces[XMLNS_XP] == $regs[1]) {
                     try {
                         $xpclass = \lang\XPClass::forName($regs[2]);
                     } catch (\lang\ClassNotFoundException $e) {
                         $xpclass = null;
                     }
                 } else {
                     // TBD: Fix mapping passing when SOAPMessage was build from
                     // SOAPTransport::retrieve() function which currently doesn't
                     // care about mapping and $mapping is just an empty array.
                     if ($this->mapping) {
                         $xpclass = $this->mapping->classFor(new \xml\QName(array_search($regs[1], $this->namespaces), $regs[2]));
                     } else {
                         $xpclass = null;
                     }
                 }
                 if ($xpclass) {
                     $result = $xpclass->newInstance();
                 } else {
                     $result = new \lang\Object();
                     $result->__qname = $regs[2];
                 }
                 foreach ($this->_recurseData($child, true, 'OBJECT') as $key => $val) {
                     $result->{$key} = $val;
                 }
                 break;
             }
             $result = $child->getContent($this->getEncoding(), $this->namespaces);
     }
     return $result;
 }
 public function newInstance_array()
 {
     $a = ArrayList::newInstance([1, 2, 3, 4]);
     $this->assertEquals(4, $a->length);
     $this->assertEquals(1, $a[0]);
     $this->assertEquals(2, $a[1]);
     $this->assertEquals(3, $a[2]);
     $this->assertEquals(4, $a[3]);
 }
Exemple #6
0
 /**
  * Boxes a type - that is, turns primitives into Generics
  *
  * @deprecated Wrapper types will move to their own library
  * @param   var in
  * @return  lang.Generic the Generic if not already generic
  * @throws  lang.IllegalArgumentException in case in cannot be boxed.
  */
 public static function boxed($in)
 {
     if (null === $in || $in instanceof Generic) {
         return $in;
     }
     $t = gettype($in);
     if ('string' === $t) {
         return new \lang\types\String($in);
     }
     if ('integer' === $t) {
         return new \lang\types\Integer($in);
     }
     if ('double' === $t) {
         return new \lang\types\Double($in);
     }
     if ('boolean' === $t) {
         return new \lang\types\Boolean($in);
     }
     if ('array' === $t) {
         return \lang\types\ArrayList::newInstance($in);
     }
     // deprecated
     throw new IllegalArgumentException('Cannot box ' . \xp::typeOf($in));
 }
Exemple #7
0
 public function empty_array()
 {
     $this->assertEquals(ArrayList::newInstance(0), Arrays::$EMPTY);
 }