X::dump($opt->fromCode(25));
// (int) 25
X::dump($opt->fromCode('bbn_ide'));
// (int) 25
X::dump($opt->fromCode('test', 58));
// (int) 42
X::dump($opt->fromCode('test', 'users', 'bbn_ide'));
// (int) 42
function($code = NULL)
{
if ($this->check()) {
$args = \func_get_args();
// An array can be used as parameters too
while (isset($args[0]) && \is_array($args[0])){
$args = $args[0];
}
// If we get an option array as param
if (isset($args[$this->fields['id']])) {
return $args[$this->fields['id']];
}
$num = \count($args);
if (!$num) {
return null;
}
// False is accepted as id_parent for root
if (($num === 1) && ($args[0] === false)) {
return $this->default;
}
if (bbn\Str::isUid($args[0])) {
if ($num === 1) {
return $args[0];
}
// If there are extra arguments with the ID we check that they correspond to its parent (that would be an extra check)
if ($this->getIdParent($args[0]) === $this->fromCode(...\array_slice($args, 1))) {
return $args[0];
}
}
// We can use whatever alphanumeric value for code
if (empty($args) || (!\is_string($args[0]) && !is_numeric($args[0]))) {
return null;
}
// They must all have the same form at start with an id_parent as last argument
if (!bbn\Str::isUid(end($args))) {
$args[] = $this->default;
$num++;
}
// At this stage we need at least one code and one id
if ($num < 2) {
return null;
}
// So the target has always the same name
// This is the full name with all the arguments plus the root
// eg ['c1', 'c2', 'c3', UID]
// UID-c3-c4-c5
// UID-c3-c4
// UID-c3
// Using the code(s) as argument(s) from now
$id_parent = array_pop($args);
$true_code = array_pop($args);
$enc_code = base64_encode($true_code);
// This is the cache name
// get_codeX::_(base64(first_code))
$cache_name = 'get_code_'.$enc_code;
// UID-get_codeX::_(base64(first_code))
if (($tmp = $this->cacheGet($id_parent, $cache_name))) {
if (!count($args)) {
return $tmp;
}
$args[] = $tmp;
return $this->fromCode(...$args);
}
$c =& $this->class_cfg;
$f =& $this->fields;
/** @var int|false $tmp */
if (!$tmp && ($tmp = $this->db->selectOne(
$c['table'], $f['id'], [
[$f['id_parent'], '=', $id_parent],
[$f['code'], '=', $true_code]
]
))
) {
$this->cacheSet($id_parent, $cache_name, $tmp);
}
if ($tmp) {
if (\count($args)) {
$args[] = $tmp;
return $this->fromCode(...$args);
}
return $tmp;
}
}
return null;
}