Skip to content
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
41 changes: 22 additions & 19 deletions src/main/php/lang.base.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,48 +159,51 @@ function cast($arg, $type) {
}
// }}}

// {{{ proto bool is(string type, var object)
// {{{ proto bool instance(string type, var value)
// Checks whether a given object is an instance of the type given
function is($type, $object) {
function instance($type, $value) {
if ('int' === $type) {
return is_int($object);
return is_int($value);
} else if ('float' === $type || 'double' === $type) {
return is_float($object);
return is_float($value);
} else if ('string' === $type) {
return is_string($object);
return is_string($value);
} else if ('bool' === $type) {
return is_bool($object);
return is_bool($value);
} else if ('var' === $type) {
return true;
} else if ('array' === $type) {
return is_array($object);
return is_array($value);
} else if ('object' === $type) {
return is_object($object);
return is_object($value);
} else if ('callable' === $type) {
return is_callable($object);
return is_callable($value);
} else if ('iterable' === $type) {
return is_array($object) || $object instanceof \Traversable;
return is_array($value) || $value instanceof \Traversable;
} else if ('?' === $type[0]) {
return null === $object || is(substr($type, 1), $object);
return null === $value || instance(substr($type, 1), $value);
} else if (0 === strncmp($type, 'function(', 9)) {
return \lang\FunctionType::forName($type)->isInstance($object);
return \lang\FunctionType::forName($type)->isInstance($value);
} else if (0 === substr_compare($type, '[]', -2)) {
return (new \lang\ArrayType(substr($type, 0, -2)))->isInstance($object);
return (new \lang\ArrayType(substr($type, 0, -2)))->isInstance($value);
} else if (0 === substr_compare($type, '[:', 0, 2)) {
return (new \lang\MapType(substr($type, 2, -1)))->isInstance($object);
return (new \lang\MapType(substr($type, 2, -1)))->isInstance($value);
} else if (0 === strncmp($type, '(function(', 10)) {
return \lang\FunctionType::forName(substr($type, 1, -1))->isInstance($object);
return \lang\FunctionType::forName(substr($type, 1, -1))->isInstance($value);
} else if (strstr($type, '|')) {
return \lang\TypeUnion::forName($type)->isInstance($object);
return \lang\TypeUnion::forName($type)->isInstance($value);
} else if (strstr($type, '&')) {
return \lang\TypeIntersection::forName($type)->isInstance($object);
return \lang\TypeIntersection::forName($type)->isInstance($value);
} else if (strstr($type, '?')) {
return \lang\WildcardType::forName($type)->isInstance($object);
return \lang\WildcardType::forName($type)->isInstance($value);
} else {
$literal= literal($type);
return $object instanceof $literal;
return $value instanceof $literal;
}
}

// Deprecated alias
function is($type, $object) { return instance($type, $object); }
// }}}

// {{{ proto string literal(string type)
Expand Down
26 changes: 11 additions & 15 deletions src/main/php/lang/GenericTypes.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,22 +256,18 @@ public function newType0($base, $arguments) {
if (null === $type) {
continue;
} else if ('...' === substr($type, -3)) {
$src.= $j ? '$·args= array_slice(func_get_args(), '.$j.');' : '$·args= func_get_args();';
$src.= (
' if (!is(\''.substr($generic[$j], 0, -3).'[]\', $·args)) throw new \lang\IllegalArgumentException('.
'"Vararg '.($j + 1).' passed to ".__METHOD__."'.
' must be of '.$type.', ".typeof($·args)->getName()." given"'.
');'
);
$verify= substr($generic[$j], 0, -3).'[]';
} else {
$src.= (
' if ('.(isset($default[$j]) ? '('.$default[$j].' !== '.$parameters[$j].') && ' : '').
'!is(\''.$generic[$j].'\', '.$parameters[$j].')) throw new \lang\IllegalArgumentException('.
'"Argument '.($j + 1).' passed to ".__METHOD__."'.
' must be of '.$type.', ".typeof('.$parameters[$j].')->getName()." given"'.
');'
);
$verify= $generic[$j];
}

$src.= (
' if ('.(isset($default[$j]) ? '('.$default[$j].' !== '.$parameters[$j].') && ' : '').
'!instance(\''.$verify.'\', '.$parameters[$j].')) throw new \lang\IllegalArgumentException('.
'"Argument '.($j + 1).' passed to ".__METHOD__."'.
' must be of '.$type.', ".typeof('.$parameters[$j].')." given"'.
');'
);
}
}
continue;
Expand Down Expand Up @@ -326,7 +322,7 @@ public function newType0($base, $arguments) {
}

// Create class
// DEBUG fputs(STDERR, "@* ".substr($src, 0, strpos($src, '{'))." -> $qname\n");
// fputs(STDERR, "@* ".substr($src, 0, strpos($src, '{'))." -> $qname\n");
eval($src);
if ($initialize) {
foreach ($components as $i => $component) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/php/lang/XPClass.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function isAssignableFrom($type): bool {

/**
* Determines whether the specified object is an instance of this
* class. This is the equivalent of the `is()` core functionality.
* class. This is the equivalent of the `instance()` core functionality.
*
* ```php
* $class= XPClass::forName('io.File');
Expand All @@ -217,7 +217,7 @@ public function isAssignableFrom($type): bool {
* @return bool
*/
public function isInstance($obj): bool {
return is($this->name, $obj);
return $obj instanceof $this->_class;
}

/** Retrieve the class loader a class was loaded with */
Expand Down
Loading
Loading