Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove wrapper types #118

Merged
merged 19 commits into from
Jan 23, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ before_script:
- echo "date.timezone=Europe/Berlin" >> xp.ini

script:
- (EXCD=0; for i in `ls -1 src/test/config/unittest/*.ini | grep -v not-on-php-$TRAVIS_PHP_VERSION`; do echo "---> $i"; ./xp -cp test.xar xp.unittest.Runner $i; RES=$?; if [ $RES -ne 0 ]; then EXCD=$RES; fi; done; exit $EXCD;)
- (EXCD=0; for i in `ls -1 src/test/config/unittest/*.ini`; do echo "---> $i"; ./xp -cp test.xar xp.unittest.Runner $i; RES=$?; if [ $RES -ne 0 ]; then EXCD=$RES; fi; done; exit $EXCD;)
10 changes: 2 additions & 8 deletions src/main/php/io/streams/TextWriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,7 @@ public function getNewLine() {
* @param string text
*/
public function write($text) {
$this->stream->write($text instanceof \lang\types\String || $text instanceof \lang\types\Character
? $text->getBytes($this->charset)
: iconv(\xp::ENCODING, $this->charset, $text)
);
$this->stream->write(iconv(\xp::ENCODING, $this->charset, $text));
}

/**
Expand All @@ -97,9 +94,6 @@ public function write($text) {
* @param string text
*/
public function writeLine($text= '') {
$this->stream->write(($text instanceof \lang\types\String || $text instanceof \lang\types\Character
? $text->getBytes($this->charset)
: iconv(\xp::ENCODING, $this->charset, $text)
).$this->newLine);
$this->stream->write(iconv(\xp::ENCODING, $this->charset, $text).$this->newLine);
}
}
86 changes: 4 additions & 82 deletions src/main/php/lang/Primitive.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,61 +25,6 @@ static function __static() {
self::$BOOL= new self('bool', false);
}

/**
* Returns the wrapper class for this primitive
*
* @deprecated Wrapper types will move to their own library
* @see http://en.wikipedia.org/wiki/Wrapper_class
* @return lang.XPClass
*/
public function wrapperClass() {
switch ($this) {
case self::$STRING: return XPClass::forName('lang.types.String');
case self::$INT: return XPClass::forName('lang.types.Integer');
case self::$DOUBLE: return XPClass::forName('lang.types.Double');
case self::$BOOL: return XPClass::forName('lang.types.Boolean');
}
}

/**
* Boxes a type - that is, turns Generics into primitives
*
* @deprecated Wrapper types will move to their own library
* @param var in
* @return var the primitive if not already primitive
* @throws lang.IllegalArgumentException in case in cannot be unboxed.
*/
public static function unboxed($in) {
if ($in instanceof \lang\types\String) return $in->toString();
if ($in instanceof \lang\types\Double) return $in->doubleValue();
if ($in instanceof \lang\types\Integer) return $in->intValue();
if ($in instanceof \lang\types\Boolean) return $in->value;
if ($in instanceof \lang\types\ArrayList) return $in->values; // deprecated
if ($in instanceof Generic) {
throw new IllegalArgumentException('Cannot unbox '.\xp::typeOf($in));
}
return $in; // Already primitive
}

/**
* 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));
}

/**
* Get a type instance for a given name
*
Expand Down Expand Up @@ -129,33 +74,10 @@ public function isInstance($obj) {
*/
protected function coerce($value, $default) {
if (!is_array($value)) switch ($this) {
case self::$STRING:
if ($value instanceof \lang\types\String) return $value->toString();
if ($value instanceof \lang\types\Number) return (string)$value->value;
if ($value instanceof \lang\types\Boolean) return (string)$value->value;
if ($value instanceof \lang\types\Generic) return $value->toString();
return (string)$value;

case self::$INT:
if ($value instanceof \lang\types\String) return (int)$value->toString();
if ($value instanceof \lang\types\Number) return $value->intValue();
if ($value instanceof \lang\types\Boolean) return (int)$value->value;
if ($value instanceof \lang\types\Generic) return (int)$value->toString();
return (int)$value;

case self::$DOUBLE:
if ($value instanceof \lang\types\String) return (double)$value->toString();
if ($value instanceof \lang\types\Number) return $value->doubleValue();
if ($value instanceof \lang\types\Boolean) return (double)$value->value;
if ($value instanceof \lang\types\Generic) return (double)$value->toString();
return (double)$value;

case self::$BOOL:
if ($value instanceof \lang\types\String) return (bool)$value->toString();
if ($value instanceof \lang\types\Number) return (bool)$value->value;
if ($value instanceof \lang\types\Boolean) return $value->value;
if ($value instanceof \lang\types\Generic) return (bool)$value->toString();
return (bool)$value;
case self::$STRING: return (string)$value;
case self::$INT: return (int)$value;
case self::$DOUBLE: return (double)$value;
case self::$BOOL: return (bool)$value;
}

return $default($value);
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/lang/reflect/Method.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ class Method extends Routine {
*
* Example (passing arguments)
* ```php
* $method= XPClass::forName('lang.types.String')->getMethod('concat');
* $str= $method->invoke(new String('Hello'), ['World']);
* $method= XPClass::forName('util.Date')->getMethod('format');
* $str= $method->invoke(Date::now(), ['%d.%m.%Y']);
* ```
*
* Example (static invokation):
Expand Down
199 changes: 0 additions & 199 deletions src/main/php/lang/types/ArrayList.class.php

This file was deleted.

Loading