-
-
Save Jeckerson/3dbc96b463a80cadf10af62e71785ee7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static void zephir_get_class_ns(zval *result, zval *object, int lower) | |
{ | |
int found = 0; | |
unsigned int i, class_length; | |
char *cursor, *class_name; | |
zval *z = Z_ISREF_P(object) ? Z_REFVAL_P(object) : object; | |
if (Z_TYPE_P(z) != IS_OBJECT) { | |
if (Z_TYPE_P(z) != IS_STRING) { | |
ZVAL_NULL(result); | |
php_error_docref(NULL, E_WARNING, "zephir_get_class_ns expects an object"); | |
return; | |
} | |
class_name = Z_STRVAL_P(z); | |
class_length = Z_STRLEN_P(z); | |
} | |
else { | |
zend_class_entry *ce = Z_OBJCE_P(z); | |
class_name = ZSTR_VAL(ce->name); | |
class_length = ZSTR_LEN(ce->name); | |
} | |
if (!class_length) { | |
ZVAL_NULL(result); | |
return; | |
} | |
i = class_length; | |
cursor = (char *) (class_name + class_length - 1); | |
while (i > 0) { | |
if ((*cursor) == '\\') { | |
found = 1; | |
break; | |
} | |
cursor--; | |
i--; | |
} | |
if (found) { | |
int cursor_length = class_length - i; | |
cursor = (char *) emalloc(cursor_length + 1); | |
memcpy(cursor, class_name + i, cursor_length); | |
cursor[cursor_length] = 0; | |
ZVAL_STRING(result, cursor); | |
efree(cursor); | |
} else { | |
ZVAL_STRINGL(result, class_name, class_length); | |
} | |
if (lower) { | |
zend_str_tolower(Z_STRVAL_P(result), Z_STRLEN_P(result)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function zephir_get_class_ns($object, int $lower): ?string | |
{ | |
if (!is_object($object)) { | |
if (!is_string($object)) { | |
throw \Phalcon\Error('zephir_get_class_ns expects an object'); | |
} | |
$className = $object; | |
$classLength = strlen($object); | |
} else { | |
$className = get_class($object); | |
$classLength = strlen($className); | |
} | |
if (!$classLength) { | |
return null; | |
} | |
$cursor = $classLength - 1; | |
$i = $classLength; | |
$found = 0; | |
while ($i > 0) { | |
if ($className[$cursor] == '\\') { | |
$found = 1; | |
break; | |
} | |
$cursor--; | |
$i--; | |
} | |
if ($found) { | |
$result = substr($className, 0, $cursor); | |
} else { | |
$result = $className; | |
} | |
if ($lower) { | |
$result = strtolower($className); | |
} | |
return $result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example: