repository_name
stringlengths 5
67
| func_path_in_repository
stringlengths 4
234
| func_name
stringlengths 0
314
| whole_func_string
stringlengths 52
3.87M
| language
stringclasses 6
values | func_code_string
stringlengths 52
3.87M
| func_code_tokens
sequencelengths 15
672k
| func_documentation_string
stringlengths 1
47.2k
| func_documentation_tokens
sequencelengths 1
3.92k
| split_name
stringclasses 1
value | func_code_url
stringlengths 85
339
|
---|---|---|---|---|---|---|---|---|---|---|
PHPPowertools/HTML5 | src/PowerTools/HTML5/Elements.php | HTML5_Elements.isA | public static function isA($name, $mask) {
if (!static::isElement($name)) {
return false;
}
return (static::element($name) & $mask) == $mask;
} | php | public static function isA($name, $mask) {
if (!static::isElement($name)) {
return false;
}
return (static::element($name) & $mask) == $mask;
} | [
"public",
"static",
"function",
"isA",
"(",
"$",
"name",
",",
"$",
"mask",
")",
"{",
"if",
"(",
"!",
"static",
"::",
"isElement",
"(",
"$",
"name",
")",
")",
"{",
"return",
"false",
";",
"}",
"return",
"(",
"static",
"::",
"element",
"(",
"$",
"name",
")",
"&",
"$",
"mask",
")",
"==",
"$",
"mask",
";",
"}"
] | Check whether the given element meets the given criterion.
Example:
Elements::isA('script', Elements::TEXT_RAW); // Returns true.
Elements::isA('script', Elements::TEXT_RCDATA); // Returns false.
@param string $name
The element name.
@param int $mask
One of the constants on this class.
@return boolean true if the element matches the mask, false otherwise. | [
"Check",
"whether",
"the",
"given",
"element",
"meets",
"the",
"given",
"criterion",
"."
] | train | https://github.com/PHPPowertools/HTML5/blob/4ea8caf5b2618a82ca5061dcbb7421b31065c1c7/src/PowerTools/HTML5/Elements.php#L560-L566 |
PHPPowertools/HTML5 | src/PowerTools/HTML5/Elements.php | HTML5_Elements.element | public static function element($name) {
if (isset(static::$html5[$name])) {
return static::$html5[$name];
}
if (isset(static::$svg[$name])) {
return static::$svg[$name];
}
if (isset(static::$mathml[$name])) {
return static::$mathml[$name];
}
return false;
} | php | public static function element($name) {
if (isset(static::$html5[$name])) {
return static::$html5[$name];
}
if (isset(static::$svg[$name])) {
return static::$svg[$name];
}
if (isset(static::$mathml[$name])) {
return static::$mathml[$name];
}
return false;
} | [
"public",
"static",
"function",
"element",
"(",
"$",
"name",
")",
"{",
"if",
"(",
"isset",
"(",
"static",
"::",
"$",
"html5",
"[",
"$",
"name",
"]",
")",
")",
"{",
"return",
"static",
"::",
"$",
"html5",
"[",
"$",
"name",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"static",
"::",
"$",
"svg",
"[",
"$",
"name",
"]",
")",
")",
"{",
"return",
"static",
"::",
"$",
"svg",
"[",
"$",
"name",
"]",
";",
"}",
"if",
"(",
"isset",
"(",
"static",
"::",
"$",
"mathml",
"[",
"$",
"name",
"]",
")",
")",
"{",
"return",
"static",
"::",
"$",
"mathml",
"[",
"$",
"name",
"]",
";",
"}",
"return",
"false",
";",
"}"
] | Get the element mask for the given element name.
@param string $name
The name of the element.
@return int The element mask. | [
"Get",
"the",
"element",
"mask",
"for",
"the",
"given",
"element",
"name",
"."
] | train | https://github.com/PHPPowertools/HTML5/blob/4ea8caf5b2618a82ca5061dcbb7421b31065c1c7/src/PowerTools/HTML5/Elements.php#L631-L643 |
PHPPowertools/HTML5 | src/PowerTools/HTML5/Elements.php | HTML5_Elements.normalizeSvgElement | public static function normalizeSvgElement($name) {
$name = strtolower($name);
if (isset(static::$svgCaseSensitiveElementMap[$name])) {
$name = static::$svgCaseSensitiveElementMap[$name];
}
return $name;
} | php | public static function normalizeSvgElement($name) {
$name = strtolower($name);
if (isset(static::$svgCaseSensitiveElementMap[$name])) {
$name = static::$svgCaseSensitiveElementMap[$name];
}
return $name;
} | [
"public",
"static",
"function",
"normalizeSvgElement",
"(",
"$",
"name",
")",
"{",
"$",
"name",
"=",
"strtolower",
"(",
"$",
"name",
")",
";",
"if",
"(",
"isset",
"(",
"static",
"::",
"$",
"svgCaseSensitiveElementMap",
"[",
"$",
"name",
"]",
")",
")",
"{",
"$",
"name",
"=",
"static",
"::",
"$",
"svgCaseSensitiveElementMap",
"[",
"$",
"name",
"]",
";",
"}",
"return",
"$",
"name",
";",
"}"
] | Normalize a SVG element name to its proper case and form.
@param string $name
The name of the element.
@return string The normalized form of the element name. | [
"Normalize",
"a",
"SVG",
"element",
"name",
"to",
"its",
"proper",
"case",
"and",
"form",
"."
] | train | https://github.com/PHPPowertools/HTML5/blob/4ea8caf5b2618a82ca5061dcbb7421b31065c1c7/src/PowerTools/HTML5/Elements.php#L653-L660 |
PHPPowertools/HTML5 | src/PowerTools/HTML5/Elements.php | HTML5_Elements.normalizeSvgAttribute | public static function normalizeSvgAttribute($name) {
$name = strtolower($name);
if (isset(static::$svgCaseSensitiveAttributeMap[$name])) {
$name = static::$svgCaseSensitiveAttributeMap[$name];
}
return $name;
} | php | public static function normalizeSvgAttribute($name) {
$name = strtolower($name);
if (isset(static::$svgCaseSensitiveAttributeMap[$name])) {
$name = static::$svgCaseSensitiveAttributeMap[$name];
}
return $name;
} | [
"public",
"static",
"function",
"normalizeSvgAttribute",
"(",
"$",
"name",
")",
"{",
"$",
"name",
"=",
"strtolower",
"(",
"$",
"name",
")",
";",
"if",
"(",
"isset",
"(",
"static",
"::",
"$",
"svgCaseSensitiveAttributeMap",
"[",
"$",
"name",
"]",
")",
")",
"{",
"$",
"name",
"=",
"static",
"::",
"$",
"svgCaseSensitiveAttributeMap",
"[",
"$",
"name",
"]",
";",
"}",
"return",
"$",
"name",
";",
"}"
] | Normalize a SVG attribute name to its proper case and form.
@param string $name
The name of the attribute.
@return string The normalized form of the attribute name. | [
"Normalize",
"a",
"SVG",
"attribute",
"name",
"to",
"its",
"proper",
"case",
"and",
"form",
"."
] | train | https://github.com/PHPPowertools/HTML5/blob/4ea8caf5b2618a82ca5061dcbb7421b31065c1c7/src/PowerTools/HTML5/Elements.php#L670-L677 |
spiral-modules/listing | source/Listing/Filters/SearchFilter.php | SearchFilter.whereClause | protected function whereClause($selector)
{
$whereClause = [];
foreach ($this->mapping as $expression => $type) {
switch ($type) {
case self::LIKE_STRING:
$whereClause[] = $this->likeString($expression, $selector);
break;
case self::EQUALS_STRING:
$whereClause[] = $this->equalsString($expression, $selector);
break;
case self::EQUALS_INT:
$whereClause[] = $this->equalsInteger($expression, $selector);
break;
case self::EQUALS_FLOAT:
$whereClause[] = $this->equalsFloat($expression, $selector);
break;
default:
throw new FilterException("Invalid filter type, '{$type}' given");
}
}
return $this->summarize($whereClause, $selector);
} | php | protected function whereClause($selector)
{
$whereClause = [];
foreach ($this->mapping as $expression => $type) {
switch ($type) {
case self::LIKE_STRING:
$whereClause[] = $this->likeString($expression, $selector);
break;
case self::EQUALS_STRING:
$whereClause[] = $this->equalsString($expression, $selector);
break;
case self::EQUALS_INT:
$whereClause[] = $this->equalsInteger($expression, $selector);
break;
case self::EQUALS_FLOAT:
$whereClause[] = $this->equalsFloat($expression, $selector);
break;
default:
throw new FilterException("Invalid filter type, '{$type}' given");
}
}
return $this->summarize($whereClause, $selector);
} | [
"protected",
"function",
"whereClause",
"(",
"$",
"selector",
")",
"{",
"$",
"whereClause",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"this",
"->",
"mapping",
"as",
"$",
"expression",
"=>",
"$",
"type",
")",
"{",
"switch",
"(",
"$",
"type",
")",
"{",
"case",
"self",
"::",
"LIKE_STRING",
":",
"$",
"whereClause",
"[",
"]",
"=",
"$",
"this",
"->",
"likeString",
"(",
"$",
"expression",
",",
"$",
"selector",
")",
";",
"break",
";",
"case",
"self",
"::",
"EQUALS_STRING",
":",
"$",
"whereClause",
"[",
"]",
"=",
"$",
"this",
"->",
"equalsString",
"(",
"$",
"expression",
",",
"$",
"selector",
")",
";",
"break",
";",
"case",
"self",
"::",
"EQUALS_INT",
":",
"$",
"whereClause",
"[",
"]",
"=",
"$",
"this",
"->",
"equalsInteger",
"(",
"$",
"expression",
",",
"$",
"selector",
")",
";",
"break",
";",
"case",
"self",
"::",
"EQUALS_FLOAT",
":",
"$",
"whereClause",
"[",
"]",
"=",
"$",
"this",
"->",
"equalsFloat",
"(",
"$",
"expression",
",",
"$",
"selector",
")",
";",
"break",
";",
"default",
":",
"throw",
"new",
"FilterException",
"(",
"\"Invalid filter type, '{$type}' given\"",
")",
";",
"}",
"}",
"return",
"$",
"this",
"->",
"summarize",
"(",
"$",
"whereClause",
",",
"$",
"selector",
")",
";",
"}"
] | {@inheritdoc} | [
"{"
] | train | https://github.com/spiral-modules/listing/blob/89abe8939ce91cbf61b51b99e93ce1e57c8af83c/source/Listing/Filters/SearchFilter.php#L84-L112 |
spiral-modules/listing | source/Listing/Filters/SearchFilter.php | SearchFilter.summarize | private function summarize(array $whereClause, $selector)
{
switch (count($whereClause)) {
case 1:
return $whereClause[0];
default:
if ($selector instanceof RecordSelector) {
return ['@OR' => $whereClause];
}
if ($selector instanceof DocumentSelector) {
return ['$or' => $whereClause];
}
}
return [];
} | php | private function summarize(array $whereClause, $selector)
{
switch (count($whereClause)) {
case 1:
return $whereClause[0];
default:
if ($selector instanceof RecordSelector) {
return ['@OR' => $whereClause];
}
if ($selector instanceof DocumentSelector) {
return ['$or' => $whereClause];
}
}
return [];
} | [
"private",
"function",
"summarize",
"(",
"array",
"$",
"whereClause",
",",
"$",
"selector",
")",
"{",
"switch",
"(",
"count",
"(",
"$",
"whereClause",
")",
")",
"{",
"case",
"1",
":",
"return",
"$",
"whereClause",
"[",
"0",
"]",
";",
"default",
":",
"if",
"(",
"$",
"selector",
"instanceof",
"RecordSelector",
")",
"{",
"return",
"[",
"'@OR'",
"=>",
"$",
"whereClause",
"]",
";",
"}",
"if",
"(",
"$",
"selector",
"instanceof",
"DocumentSelector",
")",
"{",
"return",
"[",
"'$or'",
"=>",
"$",
"whereClause",
"]",
";",
"}",
"}",
"return",
"[",
"]",
";",
"}"
] | Summarize expression array.
@param array $whereClause
@param RecordSelector|DocumentSelector $selector
@return array | [
"Summarize",
"expression",
"array",
"."
] | train | https://github.com/spiral-modules/listing/blob/89abe8939ce91cbf61b51b99e93ce1e57c8af83c/source/Listing/Filters/SearchFilter.php#L171-L188 |
hametuha/wpametu | src/WPametu/UI/Field/Radio.php | Radio.get_option | protected function get_option($key, $label, $counter, $data, array $fields = []){
return sprintf('<label%6$s><input type="radio" name="%1$s" id="%1$s-%2$d" value="%3$s" %4$s /> %5$s </label>',
$this->get_name(), $counter, esc_attr($key),
checked( (!$data && $key == $this->default) || $key == $data , true, false),
esc_html($label), $this->inline ? ' class="inline"' : '');
} | php | protected function get_option($key, $label, $counter, $data, array $fields = []){
return sprintf('<label%6$s><input type="radio" name="%1$s" id="%1$s-%2$d" value="%3$s" %4$s /> %5$s </label>',
$this->get_name(), $counter, esc_attr($key),
checked( (!$data && $key == $this->default) || $key == $data , true, false),
esc_html($label), $this->inline ? ' class="inline"' : '');
} | [
"protected",
"function",
"get_option",
"(",
"$",
"key",
",",
"$",
"label",
",",
"$",
"counter",
",",
"$",
"data",
",",
"array",
"$",
"fields",
"=",
"[",
"]",
")",
"{",
"return",
"sprintf",
"(",
"'<label%6$s><input type=\"radio\" name=\"%1$s\" id=\"%1$s-%2$d\" value=\"%3$s\" %4$s /> %5$s </label>'",
",",
"$",
"this",
"->",
"get_name",
"(",
")",
",",
"$",
"counter",
",",
"esc_attr",
"(",
"$",
"key",
")",
",",
"checked",
"(",
"(",
"!",
"$",
"data",
"&&",
"$",
"key",
"==",
"$",
"this",
"->",
"default",
")",
"||",
"$",
"key",
"==",
"$",
"data",
",",
"true",
",",
"false",
")",
",",
"esc_html",
"(",
"$",
"label",
")",
",",
"$",
"this",
"->",
"inline",
"?",
"' class=\"inline\"'",
":",
"''",
")",
";",
"}"
] | Get fields input
@param string $key
@param string $label
@param int $counter
@param string $data
@param array $fields
@return string | [
"Get",
"fields",
"input"
] | train | https://github.com/hametuha/wpametu/blob/0939373800815a8396291143d2a57967340da5aa/src/WPametu/UI/Field/Radio.php#L49-L54 |
nabab/bbn | src/bbn/x.php | x.log | public static function log($st, $file='misc'){
if ( \defined('BBN_DATA_PATH') ){
if ( !\is_string($file) ){
$file = 'misc';
}
$log_file = BBN_DATA_PATH.'logs/'.$file.'.log';
$backtrace = array_filter(debug_backtrace(), function($a){
return $a['function'] === 'log';
});
$i = end($backtrace);
$r = "[".date('d/m/Y H:i:s')."]\t".$i['file']." - line ".$i['line'].
self::get_dump($st).PHP_EOL;
if ( php_sapi_name() === 'cli' ){
global $argv;
if ( isset($argv[2]) && ($argv[2] === 'log') ){
echo self::get_dump($st).PHP_EOL;
}
}
$s = ( file_exists($log_file) ) ? filesize($log_file) : 0;
if ( $s > 1048576 ){
file_put_contents($log_file.'.old', file_get_contents($log_file), FILE_APPEND);
file_put_contents($log_file, $r);
}
else{
file_put_contents($log_file, $r, FILE_APPEND);
}
}
} | php | public static function log($st, $file='misc'){
if ( \defined('BBN_DATA_PATH') ){
if ( !\is_string($file) ){
$file = 'misc';
}
$log_file = BBN_DATA_PATH.'logs/'.$file.'.log';
$backtrace = array_filter(debug_backtrace(), function($a){
return $a['function'] === 'log';
});
$i = end($backtrace);
$r = "[".date('d/m/Y H:i:s')."]\t".$i['file']." - line ".$i['line'].
self::get_dump($st).PHP_EOL;
if ( php_sapi_name() === 'cli' ){
global $argv;
if ( isset($argv[2]) && ($argv[2] === 'log') ){
echo self::get_dump($st).PHP_EOL;
}
}
$s = ( file_exists($log_file) ) ? filesize($log_file) : 0;
if ( $s > 1048576 ){
file_put_contents($log_file.'.old', file_get_contents($log_file), FILE_APPEND);
file_put_contents($log_file, $r);
}
else{
file_put_contents($log_file, $r, FILE_APPEND);
}
}
} | [
"public",
"static",
"function",
"log",
"(",
"$",
"st",
",",
"$",
"file",
"=",
"'misc'",
")",
"{",
"if",
"(",
"\\",
"defined",
"(",
"'BBN_DATA_PATH'",
")",
")",
"{",
"if",
"(",
"!",
"\\",
"is_string",
"(",
"$",
"file",
")",
")",
"{",
"$",
"file",
"=",
"'misc'",
";",
"}",
"$",
"log_file",
"=",
"BBN_DATA_PATH",
".",
"'logs/'",
".",
"$",
"file",
".",
"'.log'",
";",
"$",
"backtrace",
"=",
"array_filter",
"(",
"debug_backtrace",
"(",
")",
",",
"function",
"(",
"$",
"a",
")",
"{",
"return",
"$",
"a",
"[",
"'function'",
"]",
"===",
"'log'",
";",
"}",
")",
";",
"$",
"i",
"=",
"end",
"(",
"$",
"backtrace",
")",
";",
"$",
"r",
"=",
"\"[\"",
".",
"date",
"(",
"'d/m/Y H:i:s'",
")",
".",
"\"]\\t\"",
".",
"$",
"i",
"[",
"'file'",
"]",
".",
"\" - line \"",
".",
"$",
"i",
"[",
"'line'",
"]",
".",
"self",
"::",
"get_dump",
"(",
"$",
"st",
")",
".",
"PHP_EOL",
";",
"if",
"(",
"php_sapi_name",
"(",
")",
"===",
"'cli'",
")",
"{",
"global",
"$",
"argv",
";",
"if",
"(",
"isset",
"(",
"$",
"argv",
"[",
"2",
"]",
")",
"&&",
"(",
"$",
"argv",
"[",
"2",
"]",
"===",
"'log'",
")",
")",
"{",
"echo",
"self",
"::",
"get_dump",
"(",
"$",
"st",
")",
".",
"PHP_EOL",
";",
"}",
"}",
"$",
"s",
"=",
"(",
"file_exists",
"(",
"$",
"log_file",
")",
")",
"?",
"filesize",
"(",
"$",
"log_file",
")",
":",
"0",
";",
"if",
"(",
"$",
"s",
">",
"1048576",
")",
"{",
"file_put_contents",
"(",
"$",
"log_file",
".",
"'.old'",
",",
"file_get_contents",
"(",
"$",
"log_file",
")",
",",
"FILE_APPEND",
")",
";",
"file_put_contents",
"(",
"$",
"log_file",
",",
"$",
"r",
")",
";",
"}",
"else",
"{",
"file_put_contents",
"(",
"$",
"log_file",
",",
"$",
"r",
",",
"FILE_APPEND",
")",
";",
"}",
"}",
"}"
] | Saves logs to a file.
```php
\bbn\x::log('My text', 'FileName');
```
@param mixed $st Item to log.
@param string $file Filename, default: "misc".
@return void | [
"Saves",
"logs",
"to",
"a",
"file",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L75-L103 |
nabab/bbn | src/bbn/x.php | x.log_error | public static function log_error($errno, $errstr, $errfile, $errline){
if ( \defined('BBN_DATA_PATH') ){
if ( is_dir(BBN_DATA_PATH.'logs') ){
$file = BBN_DATA_PATH.'logs/_php_error.json';
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20);
$r = false;
if ( is_file($file) ){
$r = json_decode(file_get_contents($file), 1);
}
if ( !$r ){
$r = [];
}
$t = date('Y-m-d H:i:s');
if ( class_exists('\\bbn\\mvc') ){
$mvc = mvc::get_instance();
}
$idx = self::find($r, [
'type' => $errno,
'error' => $errstr,
'file' => $errfile,
'line' => $errline,
'request' => ''
]);
if ( $idx !== false ){
$r[$idx]['count']++;
$r[$idx]['last_date'] = $t;
$r[$idx]['backtrace'] = $backtrace;
}
else{
$r[] = [
'first_date' => $t,
'last_date' => $t,
'count' => 1,
'type' => $errno,
'error' => $errstr,
'file' => $errfile,
'line' => $errline,
'backtrace' => $backtrace,
'request' => ''
//'context' => $context
];
}
self::sort_by($r, 'last_date', 'DESC');
file_put_contents($file, json_encode($r, JSON_PRETTY_PRINT));
}
if ( $errno > 8 ){
die($errstr);
}
}
return false;
} | php | public static function log_error($errno, $errstr, $errfile, $errline){
if ( \defined('BBN_DATA_PATH') ){
if ( is_dir(BBN_DATA_PATH.'logs') ){
$file = BBN_DATA_PATH.'logs/_php_error.json';
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 20);
$r = false;
if ( is_file($file) ){
$r = json_decode(file_get_contents($file), 1);
}
if ( !$r ){
$r = [];
}
$t = date('Y-m-d H:i:s');
if ( class_exists('\\bbn\\mvc') ){
$mvc = mvc::get_instance();
}
$idx = self::find($r, [
'type' => $errno,
'error' => $errstr,
'file' => $errfile,
'line' => $errline,
'request' => ''
]);
if ( $idx !== false ){
$r[$idx]['count']++;
$r[$idx]['last_date'] = $t;
$r[$idx]['backtrace'] = $backtrace;
}
else{
$r[] = [
'first_date' => $t,
'last_date' => $t,
'count' => 1,
'type' => $errno,
'error' => $errstr,
'file' => $errfile,
'line' => $errline,
'backtrace' => $backtrace,
'request' => ''
//'context' => $context
];
}
self::sort_by($r, 'last_date', 'DESC');
file_put_contents($file, json_encode($r, JSON_PRETTY_PRINT));
}
if ( $errno > 8 ){
die($errstr);
}
}
return false;
} | [
"public",
"static",
"function",
"log_error",
"(",
"$",
"errno",
",",
"$",
"errstr",
",",
"$",
"errfile",
",",
"$",
"errline",
")",
"{",
"if",
"(",
"\\",
"defined",
"(",
"'BBN_DATA_PATH'",
")",
")",
"{",
"if",
"(",
"is_dir",
"(",
"BBN_DATA_PATH",
".",
"'logs'",
")",
")",
"{",
"$",
"file",
"=",
"BBN_DATA_PATH",
".",
"'logs/_php_error.json'",
";",
"$",
"backtrace",
"=",
"debug_backtrace",
"(",
"DEBUG_BACKTRACE_IGNORE_ARGS",
",",
"20",
")",
";",
"$",
"r",
"=",
"false",
";",
"if",
"(",
"is_file",
"(",
"$",
"file",
")",
")",
"{",
"$",
"r",
"=",
"json_decode",
"(",
"file_get_contents",
"(",
"$",
"file",
")",
",",
"1",
")",
";",
"}",
"if",
"(",
"!",
"$",
"r",
")",
"{",
"$",
"r",
"=",
"[",
"]",
";",
"}",
"$",
"t",
"=",
"date",
"(",
"'Y-m-d H:i:s'",
")",
";",
"if",
"(",
"class_exists",
"(",
"'\\\\bbn\\\\mvc'",
")",
")",
"{",
"$",
"mvc",
"=",
"mvc",
"::",
"get_instance",
"(",
")",
";",
"}",
"$",
"idx",
"=",
"self",
"::",
"find",
"(",
"$",
"r",
",",
"[",
"'type'",
"=>",
"$",
"errno",
",",
"'error'",
"=>",
"$",
"errstr",
",",
"'file'",
"=>",
"$",
"errfile",
",",
"'line'",
"=>",
"$",
"errline",
",",
"'request'",
"=>",
"''",
"]",
")",
";",
"if",
"(",
"$",
"idx",
"!==",
"false",
")",
"{",
"$",
"r",
"[",
"$",
"idx",
"]",
"[",
"'count'",
"]",
"++",
";",
"$",
"r",
"[",
"$",
"idx",
"]",
"[",
"'last_date'",
"]",
"=",
"$",
"t",
";",
"$",
"r",
"[",
"$",
"idx",
"]",
"[",
"'backtrace'",
"]",
"=",
"$",
"backtrace",
";",
"}",
"else",
"{",
"$",
"r",
"[",
"]",
"=",
"[",
"'first_date'",
"=>",
"$",
"t",
",",
"'last_date'",
"=>",
"$",
"t",
",",
"'count'",
"=>",
"1",
",",
"'type'",
"=>",
"$",
"errno",
",",
"'error'",
"=>",
"$",
"errstr",
",",
"'file'",
"=>",
"$",
"errfile",
",",
"'line'",
"=>",
"$",
"errline",
",",
"'backtrace'",
"=>",
"$",
"backtrace",
",",
"'request'",
"=>",
"''",
"//'context' => $context",
"]",
";",
"}",
"self",
"::",
"sort_by",
"(",
"$",
"r",
",",
"'last_date'",
",",
"'DESC'",
")",
";",
"file_put_contents",
"(",
"$",
"file",
",",
"json_encode",
"(",
"$",
"r",
",",
"JSON_PRETTY_PRINT",
")",
")",
";",
"}",
"if",
"(",
"$",
"errno",
">",
"8",
")",
"{",
"die",
"(",
"$",
"errstr",
")",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Puts the PHP errors into a JSON file.
@param string $errno The text to save.
@param string $errstr The file's name, default: "misc".
@param $errfile
@param $errline
@return bool | [
"Puts",
"the",
"PHP",
"errors",
"into",
"a",
"JSON",
"file",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L114-L164 |
nabab/bbn | src/bbn/x.php | x.merge_objects | public static function merge_objects($o1, $o2){
$args = \func_get_args();
/* @todo check if it's working with more than 2 object arguments */
if ( \count($args) > 2 ){
for ( $i = \count($args) - 1; $i > 1; $i-- ){
$args[$i-1] = self::merge_arrays($args[$i-1], $args[$i]);
}
$o2 = $args[1];
}
$a1 = self::to_array($o1);
$a2 = self::to_array($o2);
$res = self::merge_arrays($a1, $a2);
return self::to_object($res);
} | php | public static function merge_objects($o1, $o2){
$args = \func_get_args();
/* @todo check if it's working with more than 2 object arguments */
if ( \count($args) > 2 ){
for ( $i = \count($args) - 1; $i > 1; $i-- ){
$args[$i-1] = self::merge_arrays($args[$i-1], $args[$i]);
}
$o2 = $args[1];
}
$a1 = self::to_array($o1);
$a2 = self::to_array($o2);
$res = self::merge_arrays($a1, $a2);
return self::to_object($res);
} | [
"public",
"static",
"function",
"merge_objects",
"(",
"$",
"o1",
",",
"$",
"o2",
")",
"{",
"$",
"args",
"=",
"\\",
"func_get_args",
"(",
")",
";",
"/* @todo check if it's working with more than 2 object arguments */",
"if",
"(",
"\\",
"count",
"(",
"$",
"args",
")",
">",
"2",
")",
"{",
"for",
"(",
"$",
"i",
"=",
"\\",
"count",
"(",
"$",
"args",
")",
"-",
"1",
";",
"$",
"i",
">",
"1",
";",
"$",
"i",
"--",
")",
"{",
"$",
"args",
"[",
"$",
"i",
"-",
"1",
"]",
"=",
"self",
"::",
"merge_arrays",
"(",
"$",
"args",
"[",
"$",
"i",
"-",
"1",
"]",
",",
"$",
"args",
"[",
"$",
"i",
"]",
")",
";",
"}",
"$",
"o2",
"=",
"$",
"args",
"[",
"1",
"]",
";",
"}",
"$",
"a1",
"=",
"self",
"::",
"to_array",
"(",
"$",
"o1",
")",
";",
"$",
"a2",
"=",
"self",
"::",
"to_array",
"(",
"$",
"o2",
")",
";",
"$",
"res",
"=",
"self",
"::",
"merge_arrays",
"(",
"$",
"a1",
",",
"$",
"a2",
")",
";",
"return",
"self",
"::",
"to_object",
"(",
"$",
"res",
")",
";",
"}"
] | Returns to a merged object from two objects.
```php
class A {
public $a = 10;
public $b = 20;
};
class B {
public $c = 30;
public $d = 40;
};
$obj1 = new A;
$obj2 = new B;
\bbn\x::merge_objects($obj1, $obj2);
// object {'a': 10, 'b': 20, 'c': 30, 'd': 40}
```
@param object $o1 The first object to merge.
@param object $o2 The second object to merge.
@return object The merged object. | [
"Returns",
"to",
"a",
"merged",
"object",
"from",
"two",
"objects",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L191-L204 |
nabab/bbn | src/bbn/x.php | x.merge_arrays | public static function merge_arrays(array $a1, array $a2){
$args = \func_get_args();
if ( \count($args) > 2 ){
for ( $i = \count($args) - 1; $i > 1; $i-- ){
$args[$i-1] = self::merge_arrays($args[$i-1], $args[$i]);
}
$a2 = $args[1];
}
if ( (self::is_assoc($a1) || empty($a1)) && (self::is_assoc($a2) || empty($a2)) ){
$keys = array_unique(array_merge(array_keys($a1), array_keys($a2)));
$r = [];
foreach ( $keys as $k ){
if ( !array_key_exists($k, $a1) && !array_key_exists($k, $a2) ){
continue;
}
else if ( !array_key_exists($k, $a2) ){
$r[$k] = $a1[$k];
}
else if ( !array_key_exists($k, $a1) || !\is_array($a2[$k]) || !\is_array($a1[$k]) || is_numeric(key($a2[$k])) ){
$r[$k] = $a2[$k];
}
else{
$r[$k] = self::merge_arrays($a1[$k], $a2[$k]);
}
}
}
else{
$r = array_merge($a1, $a2);
}
return $r;
} | php | public static function merge_arrays(array $a1, array $a2){
$args = \func_get_args();
if ( \count($args) > 2 ){
for ( $i = \count($args) - 1; $i > 1; $i-- ){
$args[$i-1] = self::merge_arrays($args[$i-1], $args[$i]);
}
$a2 = $args[1];
}
if ( (self::is_assoc($a1) || empty($a1)) && (self::is_assoc($a2) || empty($a2)) ){
$keys = array_unique(array_merge(array_keys($a1), array_keys($a2)));
$r = [];
foreach ( $keys as $k ){
if ( !array_key_exists($k, $a1) && !array_key_exists($k, $a2) ){
continue;
}
else if ( !array_key_exists($k, $a2) ){
$r[$k] = $a1[$k];
}
else if ( !array_key_exists($k, $a1) || !\is_array($a2[$k]) || !\is_array($a1[$k]) || is_numeric(key($a2[$k])) ){
$r[$k] = $a2[$k];
}
else{
$r[$k] = self::merge_arrays($a1[$k], $a2[$k]);
}
}
}
else{
$r = array_merge($a1, $a2);
}
return $r;
} | [
"public",
"static",
"function",
"merge_arrays",
"(",
"array",
"$",
"a1",
",",
"array",
"$",
"a2",
")",
"{",
"$",
"args",
"=",
"\\",
"func_get_args",
"(",
")",
";",
"if",
"(",
"\\",
"count",
"(",
"$",
"args",
")",
">",
"2",
")",
"{",
"for",
"(",
"$",
"i",
"=",
"\\",
"count",
"(",
"$",
"args",
")",
"-",
"1",
";",
"$",
"i",
">",
"1",
";",
"$",
"i",
"--",
")",
"{",
"$",
"args",
"[",
"$",
"i",
"-",
"1",
"]",
"=",
"self",
"::",
"merge_arrays",
"(",
"$",
"args",
"[",
"$",
"i",
"-",
"1",
"]",
",",
"$",
"args",
"[",
"$",
"i",
"]",
")",
";",
"}",
"$",
"a2",
"=",
"$",
"args",
"[",
"1",
"]",
";",
"}",
"if",
"(",
"(",
"self",
"::",
"is_assoc",
"(",
"$",
"a1",
")",
"||",
"empty",
"(",
"$",
"a1",
")",
")",
"&&",
"(",
"self",
"::",
"is_assoc",
"(",
"$",
"a2",
")",
"||",
"empty",
"(",
"$",
"a2",
")",
")",
")",
"{",
"$",
"keys",
"=",
"array_unique",
"(",
"array_merge",
"(",
"array_keys",
"(",
"$",
"a1",
")",
",",
"array_keys",
"(",
"$",
"a2",
")",
")",
")",
";",
"$",
"r",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"keys",
"as",
"$",
"k",
")",
"{",
"if",
"(",
"!",
"array_key_exists",
"(",
"$",
"k",
",",
"$",
"a1",
")",
"&&",
"!",
"array_key_exists",
"(",
"$",
"k",
",",
"$",
"a2",
")",
")",
"{",
"continue",
";",
"}",
"else",
"if",
"(",
"!",
"array_key_exists",
"(",
"$",
"k",
",",
"$",
"a2",
")",
")",
"{",
"$",
"r",
"[",
"$",
"k",
"]",
"=",
"$",
"a1",
"[",
"$",
"k",
"]",
";",
"}",
"else",
"if",
"(",
"!",
"array_key_exists",
"(",
"$",
"k",
",",
"$",
"a1",
")",
"||",
"!",
"\\",
"is_array",
"(",
"$",
"a2",
"[",
"$",
"k",
"]",
")",
"||",
"!",
"\\",
"is_array",
"(",
"$",
"a1",
"[",
"$",
"k",
"]",
")",
"||",
"is_numeric",
"(",
"key",
"(",
"$",
"a2",
"[",
"$",
"k",
"]",
")",
")",
")",
"{",
"$",
"r",
"[",
"$",
"k",
"]",
"=",
"$",
"a2",
"[",
"$",
"k",
"]",
";",
"}",
"else",
"{",
"$",
"r",
"[",
"$",
"k",
"]",
"=",
"self",
"::",
"merge_arrays",
"(",
"$",
"a1",
"[",
"$",
"k",
"]",
",",
"$",
"a2",
"[",
"$",
"k",
"]",
")",
";",
"}",
"}",
"}",
"else",
"{",
"$",
"r",
"=",
"array_merge",
"(",
"$",
"a1",
",",
"$",
"a2",
")",
";",
"}",
"return",
"$",
"r",
";",
"}"
] | Returns to a merged array from two or more arrays.
```php
\bbn\x::merge_arrays([1, 'Test'], [2, 'Example']);
// array [1, 'Test', 2, 'Example']
```
@param array $a1 The first array to merge.
@param array $a2 The second array to merge.
@return array The merged array. | [
"Returns",
"to",
"a",
"merged",
"array",
"from",
"two",
"or",
"more",
"arrays",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L217-L247 |
nabab/bbn | src/bbn/x.php | x.remove_empty | public static function remove_empty($arr, $remove_space = false){
foreach ( $arr as $k => $v ){
if ( \is_object($arr) ){
if ( \is_array($v) || \is_object($v) ){
$arr->$k = self::remove_empty($v);
}
if ( empty($arr->$k) ){
unset($arr->$k);
}
}
else{
if ( \is_array($v) || \is_object($v) ){
$arr[$k] = self::remove_empty($v);
}
else if ( $remove_space ){
$arr[$k] = trim($arr[$k]);
}
if ( empty($arr[$k]) ){
unset($arr[$k]);
}
}
}
return $arr;
} | php | public static function remove_empty($arr, $remove_space = false){
foreach ( $arr as $k => $v ){
if ( \is_object($arr) ){
if ( \is_array($v) || \is_object($v) ){
$arr->$k = self::remove_empty($v);
}
if ( empty($arr->$k) ){
unset($arr->$k);
}
}
else{
if ( \is_array($v) || \is_object($v) ){
$arr[$k] = self::remove_empty($v);
}
else if ( $remove_space ){
$arr[$k] = trim($arr[$k]);
}
if ( empty($arr[$k]) ){
unset($arr[$k]);
}
}
}
return $arr;
} | [
"public",
"static",
"function",
"remove_empty",
"(",
"$",
"arr",
",",
"$",
"remove_space",
"=",
"false",
")",
"{",
"foreach",
"(",
"$",
"arr",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"\\",
"is_object",
"(",
"$",
"arr",
")",
")",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"v",
")",
"||",
"\\",
"is_object",
"(",
"$",
"v",
")",
")",
"{",
"$",
"arr",
"->",
"$",
"k",
"=",
"self",
"::",
"remove_empty",
"(",
"$",
"v",
")",
";",
"}",
"if",
"(",
"empty",
"(",
"$",
"arr",
"->",
"$",
"k",
")",
")",
"{",
"unset",
"(",
"$",
"arr",
"->",
"$",
"k",
")",
";",
"}",
"}",
"else",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"v",
")",
"||",
"\\",
"is_object",
"(",
"$",
"v",
")",
")",
"{",
"$",
"arr",
"[",
"$",
"k",
"]",
"=",
"self",
"::",
"remove_empty",
"(",
"$",
"v",
")",
";",
"}",
"else",
"if",
"(",
"$",
"remove_space",
")",
"{",
"$",
"arr",
"[",
"$",
"k",
"]",
"=",
"trim",
"(",
"$",
"arr",
"[",
"$",
"k",
"]",
")",
";",
"}",
"if",
"(",
"empty",
"(",
"$",
"arr",
"[",
"$",
"k",
"]",
")",
")",
"{",
"unset",
"(",
"$",
"arr",
"[",
"$",
"k",
"]",
")",
";",
"}",
"}",
"}",
"return",
"$",
"arr",
";",
"}"
] | Returns an object or an array cleaned of all empty values.
@todo Add a preserve_keys option?
```php
\bbn\x::remove_empty(['Allison', 'Mike', '', 'John', ' ']);
// array [0 => 'Allison', 1 => 'Mike', 3 => 'John', 4 => ' ']
\bbn\x::remove_empty(['Allison', 'Mike', '', 'John', ' '], 1));
// array [0 => 'Allison', 1 => 'Mike', 3 => 'John']
```
@param array|object $arr An object or array to clean.
@param bool $remove_space If "true" the spaces are removed, default: "false".
@return array The cleaned result. | [
"Returns",
"an",
"object",
"or",
"an",
"array",
"cleaned",
"of",
"all",
"empty",
"values",
".",
"@todo",
"Add",
"a",
"preserve_keys",
"option?"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L416-L439 |
nabab/bbn | src/bbn/x.php | x.to_groups | public static function to_groups(array $arr, $keyname = 'value', $valname = 'text'){
$r = [];
foreach ( $arr as $k => $v ){
$r[] = [$keyname => $k, $valname => $v];
}
return $r;
} | php | public static function to_groups(array $arr, $keyname = 'value', $valname = 'text'){
$r = [];
foreach ( $arr as $k => $v ){
$r[] = [$keyname => $k, $valname => $v];
}
return $r;
} | [
"public",
"static",
"function",
"to_groups",
"(",
"array",
"$",
"arr",
",",
"$",
"keyname",
"=",
"'value'",
",",
"$",
"valname",
"=",
"'text'",
")",
"{",
"$",
"r",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"arr",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"$",
"r",
"[",
"]",
"=",
"[",
"$",
"keyname",
"=>",
"$",
"k",
",",
"$",
"valname",
"=>",
"$",
"v",
"]",
";",
"}",
"return",
"$",
"r",
";",
"}"
] | Converts an indexed array into a numeric array where the original index is a property.
@todo the name is not fitted
```php
\bbn\x::to_groups([25 => 'Allison', 33 => 'Mike', 19 => 'John']);
// array [['value' => 25, 'text' => 'Allison'], ['value' => 33, 'text' => 'Francis'], ['value' => 19, 'text' => 'John']]
\bbn\x::to_groups(['Allison', 'Mike', 'John'],'id', 'name');
// array [['id' => 25, 'name' => 'Allison'], ['id' => 33, 'name' => 'Francis'], ['id' => 19, 'name' => 'John']]
```
@param array $arr The original array.
@param string $keyname Alias for the index.
@param string $valname Alias for the value.
@return array Groups array. | [
"Converts",
"an",
"indexed",
"array",
"into",
"a",
"numeric",
"array",
"where",
"the",
"original",
"index",
"is",
"a",
"property",
".",
"@todo",
"the",
"name",
"is",
"not",
"fitted"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L458-L464 |
nabab/bbn | src/bbn/x.php | x.is_assoc | public static function is_assoc(array $r){
$keys = array_keys($r);
$c = \count($keys);
for ( $i = 0; $i < $c; $i++ ){
if ( $keys[$i] !== $i ){
return 1;
}
}
return false;
} | php | public static function is_assoc(array $r){
$keys = array_keys($r);
$c = \count($keys);
for ( $i = 0; $i < $c; $i++ ){
if ( $keys[$i] !== $i ){
return 1;
}
}
return false;
} | [
"public",
"static",
"function",
"is_assoc",
"(",
"array",
"$",
"r",
")",
"{",
"$",
"keys",
"=",
"array_keys",
"(",
"$",
"r",
")",
";",
"$",
"c",
"=",
"\\",
"count",
"(",
"$",
"keys",
")",
";",
"for",
"(",
"$",
"i",
"=",
"0",
";",
"$",
"i",
"<",
"$",
"c",
";",
"$",
"i",
"++",
")",
"{",
"if",
"(",
"$",
"keys",
"[",
"$",
"i",
"]",
"!==",
"$",
"i",
")",
"{",
"return",
"1",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Checks if the given array is associative.
```php
\bbn\\x::is_assoc(['id' => 0, 'name' => 'Allison']);
\bbn\\x::is_assoc(['Allison', 'John', 'Bert']);
\bbn\\x::is_assoc([0 => "Allison", 1 => "John", 2 => "Bert"]);
\bbn\\x::is_assoc([0 => "Allison", 1 => "John", 3 => "Bert"]);
// boolean true
// boolean false
// boolean false
// boolean true
```
@param array $r The array to check.
@return bool | [
"Checks",
"if",
"the",
"given",
"array",
"is",
"associative",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L487-L496 |
nabab/bbn | src/bbn/x.php | x.get_dump | public static function get_dump(){
$args = \func_get_args();
$st = '';
foreach ( $args as $a ){
$r = $a;
if ( \is_null($a) ){
$r = 'null';
}
else if ( $a === false ){
$r = 'false';
}
else if ( $a === true ){
$r = 'true';
}
else if ( $a === 0 ){
$r = '0';
}
else if ( $a === '' ){
$r = '""';
}
else if ( $a === [] ){
$r = '[]';
}
else if ( !$a ){
$r = '0';
}
else if ( !\is_string($a) && \is_callable($a) ){
$r = 'Function';
}
else if ( \is_object($a) ){
$n = \get_class($a);
if ( $n === 'stdClass' ){
$r = str::export($a);
}
else{
$r = $n.' Object';
}
}
else if ( \is_array($a) ){
$r = str::export($a);
}
else if ( \is_resource($a) ){
$r = 'Resource '.get_resource_type($a);
}
else if ( str::is_buid($a) ){
$r = '0x'.bin2hex($a);
}
$st .= $r.PHP_EOL;
}
return PHP_EOL.$st.PHP_EOL;
} | php | public static function get_dump(){
$args = \func_get_args();
$st = '';
foreach ( $args as $a ){
$r = $a;
if ( \is_null($a) ){
$r = 'null';
}
else if ( $a === false ){
$r = 'false';
}
else if ( $a === true ){
$r = 'true';
}
else if ( $a === 0 ){
$r = '0';
}
else if ( $a === '' ){
$r = '""';
}
else if ( $a === [] ){
$r = '[]';
}
else if ( !$a ){
$r = '0';
}
else if ( !\is_string($a) && \is_callable($a) ){
$r = 'Function';
}
else if ( \is_object($a) ){
$n = \get_class($a);
if ( $n === 'stdClass' ){
$r = str::export($a);
}
else{
$r = $n.' Object';
}
}
else if ( \is_array($a) ){
$r = str::export($a);
}
else if ( \is_resource($a) ){
$r = 'Resource '.get_resource_type($a);
}
else if ( str::is_buid($a) ){
$r = '0x'.bin2hex($a);
}
$st .= $r.PHP_EOL;
}
return PHP_EOL.$st.PHP_EOL;
} | [
"public",
"static",
"function",
"get_dump",
"(",
")",
"{",
"$",
"args",
"=",
"\\",
"func_get_args",
"(",
")",
";",
"$",
"st",
"=",
"''",
";",
"foreach",
"(",
"$",
"args",
"as",
"$",
"a",
")",
"{",
"$",
"r",
"=",
"$",
"a",
";",
"if",
"(",
"\\",
"is_null",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"=",
"'null'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"false",
")",
"{",
"$",
"r",
"=",
"'false'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"true",
")",
"{",
"$",
"r",
"=",
"'true'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"0",
")",
"{",
"$",
"r",
"=",
"'0'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"''",
")",
"{",
"$",
"r",
"=",
"'\"\"'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"[",
"]",
")",
"{",
"$",
"r",
"=",
"'[]'",
";",
"}",
"else",
"if",
"(",
"!",
"$",
"a",
")",
"{",
"$",
"r",
"=",
"'0'",
";",
"}",
"else",
"if",
"(",
"!",
"\\",
"is_string",
"(",
"$",
"a",
")",
"&&",
"\\",
"is_callable",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"=",
"'Function'",
";",
"}",
"else",
"if",
"(",
"\\",
"is_object",
"(",
"$",
"a",
")",
")",
"{",
"$",
"n",
"=",
"\\",
"get_class",
"(",
"$",
"a",
")",
";",
"if",
"(",
"$",
"n",
"===",
"'stdClass'",
")",
"{",
"$",
"r",
"=",
"str",
"::",
"export",
"(",
"$",
"a",
")",
";",
"}",
"else",
"{",
"$",
"r",
"=",
"$",
"n",
".",
"' Object'",
";",
"}",
"}",
"else",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"=",
"str",
"::",
"export",
"(",
"$",
"a",
")",
";",
"}",
"else",
"if",
"(",
"\\",
"is_resource",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"=",
"'Resource '",
".",
"get_resource_type",
"(",
"$",
"a",
")",
";",
"}",
"else",
"if",
"(",
"str",
"::",
"is_buid",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"=",
"'0x'",
".",
"bin2hex",
"(",
"$",
"a",
")",
";",
"}",
"$",
"st",
".=",
"$",
"r",
".",
"PHP_EOL",
";",
"}",
"return",
"PHP_EOL",
".",
"$",
"st",
".",
"PHP_EOL",
";",
"}"
] | Returns a dump of the given variable.
@param mixed
@return string | [
"Returns",
"a",
"dump",
"of",
"the",
"given",
"variable",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L504-L554 |
nabab/bbn | src/bbn/x.php | x.build_options | public static function build_options($values, $selected='', $empty_label=false){
if ( \is_array($values) )
{
$r = '';
if ( $empty_label !== false ){
$r .= '<option value="">'.$empty_label.'</option>';
}
$is_assoc = self::is_assoc($values);
foreach ( $values as $k => $v )
{
if ( \is_array($v) && \count($v) == 2 )
{
$value = $v[0];
$title = $v[1];
}
else if ( !isset($values[0]) && $is_assoc ){
$value = $k;
$title = $v;
}
else {
$value = $title = $v;
}
if ( isset($value,$title) ){
$r .= '<option value="'.$value.'"'.
( $value == $selected ? ' selected="selected"' : '').
'>'.$title.'</option>';
}
unset($value,$title);
}
return $r;
}
} | php | public static function build_options($values, $selected='', $empty_label=false){
if ( \is_array($values) )
{
$r = '';
if ( $empty_label !== false ){
$r .= '<option value="">'.$empty_label.'</option>';
}
$is_assoc = self::is_assoc($values);
foreach ( $values as $k => $v )
{
if ( \is_array($v) && \count($v) == 2 )
{
$value = $v[0];
$title = $v[1];
}
else if ( !isset($values[0]) && $is_assoc ){
$value = $k;
$title = $v;
}
else {
$value = $title = $v;
}
if ( isset($value,$title) ){
$r .= '<option value="'.$value.'"'.
( $value == $selected ? ' selected="selected"' : '').
'>'.$title.'</option>';
}
unset($value,$title);
}
return $r;
}
} | [
"public",
"static",
"function",
"build_options",
"(",
"$",
"values",
",",
"$",
"selected",
"=",
"''",
",",
"$",
"empty_label",
"=",
"false",
")",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"values",
")",
")",
"{",
"$",
"r",
"=",
"''",
";",
"if",
"(",
"$",
"empty_label",
"!==",
"false",
")",
"{",
"$",
"r",
".=",
"'<option value=\"\">'",
".",
"$",
"empty_label",
".",
"'</option>'",
";",
"}",
"$",
"is_assoc",
"=",
"self",
"::",
"is_assoc",
"(",
"$",
"values",
")",
";",
"foreach",
"(",
"$",
"values",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"v",
")",
"&&",
"\\",
"count",
"(",
"$",
"v",
")",
"==",
"2",
")",
"{",
"$",
"value",
"=",
"$",
"v",
"[",
"0",
"]",
";",
"$",
"title",
"=",
"$",
"v",
"[",
"1",
"]",
";",
"}",
"else",
"if",
"(",
"!",
"isset",
"(",
"$",
"values",
"[",
"0",
"]",
")",
"&&",
"$",
"is_assoc",
")",
"{",
"$",
"value",
"=",
"$",
"k",
";",
"$",
"title",
"=",
"$",
"v",
";",
"}",
"else",
"{",
"$",
"value",
"=",
"$",
"title",
"=",
"$",
"v",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"value",
",",
"$",
"title",
")",
")",
"{",
"$",
"r",
".=",
"'<option value=\"'",
".",
"$",
"value",
".",
"'\"'",
".",
"(",
"$",
"value",
"==",
"$",
"selected",
"?",
"' selected=\"selected\"'",
":",
"''",
")",
".",
"'>'",
".",
"$",
"title",
".",
"'</option>'",
";",
"}",
"unset",
"(",
"$",
"value",
",",
"$",
"title",
")",
";",
"}",
"return",
"$",
"r",
";",
"}",
"}"
] | Returns the HTML code for creating the <option> tag(s) based on an array.
If the array is indexed, the index will be used as value
```php
\bbn\x::build_options(['yes', 'no']);
// string "<option value="yes">yes</option>;<option value="no">no</option>"
\bbn\x::build_options(['yes', 'no'], 'no');
// string "<option value="yes">yes</option><option value="no" selected="selected">no</option>"
\bbn\x::build_options(['yes', 'no'], 'no', 'LabelForEmpty');
// string "<option value="">LabelForEmpty</option><option value="yes">yes</option><option value="no" selected="selected">no</option>"
\bbn\x::dump(\bbn\x::build_options([3 => "Allison", 4 => "Mike", 5 => "Andrew"], 5, 'Who?'));
// string "<option value="">Who?</option><option value="3">Allison</option><option value="4">Mike</option><option value="5" selected="selected">Andrew</option>"
```
@param array $values The source array for the options
@param mixed $selected The selected value
@param boolean $empty_label A label for empty value
@return string The HTML code. | [
"Returns",
"the",
"HTML",
"code",
"for",
"creating",
"the",
"<",
";",
"option>",
";",
"tag",
"(",
"s",
")",
"based",
"on",
"an",
"array",
".",
"If",
"the",
"array",
"is",
"indexed",
"the",
"index",
"will",
"be",
"used",
"as",
"value"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L608-L639 |
nabab/bbn | src/bbn/x.php | x.to_keypair | public static function to_keypair($arr, $protected = 1){
$num = \count($arr);
$res = [];
if ( ($num % 2) === 0 ){
$i = 0;
while ( isset($arr[$i]) ){
if ( !\is_string($arr[$i]) || ( !$protected && !preg_match('/[0-9A-z\-_]+/8', str::cast($arr[$i])) ) ){
return false;
}
$res[$arr[$i]] = $arr[$i+1];
$i += 2;
}
}
return $res;
} | php | public static function to_keypair($arr, $protected = 1){
$num = \count($arr);
$res = [];
if ( ($num % 2) === 0 ){
$i = 0;
while ( isset($arr[$i]) ){
if ( !\is_string($arr[$i]) || ( !$protected && !preg_match('/[0-9A-z\-_]+/8', str::cast($arr[$i])) ) ){
return false;
}
$res[$arr[$i]] = $arr[$i+1];
$i += 2;
}
}
return $res;
} | [
"public",
"static",
"function",
"to_keypair",
"(",
"$",
"arr",
",",
"$",
"protected",
"=",
"1",
")",
"{",
"$",
"num",
"=",
"\\",
"count",
"(",
"$",
"arr",
")",
";",
"$",
"res",
"=",
"[",
"]",
";",
"if",
"(",
"(",
"$",
"num",
"%",
"2",
")",
"===",
"0",
")",
"{",
"$",
"i",
"=",
"0",
";",
"while",
"(",
"isset",
"(",
"$",
"arr",
"[",
"$",
"i",
"]",
")",
")",
"{",
"if",
"(",
"!",
"\\",
"is_string",
"(",
"$",
"arr",
"[",
"$",
"i",
"]",
")",
"||",
"(",
"!",
"$",
"protected",
"&&",
"!",
"preg_match",
"(",
"'/[0-9A-z\\-_]+/8'",
",",
"str",
"::",
"cast",
"(",
"$",
"arr",
"[",
"$",
"i",
"]",
")",
")",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"res",
"[",
"$",
"arr",
"[",
"$",
"i",
"]",
"]",
"=",
"$",
"arr",
"[",
"$",
"i",
"+",
"1",
"]",
";",
"$",
"i",
"+=",
"2",
";",
"}",
"}",
"return",
"$",
"res",
";",
"}"
] | Converts a numeric array into an associative one, alternating key and value.
```php
\bbn\x::to_keypair(['Test', 'TestFile', 'Example', 'ExampleFile']);
// string ['Test' => 'TestFile', 'Example' => 'ExampleFile']
```
@param array $arr The array. It must contain an even number of values
@param bool $protected If false no index protection will be performed
@return array|false | [
"Converts",
"a",
"numeric",
"array",
"into",
"an",
"associative",
"one",
"alternating",
"key",
"and",
"value",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L653-L667 |
nabab/bbn | src/bbn/x.php | x.max_with_key | public static function max_with_key($ar, $key){
if (!\is_array($ar) || \count($ar) == 0) return false;
$max = current($ar)[$key];
foreach ( $ar as $a ){
if ( is_float($a[$key]) || is_float($max) ){
if ( self::compare_floats($a[$key], $max, '>') ){
$max = $a[$key];
}
}
else if( $a[$key] > $max ){
$max = $a[$key];
}
}
return $max;
} | php | public static function max_with_key($ar, $key){
if (!\is_array($ar) || \count($ar) == 0) return false;
$max = current($ar)[$key];
foreach ( $ar as $a ){
if ( is_float($a[$key]) || is_float($max) ){
if ( self::compare_floats($a[$key], $max, '>') ){
$max = $a[$key];
}
}
else if( $a[$key] > $max ){
$max = $a[$key];
}
}
return $max;
} | [
"public",
"static",
"function",
"max_with_key",
"(",
"$",
"ar",
",",
"$",
"key",
")",
"{",
"if",
"(",
"!",
"\\",
"is_array",
"(",
"$",
"ar",
")",
"||",
"\\",
"count",
"(",
"$",
"ar",
")",
"==",
"0",
")",
"return",
"false",
";",
"$",
"max",
"=",
"current",
"(",
"$",
"ar",
")",
"[",
"$",
"key",
"]",
";",
"foreach",
"(",
"$",
"ar",
"as",
"$",
"a",
")",
"{",
"if",
"(",
"is_float",
"(",
"$",
"a",
"[",
"$",
"key",
"]",
")",
"||",
"is_float",
"(",
"$",
"max",
")",
")",
"{",
"if",
"(",
"self",
"::",
"compare_floats",
"(",
"$",
"a",
"[",
"$",
"key",
"]",
",",
"$",
"max",
",",
"'>'",
")",
")",
"{",
"$",
"max",
"=",
"$",
"a",
"[",
"$",
"key",
"]",
";",
"}",
"}",
"else",
"if",
"(",
"$",
"a",
"[",
"$",
"key",
"]",
">",
"$",
"max",
")",
"{",
"$",
"max",
"=",
"$",
"a",
"[",
"$",
"key",
"]",
";",
"}",
"}",
"return",
"$",
"max",
";",
"}"
] | Returns the maximum value of a given property from a 2 dimensions array.
@todo Add a custom callable as last parameter
```php
\bbn\x::max_with_key([
['age' => 1, 'name' => 'Michelle'],
['age' => 8, 'name' => 'John'],
['age' => 45, 'name' => 'Sarah'],
['age' => 45, 'name' => 'Camilla'],
['age' => 2, 'name' => 'Allison']
], 'age');
// int 45
```
@param array $ar A multidimensional array
@param string $key Where to check the property value from
@return mixed | [
"Returns",
"the",
"maximum",
"value",
"of",
"a",
"given",
"property",
"from",
"a",
"2",
"dimensions",
"array",
".",
"@todo",
"Add",
"a",
"custom",
"callable",
"as",
"last",
"parameter"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L688-L702 |
nabab/bbn | src/bbn/x.php | x.min_with_key | public static function min_with_key($array, $key){
if (!\is_array($array) || \count($array) == 0) return false;
$min = $array[0][$key];
foreach($array as $a){
if($a[$key] < $min){
$min = $a[$key];
}
}
return $min;
} | php | public static function min_with_key($array, $key){
if (!\is_array($array) || \count($array) == 0) return false;
$min = $array[0][$key];
foreach($array as $a){
if($a[$key] < $min){
$min = $a[$key];
}
}
return $min;
} | [
"public",
"static",
"function",
"min_with_key",
"(",
"$",
"array",
",",
"$",
"key",
")",
"{",
"if",
"(",
"!",
"\\",
"is_array",
"(",
"$",
"array",
")",
"||",
"\\",
"count",
"(",
"$",
"array",
")",
"==",
"0",
")",
"return",
"false",
";",
"$",
"min",
"=",
"$",
"array",
"[",
"0",
"]",
"[",
"$",
"key",
"]",
";",
"foreach",
"(",
"$",
"array",
"as",
"$",
"a",
")",
"{",
"if",
"(",
"$",
"a",
"[",
"$",
"key",
"]",
"<",
"$",
"min",
")",
"{",
"$",
"min",
"=",
"$",
"a",
"[",
"$",
"key",
"]",
";",
"}",
"}",
"return",
"$",
"min",
";",
"}"
] | Returns the minimum value of an index from a multidimensional array.
```php
\bbn\x::min_with_key([
['age' => 1, 'name' => 'Michelle'],
['age' => 8, 'name' => 'John'],
['age' => 45, 'name' => 'Sarah'],
['age' => 45, 'name' => 'Camilla'],
['age' => 2, 'name' => 'Allison']
], 'age');
// int 1
```
@param array $array A multidimensional array.
@param string $key The index where to search.
@return mixed value | [
"Returns",
"the",
"minimum",
"value",
"of",
"an",
"index",
"from",
"a",
"multidimensional",
"array",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L722-L731 |
nabab/bbn | src/bbn/x.php | x.debug | public static function debug($file=''){
$debug = array_map(function($a){
if ( isset($a['object']) ){
unset($a['object']);
}
return $a;
}, debug_backtrace());
if ( empty($file) ){
self::hdump($debug);
}
else{
self::log($debug, $file);
}
} | php | public static function debug($file=''){
$debug = array_map(function($a){
if ( isset($a['object']) ){
unset($a['object']);
}
return $a;
}, debug_backtrace());
if ( empty($file) ){
self::hdump($debug);
}
else{
self::log($debug, $file);
}
} | [
"public",
"static",
"function",
"debug",
"(",
"$",
"file",
"=",
"''",
")",
"{",
"$",
"debug",
"=",
"array_map",
"(",
"function",
"(",
"$",
"a",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"a",
"[",
"'object'",
"]",
")",
")",
"{",
"unset",
"(",
"$",
"a",
"[",
"'object'",
"]",
")",
";",
"}",
"return",
"$",
"a",
";",
"}",
",",
"debug_backtrace",
"(",
")",
")",
";",
"if",
"(",
"empty",
"(",
"$",
"file",
")",
")",
"{",
"self",
"::",
"hdump",
"(",
"$",
"debug",
")",
";",
"}",
"else",
"{",
"self",
"::",
"log",
"(",
"$",
"debug",
",",
"$",
"file",
")",
";",
"}",
"}"
] | Gets the backtrace and dumps or logs it into a file.
```php
\bbn\x::dump(\bbn\x::debug());
```
@param string $file The file to debug
@return void | [
"Gets",
"the",
"backtrace",
"and",
"dumps",
"or",
"logs",
"it",
"into",
"a",
"file",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L742-L755 |
nabab/bbn | src/bbn/x.php | x.map | public static function map(callable $fn, array $ar, string $items = null){
$res = [];
foreach ( $ar as $i => $a ){
$is_false = $a === false;
$r = $fn($a);
if ( $is_false ){
$res[] = $r;
}
else if ( $r !== false ){
if ( \is_array($r) && $items && isset($r[$items]) && \is_array($r[$items]) ){
$r[$items] = self::map($fn, $r[$items], $items);
}
$res[] = $r;
}
}
return $res;
} | php | public static function map(callable $fn, array $ar, string $items = null){
$res = [];
foreach ( $ar as $i => $a ){
$is_false = $a === false;
$r = $fn($a);
if ( $is_false ){
$res[] = $r;
}
else if ( $r !== false ){
if ( \is_array($r) && $items && isset($r[$items]) && \is_array($r[$items]) ){
$r[$items] = self::map($fn, $r[$items], $items);
}
$res[] = $r;
}
}
return $res;
} | [
"public",
"static",
"function",
"map",
"(",
"callable",
"$",
"fn",
",",
"array",
"$",
"ar",
",",
"string",
"$",
"items",
"=",
"null",
")",
"{",
"$",
"res",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"ar",
"as",
"$",
"i",
"=>",
"$",
"a",
")",
"{",
"$",
"is_false",
"=",
"$",
"a",
"===",
"false",
";",
"$",
"r",
"=",
"$",
"fn",
"(",
"$",
"a",
")",
";",
"if",
"(",
"$",
"is_false",
")",
"{",
"$",
"res",
"[",
"]",
"=",
"$",
"r",
";",
"}",
"else",
"if",
"(",
"$",
"r",
"!==",
"false",
")",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"r",
")",
"&&",
"$",
"items",
"&&",
"isset",
"(",
"$",
"r",
"[",
"$",
"items",
"]",
")",
"&&",
"\\",
"is_array",
"(",
"$",
"r",
"[",
"$",
"items",
"]",
")",
")",
"{",
"$",
"r",
"[",
"$",
"items",
"]",
"=",
"self",
"::",
"map",
"(",
"$",
"fn",
",",
"$",
"r",
"[",
"$",
"items",
"]",
",",
"$",
"items",
")",
";",
"}",
"$",
"res",
"[",
"]",
"=",
"$",
"r",
";",
"}",
"}",
"return",
"$",
"res",
";",
"}"
] | Applies the given function at all levels of a multidimensional array (if defined param $item).
```php
$ar = [
['age' => 45,
'name' => 'John',
'children' => [
['age' => 8, 'name' => 'Carol'],
['age' => 24, 'name' => 'Jack'],
]
],
['age' => 44, 'name' => 'Benjamin'],
['age' => 60, 'name' => 'Paul', 'children' =>
[
['age' => 36, 'name' => 'Mike'],
['age' => 46, 'name' => 'Alan', 'children' =>
['age' => 8, 'name' => 'Allison'],
]
]
]
];
\bbn\x::hdump(\bbn\x::map(function($a){
if ( $a['age']>20){
$a['name'] = 'Mr. '.$a['name'];
}
return $a;
}, $ar,'children'));
/* array [
[
"age" => 45,
"name" => "Mr. John",
"children" => [
[
"age" => 8,
"name" => "Carol",
],
[
"age" => 24,
"name" => "Mr. Jack",
],
],
],
[
"age" => 44,
"name" => "Mr. Benjamin",
],
[
"age" => 60,
"name" => "Mr. Paul",
"children" => [
[
"age" => 36,
"name" => "Mr. Mike",
],
[
"age" => 46,
"name" => "Mr. Alan",
"children" => [
"age" => 8,
"name" => "Allison",
],
],
],
]
```
@param callable $fn The function to be applied to the items of the array
@param array $ar
@param string|null $items If null the function will be applied just to the item of the parent array
@return array | [
"Applies",
"the",
"given",
"function",
"at",
"all",
"levels",
"of",
"a",
"multidimensional",
"array",
"(",
"if",
"defined",
"param",
"$item",
")",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L829-L845 |
nabab/bbn | src/bbn/x.php | x.find | public static function find(array $ar, array $where){
//die(var_dump($where));
if ( !empty($where) ){
foreach ( $ar as $i => $v ){
$ok = 1;
foreach ( $where as $k => $w ){
if ( !isset($v[$k]) || ($v[$k] !== $w) ){
$ok = false;
break;
}
}
if ( $ok ){
return $i;
}
}
}
return false;
} | php | public static function find(array $ar, array $where){
//die(var_dump($where));
if ( !empty($where) ){
foreach ( $ar as $i => $v ){
$ok = 1;
foreach ( $where as $k => $w ){
if ( !isset($v[$k]) || ($v[$k] !== $w) ){
$ok = false;
break;
}
}
if ( $ok ){
return $i;
}
}
}
return false;
} | [
"public",
"static",
"function",
"find",
"(",
"array",
"$",
"ar",
",",
"array",
"$",
"where",
")",
"{",
"//die(var_dump($where));",
"if",
"(",
"!",
"empty",
"(",
"$",
"where",
")",
")",
"{",
"foreach",
"(",
"$",
"ar",
"as",
"$",
"i",
"=>",
"$",
"v",
")",
"{",
"$",
"ok",
"=",
"1",
";",
"foreach",
"(",
"$",
"where",
"as",
"$",
"k",
"=>",
"$",
"w",
")",
"{",
"if",
"(",
"!",
"isset",
"(",
"$",
"v",
"[",
"$",
"k",
"]",
")",
"||",
"(",
"$",
"v",
"[",
"$",
"k",
"]",
"!==",
"$",
"w",
")",
")",
"{",
"$",
"ok",
"=",
"false",
";",
"break",
";",
"}",
"}",
"if",
"(",
"$",
"ok",
")",
"{",
"return",
"$",
"i",
";",
"}",
"}",
"}",
"return",
"false",
";",
"}"
] | Returns the array's first index, which satisfies the 'where' condition.
```php
\bbn\x::hdump(\bbn\x::find([[
'id' => 1,
'name' => 'Andrew',
'fname' => 'Williams'
], [
'id' => 2,
'name' => 'Albert',
'fname' => 'Taylor'
], [
'id' => 3,
'name' => 'Mike',
'fname' => 'Smith'
], [
'id' => 4,
'name' => 'John',
'fname' => 'White'
]], ['id' => 4]));
// int 3
\bbn\x::hdump(\bbn\x::find([[
'id' => 1,
'name' => 'Andrew',
'fname' => 'Williams'
], [
'id' => 2,
'name' => 'Albert',
'fname' => 'Taylor'
], [
'id' => 3,
'name' => 'Mike',
'fname' => 'Smith'
], [
'id' => 4,
'name' => 'John',
'fname' => 'White'
]], ['name' => 'Albert', 'fname' => 'Taylor']));
// int 1
```
@param array $ar The search within the array
@param array $where The where condition
@return bool|int | [
"Returns",
"the",
"array",
"s",
"first",
"index",
"which",
"satisfies",
"the",
"where",
"condition",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L893-L910 |
nabab/bbn | src/bbn/x.php | x.get_row | public static function get_row(array $r, array $where){
if ( ($res = self::find($r, $where)) !== false ){
return $r[$res];
}
return false;
} | php | public static function get_row(array $r, array $where){
if ( ($res = self::find($r, $where)) !== false ){
return $r[$res];
}
return false;
} | [
"public",
"static",
"function",
"get_row",
"(",
"array",
"$",
"r",
",",
"array",
"$",
"where",
")",
"{",
"if",
"(",
"(",
"$",
"res",
"=",
"self",
"::",
"find",
"(",
"$",
"r",
",",
"$",
"where",
")",
")",
"!==",
"false",
")",
"{",
"return",
"$",
"r",
"[",
"$",
"res",
"]",
";",
"}",
"return",
"false",
";",
"}"
] | Returns the first row of an array to satisfy the where parameters ({@link find()).
```php
\bbn\x::dump(\bbn\x::get_row([[
'id' => 1,
'name' => 'Andrew',
'fname' => 'Williams'
], [
'id' => 2,
'name' => 'Albert',
'fname' => 'Taylor'
], [
'id' => 3,
'name' => 'Mike',
'fname' => 'Smith'
], [
'id' => 4,
'name' => 'John',
'fname' => 'White'
]], ['name' => 'Albert']));
// array [ "id" => 2, "name" => "Albert", "fname" => "Taylor", ]
```
@param array $r The array
@param array $where The where condition
@return bool|mixed | [
"Returns",
"the",
"first",
"row",
"of",
"an",
"array",
"to",
"satisfy",
"the",
"where",
"parameters",
"(",
"{",
"@link",
"find",
"()",
")",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L941-L946 |
nabab/bbn | src/bbn/x.php | x.get_field | public static function get_field(array $r, array $where, string $field){
if ( ($res = self::get_row($r, $where)) && isset($res[$field]) ){
return $res[$field];
}
return false;
} | php | public static function get_field(array $r, array $where, string $field){
if ( ($res = self::get_row($r, $where)) && isset($res[$field]) ){
return $res[$field];
}
return false;
} | [
"public",
"static",
"function",
"get_field",
"(",
"array",
"$",
"r",
",",
"array",
"$",
"where",
",",
"string",
"$",
"field",
")",
"{",
"if",
"(",
"(",
"$",
"res",
"=",
"self",
"::",
"get_row",
"(",
"$",
"r",
",",
"$",
"where",
")",
")",
"&&",
"isset",
"(",
"$",
"res",
"[",
"$",
"field",
"]",
")",
")",
"{",
"return",
"$",
"res",
"[",
"$",
"field",
"]",
";",
"}",
"return",
"false",
";",
"}"
] | Returns the first value of a specific field of an array.
```php
\bbn\x::dump(\bbn\x::get_row([[
'id' => 1,
'name' => 'Andrew',
'fname' => 'Williams'
], [
'id' => 2,
'name' => 'Albert',
'fname' => 'Taylor'
], [
'id' => 3,
'name' => 'Mike',
'fname' => 'Smith'
], [
'id' => 4,
'name' => 'John',
'fname' => 'White'
]], ['name' => 'Albert'],'id'));
// int 2
```
@param array $r The array
@param array $where The where condition
@param string $field The field where to look for
@return bool|mixed | [
"Returns",
"the",
"first",
"value",
"of",
"a",
"specific",
"field",
"of",
"an",
"array",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L977-L982 |
nabab/bbn | src/bbn/x.php | x.pick | public static function pick(array $ar, array $keys){
while ( \count($keys) ){
$r = array_shift($keys);
if ( isset($ar[$r]) ){
$ar = $ar[$r];
if ( !count($keys) ){
return $ar;
}
}
}
} | php | public static function pick(array $ar, array $keys){
while ( \count($keys) ){
$r = array_shift($keys);
if ( isset($ar[$r]) ){
$ar = $ar[$r];
if ( !count($keys) ){
return $ar;
}
}
}
} | [
"public",
"static",
"function",
"pick",
"(",
"array",
"$",
"ar",
",",
"array",
"$",
"keys",
")",
"{",
"while",
"(",
"\\",
"count",
"(",
"$",
"keys",
")",
")",
"{",
"$",
"r",
"=",
"array_shift",
"(",
"$",
"keys",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"ar",
"[",
"$",
"r",
"]",
")",
")",
"{",
"$",
"ar",
"=",
"$",
"ar",
"[",
"$",
"r",
"]",
";",
"if",
"(",
"!",
"count",
"(",
"$",
"keys",
")",
")",
"{",
"return",
"$",
"ar",
";",
"}",
"}",
"}",
"}"
] | Returns a reference to a subarray targeted by an array $keys.
```php
$ar = [
'session' => [
'user' => [
'profile' => [
'admin' => [
'email' => '[email protected]'
]
]
]
]
];
\bbn\x::hdump(\bbn\x::pick($ar,['session', 'user', 'profile', 'admin', 'email']));
// string [email protected]
\bbn\x::hdump(\bbn\x::pick($ar,['session', 'user', 'profile', 'admin']));
// ["email" => "[email protected]",]
```
@param array $ar The array
@param array $keys The array's keys
@return array|mixed | [
"Returns",
"a",
"reference",
"to",
"a",
"subarray",
"targeted",
"by",
"an",
"array",
"$keys",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1009-L1019 |
nabab/bbn | src/bbn/x.php | x.sort | public static function sort(&$ar){
usort($ar, function($a, $b){
if ( !str::is_number($a, $b) ){
$a = str_replace('.', '0', str_replace('_', '1', str::change_case($a, 'lower')));
$b = str_replace('.', '0', str_replace('_', '1', str::change_case($b, 'lower')));
return strcmp($a, $b);
}
if ( $a > $b ){
return 1;
}
else if ($a == $b){
return 0;
}
return -1;
});
} | php | public static function sort(&$ar){
usort($ar, function($a, $b){
if ( !str::is_number($a, $b) ){
$a = str_replace('.', '0', str_replace('_', '1', str::change_case($a, 'lower')));
$b = str_replace('.', '0', str_replace('_', '1', str::change_case($b, 'lower')));
return strcmp($a, $b);
}
if ( $a > $b ){
return 1;
}
else if ($a == $b){
return 0;
}
return -1;
});
} | [
"public",
"static",
"function",
"sort",
"(",
"&",
"$",
"ar",
")",
"{",
"usort",
"(",
"$",
"ar",
",",
"function",
"(",
"$",
"a",
",",
"$",
"b",
")",
"{",
"if",
"(",
"!",
"str",
"::",
"is_number",
"(",
"$",
"a",
",",
"$",
"b",
")",
")",
"{",
"$",
"a",
"=",
"str_replace",
"(",
"'.'",
",",
"'0'",
",",
"str_replace",
"(",
"'_'",
",",
"'1'",
",",
"str",
"::",
"change_case",
"(",
"$",
"a",
",",
"'lower'",
")",
")",
")",
";",
"$",
"b",
"=",
"str_replace",
"(",
"'.'",
",",
"'0'",
",",
"str_replace",
"(",
"'_'",
",",
"'1'",
",",
"str",
"::",
"change_case",
"(",
"$",
"b",
",",
"'lower'",
")",
")",
")",
";",
"return",
"strcmp",
"(",
"$",
"a",
",",
"$",
"b",
")",
";",
"}",
"if",
"(",
"$",
"a",
">",
"$",
"b",
")",
"{",
"return",
"1",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"==",
"$",
"b",
")",
"{",
"return",
"0",
";",
"}",
"return",
"-",
"1",
";",
"}",
")",
";",
"}"
] | Sorts the items of an array.
```php
$var = [3, 2, 5, 6, 1];
\bbn\x::sort($var);
\bbn\x::hdump($var);
// array [1,2,3,5,6]
```
@param $ar array The reference of the array to sort
@return void | [
"Sorts",
"the",
"items",
"of",
"an",
"array",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1035-L1050 |
nabab/bbn | src/bbn/x.php | x.sort_by | public static function sort_by(&$ar, $key, $dir = ''){
$args = \func_get_args();
array_shift($args);
if ( \is_string($key) ){
$args = [[
'key' => $key,
'dir' => $dir
]];
}
usort($ar, function($a, $b) use($args){
foreach ( $args as $arg ){
$key = $arg['key'];
$dir = $arg['dir'] ?? 'asc';
if ( !\is_array($key) ){
$key = [$key];
}
$v1 = self::pick($a, $key);
$v2 = self::pick($b, $key);
$a1 = strtolower($dir) === 'desc' ? ($v2 ?? null) : ($v1 ?? null);
$a2 = strtolower($dir) === 'desc' ? ($v1 ?? null) : ($v2 ?? null);
if ( !str::is_number($v1, $v2) ){
$a1 = str_replace('.', '0', str_replace('_', '1', str::change_case($a1, 'lower')));
$a2 = str_replace('.', '0', str_replace('_', '1', str::change_case($a2, 'lower')));
$cmp = strcmp($a1, $a2);
if ( !empty($cmp) ){
return $cmp;
}
}
if ( $a1 > $a2 ){
return 1;
}
else if ( $a1 < $a2 ){
return -1;
}
}
return 0;
});
} | php | public static function sort_by(&$ar, $key, $dir = ''){
$args = \func_get_args();
array_shift($args);
if ( \is_string($key) ){
$args = [[
'key' => $key,
'dir' => $dir
]];
}
usort($ar, function($a, $b) use($args){
foreach ( $args as $arg ){
$key = $arg['key'];
$dir = $arg['dir'] ?? 'asc';
if ( !\is_array($key) ){
$key = [$key];
}
$v1 = self::pick($a, $key);
$v2 = self::pick($b, $key);
$a1 = strtolower($dir) === 'desc' ? ($v2 ?? null) : ($v1 ?? null);
$a2 = strtolower($dir) === 'desc' ? ($v1 ?? null) : ($v2 ?? null);
if ( !str::is_number($v1, $v2) ){
$a1 = str_replace('.', '0', str_replace('_', '1', str::change_case($a1, 'lower')));
$a2 = str_replace('.', '0', str_replace('_', '1', str::change_case($a2, 'lower')));
$cmp = strcmp($a1, $a2);
if ( !empty($cmp) ){
return $cmp;
}
}
if ( $a1 > $a2 ){
return 1;
}
else if ( $a1 < $a2 ){
return -1;
}
}
return 0;
});
} | [
"public",
"static",
"function",
"sort_by",
"(",
"&",
"$",
"ar",
",",
"$",
"key",
",",
"$",
"dir",
"=",
"''",
")",
"{",
"$",
"args",
"=",
"\\",
"func_get_args",
"(",
")",
";",
"array_shift",
"(",
"$",
"args",
")",
";",
"if",
"(",
"\\",
"is_string",
"(",
"$",
"key",
")",
")",
"{",
"$",
"args",
"=",
"[",
"[",
"'key'",
"=>",
"$",
"key",
",",
"'dir'",
"=>",
"$",
"dir",
"]",
"]",
";",
"}",
"usort",
"(",
"$",
"ar",
",",
"function",
"(",
"$",
"a",
",",
"$",
"b",
")",
"use",
"(",
"$",
"args",
")",
"{",
"foreach",
"(",
"$",
"args",
"as",
"$",
"arg",
")",
"{",
"$",
"key",
"=",
"$",
"arg",
"[",
"'key'",
"]",
";",
"$",
"dir",
"=",
"$",
"arg",
"[",
"'dir'",
"]",
"??",
"'asc'",
";",
"if",
"(",
"!",
"\\",
"is_array",
"(",
"$",
"key",
")",
")",
"{",
"$",
"key",
"=",
"[",
"$",
"key",
"]",
";",
"}",
"$",
"v1",
"=",
"self",
"::",
"pick",
"(",
"$",
"a",
",",
"$",
"key",
")",
";",
"$",
"v2",
"=",
"self",
"::",
"pick",
"(",
"$",
"b",
",",
"$",
"key",
")",
";",
"$",
"a1",
"=",
"strtolower",
"(",
"$",
"dir",
")",
"===",
"'desc'",
"?",
"(",
"$",
"v2",
"??",
"null",
")",
":",
"(",
"$",
"v1",
"??",
"null",
")",
";",
"$",
"a2",
"=",
"strtolower",
"(",
"$",
"dir",
")",
"===",
"'desc'",
"?",
"(",
"$",
"v1",
"??",
"null",
")",
":",
"(",
"$",
"v2",
"??",
"null",
")",
";",
"if",
"(",
"!",
"str",
"::",
"is_number",
"(",
"$",
"v1",
",",
"$",
"v2",
")",
")",
"{",
"$",
"a1",
"=",
"str_replace",
"(",
"'.'",
",",
"'0'",
",",
"str_replace",
"(",
"'_'",
",",
"'1'",
",",
"str",
"::",
"change_case",
"(",
"$",
"a1",
",",
"'lower'",
")",
")",
")",
";",
"$",
"a2",
"=",
"str_replace",
"(",
"'.'",
",",
"'0'",
",",
"str_replace",
"(",
"'_'",
",",
"'1'",
",",
"str",
"::",
"change_case",
"(",
"$",
"a2",
",",
"'lower'",
")",
")",
")",
";",
"$",
"cmp",
"=",
"strcmp",
"(",
"$",
"a1",
",",
"$",
"a2",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"cmp",
")",
")",
"{",
"return",
"$",
"cmp",
";",
"}",
"}",
"if",
"(",
"$",
"a1",
">",
"$",
"a2",
")",
"{",
"return",
"1",
";",
"}",
"else",
"if",
"(",
"$",
"a1",
"<",
"$",
"a2",
")",
"{",
"return",
"-",
"1",
";",
"}",
"}",
"return",
"0",
";",
"}",
")",
";",
"}"
] | Sorts the items of an indexed array based on a given $key.
```php
$v = [['age'=>10, 'name'=>'thomas'], ['age'=>22, 'name'=>'John'], ['age'=>37, 'name'=>'Michael']];
\bbn\x::sort_by($v,'name','desc');
\bbn\x::hdump($v);
\bbn\x::sort_by($v,'name','asc');
\bbn\x::hdump($v);
\bbn\x::sort_by($v,'age','asc');
\bbn\x::hdump($v);
\bbn\x::sort_by($v,'age','desc');
\bbn\x::hdump($v);
```
@param array $ar The array of data to sort
@param string|int $key The key to sort by
@param string $dir The direction of the sort ('asc'|'desc')
@return void | [
"Sorts",
"the",
"items",
"of",
"an",
"indexed",
"array",
"based",
"on",
"a",
"given",
"$key",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1072-L1110 |
nabab/bbn | src/bbn/x.php | x.curl | public static function curl(string $url, array $param = null, array $options = ['post' => 1]){
$ch = curl_init();
self::$last_curl = $ch;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (\is_object($param) ){
$param = self::to_array($param);
}
if ( \defined('BBN_IS_SSL') && \defined('BBN_IS_DEV') && BBN_IS_SSL && BBN_IS_DEV ){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
$options = array_change_key_case($options, CASE_UPPER);
foreach ( $options as $opt => $val ){
if ( \defined('CURLOPT_'.$opt) ){
curl_setopt($ch, constant('CURLOPT_'.$opt), $val);
}
}
if ( $param ){
if ( !empty($options['POST']) ){
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
}
else if ( !empty($options['DELETE']) ){
//die($url.'?'.http_build_query($param));
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($param));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
else{
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($param));
}
}
else{
curl_setopt($ch, CURLOPT_URL, $url);
if ( !empty($options['DELETE']) ){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
}
$r = curl_exec($ch);
if ( !$r ){
self::log(["PROBLEME AVEC L'URL $url", curl_error($ch), curl_getinfo($ch)], 'curl');
}
return $r;
} | php | public static function curl(string $url, array $param = null, array $options = ['post' => 1]){
$ch = curl_init();
self::$last_curl = $ch;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if (\is_object($param) ){
$param = self::to_array($param);
}
if ( \defined('BBN_IS_SSL') && \defined('BBN_IS_DEV') && BBN_IS_SSL && BBN_IS_DEV ){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);
}
$options = array_change_key_case($options, CASE_UPPER);
foreach ( $options as $opt => $val ){
if ( \defined('CURLOPT_'.$opt) ){
curl_setopt($ch, constant('CURLOPT_'.$opt), $val);
}
}
if ( $param ){
if ( !empty($options['POST']) ){
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $param);
}
else if ( !empty($options['DELETE']) ){
//die($url.'?'.http_build_query($param));
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($param));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
else{
curl_setopt($ch, CURLOPT_URL, $url.'?'.http_build_query($param));
}
}
else{
curl_setopt($ch, CURLOPT_URL, $url);
if ( !empty($options['DELETE']) ){
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
}
$r = curl_exec($ch);
if ( !$r ){
self::log(["PROBLEME AVEC L'URL $url", curl_error($ch), curl_getinfo($ch)], 'curl');
}
return $r;
} | [
"public",
"static",
"function",
"curl",
"(",
"string",
"$",
"url",
",",
"array",
"$",
"param",
"=",
"null",
",",
"array",
"$",
"options",
"=",
"[",
"'post'",
"=>",
"1",
"]",
")",
"{",
"$",
"ch",
"=",
"curl_init",
"(",
")",
";",
"self",
"::",
"$",
"last_curl",
"=",
"$",
"ch",
";",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_RETURNTRANSFER",
",",
"1",
")",
";",
"if",
"(",
"\\",
"is_object",
"(",
"$",
"param",
")",
")",
"{",
"$",
"param",
"=",
"self",
"::",
"to_array",
"(",
"$",
"param",
")",
";",
"}",
"if",
"(",
"\\",
"defined",
"(",
"'BBN_IS_SSL'",
")",
"&&",
"\\",
"defined",
"(",
"'BBN_IS_DEV'",
")",
"&&",
"BBN_IS_SSL",
"&&",
"BBN_IS_DEV",
")",
"{",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_SSL_VERIFYPEER",
",",
"false",
")",
";",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_SSL_VERIFYHOST",
",",
"false",
")",
";",
"//curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);",
"}",
"$",
"options",
"=",
"array_change_key_case",
"(",
"$",
"options",
",",
"CASE_UPPER",
")",
";",
"foreach",
"(",
"$",
"options",
"as",
"$",
"opt",
"=>",
"$",
"val",
")",
"{",
"if",
"(",
"\\",
"defined",
"(",
"'CURLOPT_'",
".",
"$",
"opt",
")",
")",
"{",
"curl_setopt",
"(",
"$",
"ch",
",",
"constant",
"(",
"'CURLOPT_'",
".",
"$",
"opt",
")",
",",
"$",
"val",
")",
";",
"}",
"}",
"if",
"(",
"$",
"param",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"options",
"[",
"'POST'",
"]",
")",
")",
"{",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_URL",
",",
"$",
"url",
")",
";",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_POSTFIELDS",
",",
"$",
"param",
")",
";",
"}",
"else",
"if",
"(",
"!",
"empty",
"(",
"$",
"options",
"[",
"'DELETE'",
"]",
")",
")",
"{",
"//die($url.'?'.http_build_query($param));",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_URL",
",",
"$",
"url",
".",
"'?'",
".",
"http_build_query",
"(",
"$",
"param",
")",
")",
";",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_CUSTOMREQUEST",
",",
"'DELETE'",
")",
";",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_RETURNTRANSFER",
",",
"true",
")",
";",
"}",
"else",
"{",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_URL",
",",
"$",
"url",
".",
"'?'",
".",
"http_build_query",
"(",
"$",
"param",
")",
")",
";",
"}",
"}",
"else",
"{",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_URL",
",",
"$",
"url",
")",
";",
"if",
"(",
"!",
"empty",
"(",
"$",
"options",
"[",
"'DELETE'",
"]",
")",
")",
"{",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_CUSTOMREQUEST",
",",
"'DELETE'",
")",
";",
"curl_setopt",
"(",
"$",
"ch",
",",
"CURLOPT_RETURNTRANSFER",
",",
"true",
")",
";",
"}",
"}",
"$",
"r",
"=",
"curl_exec",
"(",
"$",
"ch",
")",
";",
"if",
"(",
"!",
"$",
"r",
")",
"{",
"self",
"::",
"log",
"(",
"[",
"\"PROBLEME AVEC L'URL $url\"",
",",
"curl_error",
"(",
"$",
"ch",
")",
",",
"curl_getinfo",
"(",
"$",
"ch",
")",
"]",
",",
"'curl'",
")",
";",
"}",
"return",
"$",
"r",
";",
"}"
] | Makes a Curl call towards a URL and returns the result as a string.
```php
$url = 'https://www.omdbapi.com/';
$param = ['t'=>'la vita è bella'];
\bbn\x::hdump(\bbn\x::curl($url,$param, ['POST' => false]));
// object {"Title":"La vita è bella","Year":"1943","Rated":"N/A","Released":"26 May 1943","Runtime":"76 min","Genre":"Comedy","Director":"Carlo Ludovico Bragaglia","Writer":"Carlo Ludovico Bragaglia (story and screenplay)","Actors":"Alberto Rabagliati, María Mercader, Anna Magnani, Carlo Campanini","Plot":"N/A","Language":"Italian","Country":"Italy","Awards":"N/A","Poster":"http://ia.media-imdb.com/images/M/MV5BYmYyNzA2YWQtNDgyZC00OWVkLWIwMTEtNTdhNDQwZjcwYTMwXkEyXkFqcGdeQXVyNTczNDAyMDc@._V1_SX300.jpg","Metascore":"N/A","imdbRating":"7.9","imdbVotes":"50","imdbID":"tt0036502","Type":"movie","Response":"True"}
```
@param string $url
@param array $param
@param array $options
@return mixed | [
"Makes",
"a",
"Curl",
"call",
"towards",
"a",
"URL",
"and",
"returns",
"the",
"result",
"as",
"a",
"string",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1143-L1189 |
nabab/bbn | src/bbn/x.php | x.get_tree | public static function get_tree($ar){
$res = [];
foreach ( $ar as $k => $a ){
$r = ['text' => $k];
if ( \is_object($a) ){
$a = self::to_array($a);
}
if ( \is_array($a) ){
$r['items'] = self::get_tree($a);
}
else if ( \is_null($a) ){
$r['text'] .= ': null';
}
else if ( $a === false ){
$r['text'] .= ': false';
}
else if ( $a === true ){
$r['text'] .= ': true';
}
else {
$r['text'] .= ': '.(string)$a;
}
array_push($res, $r);
}
return $res;
} | php | public static function get_tree($ar){
$res = [];
foreach ( $ar as $k => $a ){
$r = ['text' => $k];
if ( \is_object($a) ){
$a = self::to_array($a);
}
if ( \is_array($a) ){
$r['items'] = self::get_tree($a);
}
else if ( \is_null($a) ){
$r['text'] .= ': null';
}
else if ( $a === false ){
$r['text'] .= ': false';
}
else if ( $a === true ){
$r['text'] .= ': true';
}
else {
$r['text'] .= ': '.(string)$a;
}
array_push($res, $r);
}
return $res;
} | [
"public",
"static",
"function",
"get_tree",
"(",
"$",
"ar",
")",
"{",
"$",
"res",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"ar",
"as",
"$",
"k",
"=>",
"$",
"a",
")",
"{",
"$",
"r",
"=",
"[",
"'text'",
"=>",
"$",
"k",
"]",
";",
"if",
"(",
"\\",
"is_object",
"(",
"$",
"a",
")",
")",
"{",
"$",
"a",
"=",
"self",
"::",
"to_array",
"(",
"$",
"a",
")",
";",
"}",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"[",
"'items'",
"]",
"=",
"self",
"::",
"get_tree",
"(",
"$",
"a",
")",
";",
"}",
"else",
"if",
"(",
"\\",
"is_null",
"(",
"$",
"a",
")",
")",
"{",
"$",
"r",
"[",
"'text'",
"]",
".=",
"': null'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"false",
")",
"{",
"$",
"r",
"[",
"'text'",
"]",
".=",
"': false'",
";",
"}",
"else",
"if",
"(",
"$",
"a",
"===",
"true",
")",
"{",
"$",
"r",
"[",
"'text'",
"]",
".=",
"': true'",
";",
"}",
"else",
"{",
"$",
"r",
"[",
"'text'",
"]",
".=",
"': '",
".",
"(",
"string",
")",
"$",
"a",
";",
"}",
"array_push",
"(",
"$",
"res",
",",
"$",
"r",
")",
";",
"}",
"return",
"$",
"res",
";",
"}"
] | Returns the given array or object as a tree structure ready for a JS tree.
```php
\bbn\x::hdump(\bbn\x::get_tree([['id' => 1,'name' => 'Andrew','fname' => 'Williams','children' =>[['name' => 'Emma','age' => 6],['name' => 'Giorgio','age' => 9]]], ['id' => 2,'name' => 'Albert','fname' => 'Taylor','children' =>[['name' => 'Esther','age' => 6],['name' => 'Paul','age' => 9]]], ['id' => 3,'name' => 'Mike','fname' => 'Smith','children' =>[['name' => 'Sara','age' => 6],['name' => 'Fred','age' => 9]]]]));
/* array [
[ "text" => 0, "items" => [ [ "text" => "id: 1", ], [ "text" => "name: Andrew", ], [ "text" => "fname: Williams", ], [ "text" => "children", "items" => [ [ "text" => 0, "items" => [ [ "text" => "name: Emma", ], [ "text" => "age: 6", ], ], ], [ "text" => 1, "items" => [ [ "text" => "name: Giorgio", ], [ "text" => "age: 9", ], ], ], ], ], ], ], [ "text" => 1, "items" => [ [ "text" => "id: 2", ], [ "text" => "name: Albert", ], [ "text" => "fname: Taylor", ], [ "text" => "children", "items" => [ [ "text" => 0, "items" => [ [ "text" => "name: Esther", ], [ "text" => "age: 6", ], ], ], [ "text" => 1, "items" => [ [ "text" => "name: Paul", ], [ "text" => "age: 9", ], ], ], ], ], ], ], [ "text" => 2, "items" => [ [ "text" => "id: 3", ], [ "text" => "name: Mike", ], [ "text" => "fname: Smith", ], [ "text" => "children", "items" => [ [ "text" => 0, "items" => [ [ "text" => "name: Sara", ], [ "text" => "age: 6", ], ], ], [ "text" => 1, "items" => [ [ "text" => "name: Fred", ], [ "text" => "age: 9", ], ], ], ], ], ], ], ]
```
@param array $ar
@return array | [
"Returns",
"the",
"given",
"array",
"or",
"object",
"as",
"a",
"tree",
"structure",
"ready",
"for",
"a",
"JS",
"tree",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1227-L1252 |
nabab/bbn | src/bbn/x.php | x.from_csv | public static function from_csv($st, $delimiter = ',', $enclosure = '"', $separator = PHP_EOL){
if ( \is_string($st) ){
$r = [];
$lines = explode($separator, $st);
foreach ( $lines as $line ){
array_push($r, str_getcsv($line, $delimiter, $enclosure));
}
return $r;
}
return [];
} | php | public static function from_csv($st, $delimiter = ',', $enclosure = '"', $separator = PHP_EOL){
if ( \is_string($st) ){
$r = [];
$lines = explode($separator, $st);
foreach ( $lines as $line ){
array_push($r, str_getcsv($line, $delimiter, $enclosure));
}
return $r;
}
return [];
} | [
"public",
"static",
"function",
"from_csv",
"(",
"$",
"st",
",",
"$",
"delimiter",
"=",
"','",
",",
"$",
"enclosure",
"=",
"'\"'",
",",
"$",
"separator",
"=",
"PHP_EOL",
")",
"{",
"if",
"(",
"\\",
"is_string",
"(",
"$",
"st",
")",
")",
"{",
"$",
"r",
"=",
"[",
"]",
";",
"$",
"lines",
"=",
"explode",
"(",
"$",
"separator",
",",
"$",
"st",
")",
";",
"foreach",
"(",
"$",
"lines",
"as",
"$",
"line",
")",
"{",
"array_push",
"(",
"$",
"r",
",",
"str_getcsv",
"(",
"$",
"line",
",",
"$",
"delimiter",
",",
"$",
"enclosure",
")",
")",
";",
"}",
"return",
"$",
"r",
";",
"}",
"return",
"[",
"]",
";",
"}"
] | Formats a CSV line(s) and returns it as an array.
Adapted from http://us3.php.net/manual/en/function.fputcsv.php#87120
```php
\bbn\x::dump(\bbn\x::from_csv(
'"141";"10/11/2002";"350.00";"1311742251"
"142";"12/12/2002";"349.00";"1311742258"'
));
// [ [ "141", "10/11/2002", "350.00", "1311742251", ], [ "142", "12/12/2002", "349.00", "1311742258", ], ]
```
@param $st The Csv string to format
@param string $delimiter
@param string $enclosure
@param string $separator
@return array | [
"Formats",
"a",
"CSV",
"line",
"(",
"s",
")",
"and",
"returns",
"it",
"as",
"an",
"array",
".",
"Adapted",
"from",
"http",
":",
"//",
"us3",
".",
"php",
".",
"net",
"/",
"manual",
"/",
"en",
"/",
"function",
".",
"fputcsv",
".",
"php#87120"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1320-L1330 |
nabab/bbn | src/bbn/x.php | x.to_csv | public static function to_csv(array $data, $delimiter = ';', $enclosure = '"', $separator = PHP_EOL, $encloseAll = false, $nullToMysqlNull = false ){
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$lines = [];
foreach ( $data as $d ){
$output = [];
foreach ( $d as $field ){
if ($field === null && $nullToMysqlNull){
$output[] = 'NULL';
continue;
}
// Enclose fields containing $delimiter, $enclosure or whitespace
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ){
$output[] = $enclosure . str_replace($enclosure, '\\' . $enclosure, $field) . $enclosure;
}
else {
$output[] = $field;
}
}
array_push($lines, implode( $delimiter, $output ));
}
return implode( $separator, $lines );
} | php | public static function to_csv(array $data, $delimiter = ';', $enclosure = '"', $separator = PHP_EOL, $encloseAll = false, $nullToMysqlNull = false ){
$delimiter_esc = preg_quote($delimiter, '/');
$enclosure_esc = preg_quote($enclosure, '/');
$lines = [];
foreach ( $data as $d ){
$output = [];
foreach ( $d as $field ){
if ($field === null && $nullToMysqlNull){
$output[] = 'NULL';
continue;
}
// Enclose fields containing $delimiter, $enclosure or whitespace
if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ){
$output[] = $enclosure . str_replace($enclosure, '\\' . $enclosure, $field) . $enclosure;
}
else {
$output[] = $field;
}
}
array_push($lines, implode( $delimiter, $output ));
}
return implode( $separator, $lines );
} | [
"public",
"static",
"function",
"to_csv",
"(",
"array",
"$",
"data",
",",
"$",
"delimiter",
"=",
"';'",
",",
"$",
"enclosure",
"=",
"'\"'",
",",
"$",
"separator",
"=",
"PHP_EOL",
",",
"$",
"encloseAll",
"=",
"false",
",",
"$",
"nullToMysqlNull",
"=",
"false",
")",
"{",
"$",
"delimiter_esc",
"=",
"preg_quote",
"(",
"$",
"delimiter",
",",
"'/'",
")",
";",
"$",
"enclosure_esc",
"=",
"preg_quote",
"(",
"$",
"enclosure",
",",
"'/'",
")",
";",
"$",
"lines",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"data",
"as",
"$",
"d",
")",
"{",
"$",
"output",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"d",
"as",
"$",
"field",
")",
"{",
"if",
"(",
"$",
"field",
"===",
"null",
"&&",
"$",
"nullToMysqlNull",
")",
"{",
"$",
"output",
"[",
"]",
"=",
"'NULL'",
";",
"continue",
";",
"}",
"// Enclose fields containing $delimiter, $enclosure or whitespace",
"if",
"(",
"$",
"encloseAll",
"||",
"preg_match",
"(",
"\"/(?:${delimiter_esc}|${enclosure_esc}|\\s)/\"",
",",
"$",
"field",
")",
")",
"{",
"$",
"output",
"[",
"]",
"=",
"$",
"enclosure",
".",
"str_replace",
"(",
"$",
"enclosure",
",",
"'\\\\'",
".",
"$",
"enclosure",
",",
"$",
"field",
")",
".",
"$",
"enclosure",
";",
"}",
"else",
"{",
"$",
"output",
"[",
"]",
"=",
"$",
"field",
";",
"}",
"}",
"array_push",
"(",
"$",
"lines",
",",
"implode",
"(",
"$",
"delimiter",
",",
"$",
"output",
")",
")",
";",
"}",
"return",
"implode",
"(",
"$",
"separator",
",",
"$",
"lines",
")",
";",
"}"
] | Formats an array as a CSV string.
Adapted from http://us3.php.net/manual/en/function.fputcsv.php#87120
```php
\bbn\x::dump(\bbn\x::to_csv([["John", "Mike", "David", "Clara"],["White", "Red", "Green", "Blue"]]));
/* string John;Mike;David;Clara
White;Red;Green;Blue
```
@param array $data The array to format
@param string $delimiter
@param string $enclosure
@param string $separator
@param bool $encloseAll
@param bool $nullToMysqlNull
@return string | [
"Formats",
"an",
"array",
"as",
"a",
"CSV",
"string",
".",
"Adapted",
"from",
"http",
":",
"//",
"us3",
".",
"php",
".",
"net",
"/",
"manual",
"/",
"en",
"/",
"function",
".",
"fputcsv",
".",
"php#87120"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1351-L1375 |
nabab/bbn | src/bbn/x.php | x.is_same | public static function is_same(string $file1, string $file2, $strict = false){
if ( !is_file($file1) || !is_file($file2) ){
throw Exception("Boo! One of the files given to the x::is_same function doesn't exist");
}
else{
$same = filesize($file1) === filesize($file2);
if ( !$strict || !$same ){
return $same;
}
return filemtime($file1) === filemtime($file2);
}
} | php | public static function is_same(string $file1, string $file2, $strict = false){
if ( !is_file($file1) || !is_file($file2) ){
throw Exception("Boo! One of the files given to the x::is_same function doesn't exist");
}
else{
$same = filesize($file1) === filesize($file2);
if ( !$strict || !$same ){
return $same;
}
return filemtime($file1) === filemtime($file2);
}
} | [
"public",
"static",
"function",
"is_same",
"(",
"string",
"$",
"file1",
",",
"string",
"$",
"file2",
",",
"$",
"strict",
"=",
"false",
")",
"{",
"if",
"(",
"!",
"is_file",
"(",
"$",
"file1",
")",
"||",
"!",
"is_file",
"(",
"$",
"file2",
")",
")",
"{",
"throw",
"Exception",
"(",
"\"Boo! One of the files given to the x::is_same function doesn't exist\"",
")",
";",
"}",
"else",
"{",
"$",
"same",
"=",
"filesize",
"(",
"$",
"file1",
")",
"===",
"filesize",
"(",
"$",
"file2",
")",
";",
"if",
"(",
"!",
"$",
"strict",
"||",
"!",
"$",
"same",
")",
"{",
"return",
"$",
"same",
";",
"}",
"return",
"filemtime",
"(",
"$",
"file1",
")",
"===",
"filemtime",
"(",
"$",
"file2",
")",
";",
"}",
"}"
] | Checks if two files are the same.
@param string $file1
@param string $file2
@param bool $strict
@return bool | [
"Checks",
"if",
"two",
"files",
"are",
"the",
"same",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1385-L1396 |
nabab/bbn | src/bbn/x.php | x.to_excel | public static function to_excel(array $data, string $file, bool $with_titles = true): bool
{
$checked = false;
$todo = [];
foreach ( $data as $d ){
if ( !$checked && self::is_assoc($d) ){
if ( $with_titles ){
$line1 = [];
$line2 = [];
foreach ( $d as $k => $v ){
$line1[] = $k;
$line2[] = '';
}
$todo[] = $line1;
$todo[] = $line2;
}
$checked = true;
}
$todo[] = array_values($d);
}
if ( count($todo) ){
$objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$objPHPExcel->getActiveSheet()->fromArray($todo, NULL, 'A1');
$objPHPExcel
->getDefaultStyle()
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
$ow = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($objPHPExcel);
$ow->save($file);
return \is_file($file);
}
return false;
} | php | public static function to_excel(array $data, string $file, bool $with_titles = true): bool
{
$checked = false;
$todo = [];
foreach ( $data as $d ){
if ( !$checked && self::is_assoc($d) ){
if ( $with_titles ){
$line1 = [];
$line2 = [];
foreach ( $d as $k => $v ){
$line1[] = $k;
$line2[] = '';
}
$todo[] = $line1;
$todo[] = $line2;
}
$checked = true;
}
$todo[] = array_values($d);
}
if ( count($todo) ){
$objPHPExcel = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
$objPHPExcel->getActiveSheet()->fromArray($todo, NULL, 'A1');
$objPHPExcel
->getDefaultStyle()
->getNumberFormat()
->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_TEXT);
$ow = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($objPHPExcel);
$ow->save($file);
return \is_file($file);
}
return false;
} | [
"public",
"static",
"function",
"to_excel",
"(",
"array",
"$",
"data",
",",
"string",
"$",
"file",
",",
"bool",
"$",
"with_titles",
"=",
"true",
")",
":",
"bool",
"{",
"$",
"checked",
"=",
"false",
";",
"$",
"todo",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"data",
"as",
"$",
"d",
")",
"{",
"if",
"(",
"!",
"$",
"checked",
"&&",
"self",
"::",
"is_assoc",
"(",
"$",
"d",
")",
")",
"{",
"if",
"(",
"$",
"with_titles",
")",
"{",
"$",
"line1",
"=",
"[",
"]",
";",
"$",
"line2",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"d",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"$",
"line1",
"[",
"]",
"=",
"$",
"k",
";",
"$",
"line2",
"[",
"]",
"=",
"''",
";",
"}",
"$",
"todo",
"[",
"]",
"=",
"$",
"line1",
";",
"$",
"todo",
"[",
"]",
"=",
"$",
"line2",
";",
"}",
"$",
"checked",
"=",
"true",
";",
"}",
"$",
"todo",
"[",
"]",
"=",
"array_values",
"(",
"$",
"d",
")",
";",
"}",
"if",
"(",
"count",
"(",
"$",
"todo",
")",
")",
"{",
"$",
"objPHPExcel",
"=",
"new",
"\\",
"PhpOffice",
"\\",
"PhpSpreadsheet",
"\\",
"Spreadsheet",
"(",
")",
";",
"$",
"objPHPExcel",
"->",
"getActiveSheet",
"(",
")",
"->",
"fromArray",
"(",
"$",
"todo",
",",
"NULL",
",",
"'A1'",
")",
";",
"$",
"objPHPExcel",
"->",
"getDefaultStyle",
"(",
")",
"->",
"getNumberFormat",
"(",
")",
"->",
"setFormatCode",
"(",
"\\",
"PhpOffice",
"\\",
"PhpSpreadsheet",
"\\",
"Style",
"\\",
"NumberFormat",
"::",
"FORMAT_TEXT",
")",
";",
"$",
"ow",
"=",
"new",
"\\",
"PhpOffice",
"\\",
"PhpSpreadsheet",
"\\",
"Writer",
"\\",
"Xlsx",
"(",
"$",
"objPHPExcel",
")",
";",
"$",
"ow",
"->",
"save",
"(",
"$",
"file",
")",
";",
"return",
"\\",
"is_file",
"(",
"$",
"file",
")",
";",
"}",
"return",
"false",
";",
"}"
] | Creates an Excel file from a given array.
@param array $array The array to export
@param string $file The file path
@param bool $with_titles Set it to false if you don't want the columns titles. Default true
@return bool | [
"Creates",
"an",
"Excel",
"file",
"from",
"a",
"given",
"array",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1462-L1494 |
nabab/bbn | src/bbn/x.php | x.make_uid | public static function make_uid($binary = false, $hyphens = false){
$tmp = sprintf($hyphens ? '%04x%04x-%04x-%04x-%04x-%04x%04x%04x' : '%04x%04x%04x%04x%04x%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
return $binary ? hex2bin($tmp) : $tmp;
} | php | public static function make_uid($binary = false, $hyphens = false){
$tmp = sprintf($hyphens ? '%04x%04x-%04x-%04x-%04x-%04x%04x%04x' : '%04x%04x%04x%04x%04x%04x%04x%04x',
// 32 bits for "time_low"
mt_rand(0, 0xffff), mt_rand(0, 0xffff),
// 16 bits for "time_mid"
mt_rand(0, 0xffff),
// 16 bits for "time_hi_and_version",
// four most significant bits holds version number 4
mt_rand(0, 0x0fff) | 0x4000,
// 16 bits, 8 bits for "clk_seq_hi_res",
// 8 bits for "clk_seq_low",
// two most significant bits holds zero and one for variant DCE1.1
mt_rand(0, 0x3fff) | 0x8000,
// 48 bits for "node"
mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
);
return $binary ? hex2bin($tmp) : $tmp;
} | [
"public",
"static",
"function",
"make_uid",
"(",
"$",
"binary",
"=",
"false",
",",
"$",
"hyphens",
"=",
"false",
")",
"{",
"$",
"tmp",
"=",
"sprintf",
"(",
"$",
"hyphens",
"?",
"'%04x%04x-%04x-%04x-%04x-%04x%04x%04x'",
":",
"'%04x%04x%04x%04x%04x%04x%04x%04x'",
",",
"// 32 bits for \"time_low\"",
"mt_rand",
"(",
"0",
",",
"0xffff",
")",
",",
"mt_rand",
"(",
"0",
",",
"0xffff",
")",
",",
"// 16 bits for \"time_mid\"",
"mt_rand",
"(",
"0",
",",
"0xffff",
")",
",",
"// 16 bits for \"time_hi_and_version\",",
"// four most significant bits holds version number 4",
"mt_rand",
"(",
"0",
",",
"0x0fff",
")",
"|",
"0x4000",
",",
"// 16 bits, 8 bits for \"clk_seq_hi_res\",",
"// 8 bits for \"clk_seq_low\",",
"// two most significant bits holds zero and one for variant DCE1.1",
"mt_rand",
"(",
"0",
",",
"0x3fff",
")",
"|",
"0x8000",
",",
"// 48 bits for \"node\"",
"mt_rand",
"(",
"0",
",",
"0xffff",
")",
",",
"mt_rand",
"(",
"0",
",",
"0xffff",
")",
",",
"mt_rand",
"(",
"0",
",",
"0xffff",
")",
")",
";",
"return",
"$",
"binary",
"?",
"hex2bin",
"(",
"$",
"tmp",
")",
":",
"$",
"tmp",
";",
"}"
] | Makes a UID.
@param bool $binary Set it to true if you want a binary UID
@param bool $hypens Set it to true if you want hypens to seperate the UID
@return string|bynary | [
"Makes",
"a",
"UID",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1503-L1525 |
nabab/bbn | src/bbn/x.php | x.convert_uids | public static function convert_uids($st){
if ( \is_array($st) || \is_object($st) ){
foreach ( $st as &$s ){
$s = self::convert_uids($s);
}
}
else if ( \bbn\str::is_uid($st) ){
$st = bin2hex($st);
}
return $st;
} | php | public static function convert_uids($st){
if ( \is_array($st) || \is_object($st) ){
foreach ( $st as &$s ){
$s = self::convert_uids($s);
}
}
else if ( \bbn\str::is_uid($st) ){
$st = bin2hex($st);
}
return $st;
} | [
"public",
"static",
"function",
"convert_uids",
"(",
"$",
"st",
")",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"st",
")",
"||",
"\\",
"is_object",
"(",
"$",
"st",
")",
")",
"{",
"foreach",
"(",
"$",
"st",
"as",
"&",
"$",
"s",
")",
"{",
"$",
"s",
"=",
"self",
"::",
"convert_uids",
"(",
"$",
"s",
")",
";",
"}",
"}",
"else",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"st",
")",
")",
"{",
"$",
"st",
"=",
"bin2hex",
"(",
"$",
"st",
")",
";",
"}",
"return",
"$",
"st",
";",
"}"
] | Converts a hex UID to a binary UID. You can also give an array or an object to convert the array's items or the object's properties.
@param string|array|object $st
@return string | [
"Converts",
"a",
"hex",
"UID",
"to",
"a",
"binary",
"UID",
".",
"You",
"can",
"also",
"give",
"an",
"array",
"or",
"an",
"object",
"to",
"convert",
"the",
"array",
"s",
"items",
"or",
"the",
"object",
"s",
"properties",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1532-L1542 |
nabab/bbn | src/bbn/x.php | x.compare_floats | public static function compare_floats($v1, $v2, string $operator = '===', int $precision = 4): bool
{
$v1 = round((float)$v1 * pow(10, $precision));
$v2 = round((float)$v2 * pow(10, $precision));
switch ($operator ){
case '===':
return $v1 === $v2;
case '==':
return $v1 == $v2;
case '>=':
return $v1 >= $v2;
case '<=':
return $v1 <= $v2;
case '>':
return $v1 > $v2;
case '<':
return $v1 < $v2;
}
return false;
} | php | public static function compare_floats($v1, $v2, string $operator = '===', int $precision = 4): bool
{
$v1 = round((float)$v1 * pow(10, $precision));
$v2 = round((float)$v2 * pow(10, $precision));
switch ($operator ){
case '===':
return $v1 === $v2;
case '==':
return $v1 == $v2;
case '>=':
return $v1 >= $v2;
case '<=':
return $v1 <= $v2;
case '>':
return $v1 > $v2;
case '<':
return $v1 < $v2;
}
return false;
} | [
"public",
"static",
"function",
"compare_floats",
"(",
"$",
"v1",
",",
"$",
"v2",
",",
"string",
"$",
"operator",
"=",
"'==='",
",",
"int",
"$",
"precision",
"=",
"4",
")",
":",
"bool",
"{",
"$",
"v1",
"=",
"round",
"(",
"(",
"float",
")",
"$",
"v1",
"*",
"pow",
"(",
"10",
",",
"$",
"precision",
")",
")",
";",
"$",
"v2",
"=",
"round",
"(",
"(",
"float",
")",
"$",
"v2",
"*",
"pow",
"(",
"10",
",",
"$",
"precision",
")",
")",
";",
"switch",
"(",
"$",
"operator",
")",
"{",
"case",
"'==='",
":",
"return",
"$",
"v1",
"===",
"$",
"v2",
";",
"case",
"'=='",
":",
"return",
"$",
"v1",
"==",
"$",
"v2",
";",
"case",
"'>='",
":",
"return",
"$",
"v1",
">=",
"$",
"v2",
";",
"case",
"'<='",
":",
"return",
"$",
"v1",
"<=",
"$",
"v2",
";",
"case",
"'>'",
":",
"return",
"$",
"v1",
">",
"$",
"v2",
";",
"case",
"'<'",
":",
"return",
"$",
"v1",
"<",
"$",
"v2",
";",
"}",
"return",
"false",
";",
"}"
] | Compares two float numbers with the given operator.
@param float $v1
@param float $v2
@param string $operator
@param int $precision
@return boolean | [
"Compares",
"two",
"float",
"numbers",
"with",
"the",
"given",
"operator",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1553-L1572 |
nabab/bbn | src/bbn/x.php | x.json_base64_encode | public static function json_base64_encode(array $arr, $json = true)
{
$res = [];
foreach ( $arr as $i => $a ){
if ( is_array($a) ){
$res[$i] = self::json_base64_encode($a, false);
}
else if ( is_string($a) ){
$res[$i] = base64_encode($a);
}
else{
$res[$i] = $a;
}
}
return $json ? json_encode($res) : $res;
} | php | public static function json_base64_encode(array $arr, $json = true)
{
$res = [];
foreach ( $arr as $i => $a ){
if ( is_array($a) ){
$res[$i] = self::json_base64_encode($a, false);
}
else if ( is_string($a) ){
$res[$i] = base64_encode($a);
}
else{
$res[$i] = $a;
}
}
return $json ? json_encode($res) : $res;
} | [
"public",
"static",
"function",
"json_base64_encode",
"(",
"array",
"$",
"arr",
",",
"$",
"json",
"=",
"true",
")",
"{",
"$",
"res",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"arr",
"as",
"$",
"i",
"=>",
"$",
"a",
")",
"{",
"if",
"(",
"is_array",
"(",
"$",
"a",
")",
")",
"{",
"$",
"res",
"[",
"$",
"i",
"]",
"=",
"self",
"::",
"json_base64_encode",
"(",
"$",
"a",
",",
"false",
")",
";",
"}",
"else",
"if",
"(",
"is_string",
"(",
"$",
"a",
")",
")",
"{",
"$",
"res",
"[",
"$",
"i",
"]",
"=",
"base64_encode",
"(",
"$",
"a",
")",
";",
"}",
"else",
"{",
"$",
"res",
"[",
"$",
"i",
"]",
"=",
"$",
"a",
";",
"}",
"}",
"return",
"$",
"json",
"?",
"json_encode",
"(",
"$",
"res",
")",
":",
"$",
"res",
";",
"}"
] | Encodes an array's values to the base64 encoding scheme. You can also convert the resulting array into a JSON string (default).
@param array $arr
@param boolean $json
@return string|array | [
"Encodes",
"an",
"array",
"s",
"values",
"to",
"the",
"base64",
"encoding",
"scheme",
".",
"You",
"can",
"also",
"convert",
"the",
"resulting",
"array",
"into",
"a",
"JSON",
"string",
"(",
"default",
")",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1581-L1596 |
nabab/bbn | src/bbn/x.php | x.json_base64_decode | public static function json_base64_decode($st): ?array
{
$res = \is_string($st) ? json_decode($st, true) : $st;
if ( \is_array($res) ){
foreach ( $res as $i => $a ){
if ( \is_array($a) ){
$res[$i] = self::json_base64_decode($a);
}
else if ( \is_string($a) ){
$res[$i] = base64_decode($a);
}
else{
$res[$i] = $a;
}
}
return $res;
}
return null;
} | php | public static function json_base64_decode($st): ?array
{
$res = \is_string($st) ? json_decode($st, true) : $st;
if ( \is_array($res) ){
foreach ( $res as $i => $a ){
if ( \is_array($a) ){
$res[$i] = self::json_base64_decode($a);
}
else if ( \is_string($a) ){
$res[$i] = base64_decode($a);
}
else{
$res[$i] = $a;
}
}
return $res;
}
return null;
} | [
"public",
"static",
"function",
"json_base64_decode",
"(",
"$",
"st",
")",
":",
"?",
"array",
"{",
"$",
"res",
"=",
"\\",
"is_string",
"(",
"$",
"st",
")",
"?",
"json_decode",
"(",
"$",
"st",
",",
"true",
")",
":",
"$",
"st",
";",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"res",
")",
")",
"{",
"foreach",
"(",
"$",
"res",
"as",
"$",
"i",
"=>",
"$",
"a",
")",
"{",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"a",
")",
")",
"{",
"$",
"res",
"[",
"$",
"i",
"]",
"=",
"self",
"::",
"json_base64_decode",
"(",
"$",
"a",
")",
";",
"}",
"else",
"if",
"(",
"\\",
"is_string",
"(",
"$",
"a",
")",
")",
"{",
"$",
"res",
"[",
"$",
"i",
"]",
"=",
"base64_decode",
"(",
"$",
"a",
")",
";",
"}",
"else",
"{",
"$",
"res",
"[",
"$",
"i",
"]",
"=",
"$",
"a",
";",
"}",
"}",
"return",
"$",
"res",
";",
"}",
"return",
"null",
";",
"}"
] | Decodes the base64 array's values. You can also give a JSON string of an array.
@param string|array $st
@result array | [
"Decodes",
"the",
"base64",
"array",
"s",
"values",
".",
"You",
"can",
"also",
"give",
"a",
"JSON",
"string",
"of",
"an",
"array",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1604-L1622 |
nabab/bbn | src/bbn/x.php | x.index_by_first_val | public static function index_by_first_val(array $ar): array
{
if ( empty($ar) || !isset($ar[0]) || !\count($ar[0]) ){
return $ar;
}
$cols = array_keys($ar[0]);
$idx = array_shift($cols);
$num_cols = \count($cols);
$res = [];
foreach ( $ar as $d ){
$index = $d[$idx];
unset($d[$idx]);
$res[$index] = $num_cols > 1 ? $d : $d[$cols[0]];
}
return $res;
} | php | public static function index_by_first_val(array $ar): array
{
if ( empty($ar) || !isset($ar[0]) || !\count($ar[0]) ){
return $ar;
}
$cols = array_keys($ar[0]);
$idx = array_shift($cols);
$num_cols = \count($cols);
$res = [];
foreach ( $ar as $d ){
$index = $d[$idx];
unset($d[$idx]);
$res[$index] = $num_cols > 1 ? $d : $d[$cols[0]];
}
return $res;
} | [
"public",
"static",
"function",
"index_by_first_val",
"(",
"array",
"$",
"ar",
")",
":",
"array",
"{",
"if",
"(",
"empty",
"(",
"$",
"ar",
")",
"||",
"!",
"isset",
"(",
"$",
"ar",
"[",
"0",
"]",
")",
"||",
"!",
"\\",
"count",
"(",
"$",
"ar",
"[",
"0",
"]",
")",
")",
"{",
"return",
"$",
"ar",
";",
"}",
"$",
"cols",
"=",
"array_keys",
"(",
"$",
"ar",
"[",
"0",
"]",
")",
";",
"$",
"idx",
"=",
"array_shift",
"(",
"$",
"cols",
")",
";",
"$",
"num_cols",
"=",
"\\",
"count",
"(",
"$",
"cols",
")",
";",
"$",
"res",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"ar",
"as",
"$",
"d",
")",
"{",
"$",
"index",
"=",
"$",
"d",
"[",
"$",
"idx",
"]",
";",
"unset",
"(",
"$",
"d",
"[",
"$",
"idx",
"]",
")",
";",
"$",
"res",
"[",
"$",
"index",
"]",
"=",
"$",
"num_cols",
">",
"1",
"?",
"$",
"d",
":",
"$",
"d",
"[",
"$",
"cols",
"[",
"0",
"]",
"]",
";",
"}",
"return",
"$",
"res",
";",
"}"
] | Creates an associative array based on the first array's value.
@param array $ar
@return array | [
"Creates",
"an",
"associative",
"array",
"based",
"on",
"the",
"first",
"array",
"s",
"value",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/x.php#L1629-L1644 |
thienhungho/yii2-product-management | src/modules/ProductManage/controllers/ProductUnitController.php | ProductUnitController.actionView | public function actionView($id)
{
$model = $this->findModel($id);
$providerProductUnitEquivalent = new \yii\data\ArrayDataProvider([
'allModels' => $model->productUnitEquivalents,
]);
return $this->render('view', [
'model' => $this->findModel($id),
'providerProductUnitEquivalent' => $providerProductUnitEquivalent,
]);
} | php | public function actionView($id)
{
$model = $this->findModel($id);
$providerProductUnitEquivalent = new \yii\data\ArrayDataProvider([
'allModels' => $model->productUnitEquivalents,
]);
return $this->render('view', [
'model' => $this->findModel($id),
'providerProductUnitEquivalent' => $providerProductUnitEquivalent,
]);
} | [
"public",
"function",
"actionView",
"(",
"$",
"id",
")",
"{",
"$",
"model",
"=",
"$",
"this",
"->",
"findModel",
"(",
"$",
"id",
")",
";",
"$",
"providerProductUnitEquivalent",
"=",
"new",
"\\",
"yii",
"\\",
"data",
"\\",
"ArrayDataProvider",
"(",
"[",
"'allModels'",
"=>",
"$",
"model",
"->",
"productUnitEquivalents",
",",
"]",
")",
";",
"return",
"$",
"this",
"->",
"render",
"(",
"'view'",
",",
"[",
"'model'",
"=>",
"$",
"this",
"->",
"findModel",
"(",
"$",
"id",
")",
",",
"'providerProductUnitEquivalent'",
"=>",
"$",
"providerProductUnitEquivalent",
",",
"]",
")",
";",
"}"
] | @param $id
@return string
@throws NotFoundHttpException | [
"@param",
"$id"
] | train | https://github.com/thienhungho/yii2-product-management/blob/72e1237bba123faf671e44491bd93077b50adde9/src/modules/ProductManage/controllers/ProductUnitController.php#L50-L60 |
thienhungho/yii2-product-management | src/modules/ProductManage/controllers/ProductUnitController.php | ProductUnitController.actionPdf | public function actionPdf($id) {
$model = $this->findModel($id);
$providerProductUnitEquivalent = new \yii\data\ArrayDataProvider([
'allModels' => $model->productUnitEquivalents,
]);
$content = $this->renderAjax('_pdf', [
'model' => $model,
'providerProductUnitEquivalent' => $providerProductUnitEquivalent,
]);
$pdf = new \kartik\mpdf\Pdf([
'mode' => \kartik\mpdf\Pdf::MODE_CORE,
'format' => \kartik\mpdf\Pdf::FORMAT_A4,
'orientation' => \kartik\mpdf\Pdf::ORIENT_PORTRAIT,
'destination' => \kartik\mpdf\Pdf::DEST_BROWSER,
'content' => $content,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.kv-heading-1{font-size:18px}',
'options' => ['title' => \Yii::$app->name],
'methods' => [
'SetHeader' => [\Yii::$app->name],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
} | php | public function actionPdf($id) {
$model = $this->findModel($id);
$providerProductUnitEquivalent = new \yii\data\ArrayDataProvider([
'allModels' => $model->productUnitEquivalents,
]);
$content = $this->renderAjax('_pdf', [
'model' => $model,
'providerProductUnitEquivalent' => $providerProductUnitEquivalent,
]);
$pdf = new \kartik\mpdf\Pdf([
'mode' => \kartik\mpdf\Pdf::MODE_CORE,
'format' => \kartik\mpdf\Pdf::FORMAT_A4,
'orientation' => \kartik\mpdf\Pdf::ORIENT_PORTRAIT,
'destination' => \kartik\mpdf\Pdf::DEST_BROWSER,
'content' => $content,
'cssFile' => '@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css',
'cssInline' => '.kv-heading-1{font-size:18px}',
'options' => ['title' => \Yii::$app->name],
'methods' => [
'SetHeader' => [\Yii::$app->name],
'SetFooter' => ['{PAGENO}'],
]
]);
return $pdf->render();
} | [
"public",
"function",
"actionPdf",
"(",
"$",
"id",
")",
"{",
"$",
"model",
"=",
"$",
"this",
"->",
"findModel",
"(",
"$",
"id",
")",
";",
"$",
"providerProductUnitEquivalent",
"=",
"new",
"\\",
"yii",
"\\",
"data",
"\\",
"ArrayDataProvider",
"(",
"[",
"'allModels'",
"=>",
"$",
"model",
"->",
"productUnitEquivalents",
",",
"]",
")",
";",
"$",
"content",
"=",
"$",
"this",
"->",
"renderAjax",
"(",
"'_pdf'",
",",
"[",
"'model'",
"=>",
"$",
"model",
",",
"'providerProductUnitEquivalent'",
"=>",
"$",
"providerProductUnitEquivalent",
",",
"]",
")",
";",
"$",
"pdf",
"=",
"new",
"\\",
"kartik",
"\\",
"mpdf",
"\\",
"Pdf",
"(",
"[",
"'mode'",
"=>",
"\\",
"kartik",
"\\",
"mpdf",
"\\",
"Pdf",
"::",
"MODE_CORE",
",",
"'format'",
"=>",
"\\",
"kartik",
"\\",
"mpdf",
"\\",
"Pdf",
"::",
"FORMAT_A4",
",",
"'orientation'",
"=>",
"\\",
"kartik",
"\\",
"mpdf",
"\\",
"Pdf",
"::",
"ORIENT_PORTRAIT",
",",
"'destination'",
"=>",
"\\",
"kartik",
"\\",
"mpdf",
"\\",
"Pdf",
"::",
"DEST_BROWSER",
",",
"'content'",
"=>",
"$",
"content",
",",
"'cssFile'",
"=>",
"'@vendor/kartik-v/yii2-mpdf/assets/kv-mpdf-bootstrap.min.css'",
",",
"'cssInline'",
"=>",
"'.kv-heading-1{font-size:18px}'",
",",
"'options'",
"=>",
"[",
"'title'",
"=>",
"\\",
"Yii",
"::",
"$",
"app",
"->",
"name",
"]",
",",
"'methods'",
"=>",
"[",
"'SetHeader'",
"=>",
"[",
"\\",
"Yii",
"::",
"$",
"app",
"->",
"name",
"]",
",",
"'SetFooter'",
"=>",
"[",
"'{PAGENO}'",
"]",
",",
"]",
"]",
")",
";",
"return",
"$",
"pdf",
"->",
"render",
"(",
")",
";",
"}"
] | @param $id
@return mixed
@throws NotFoundHttpException | [
"@param",
"$id"
] | train | https://github.com/thienhungho/yii2-product-management/blob/72e1237bba123faf671e44491bd93077b50adde9/src/modules/ProductManage/controllers/ProductUnitController.php#L123-L150 |
HedronDev/hedron | src/Parser/ComposerJson.php | ComposerJson.parse | public function parse(GitPostReceiveHandler $handler, CommandStackInterface $commandStack) {
if (array_search('composer.json', $handler->getIntersectFiles()) !== FALSE) {
$environment = $this->getEnvironment();
$clientDir = $this->getClientDirectoryName();
$commandStack->addCommand("cd {$environment->getDockerDirectory()}/$clientDir/{$environment->getDataDirectory()}");
if (file_exists("{$environment->getDockerDirectory()}/$clientDir/{$environment->getDataDirectory()}/composer.lock")) {
$commandStack->addCommand("composer update");
}
else {
$commandStack->addCommand("composer install");
}
$commandStack->execute();
}
} | php | public function parse(GitPostReceiveHandler $handler, CommandStackInterface $commandStack) {
if (array_search('composer.json', $handler->getIntersectFiles()) !== FALSE) {
$environment = $this->getEnvironment();
$clientDir = $this->getClientDirectoryName();
$commandStack->addCommand("cd {$environment->getDockerDirectory()}/$clientDir/{$environment->getDataDirectory()}");
if (file_exists("{$environment->getDockerDirectory()}/$clientDir/{$environment->getDataDirectory()}/composer.lock")) {
$commandStack->addCommand("composer update");
}
else {
$commandStack->addCommand("composer install");
}
$commandStack->execute();
}
} | [
"public",
"function",
"parse",
"(",
"GitPostReceiveHandler",
"$",
"handler",
",",
"CommandStackInterface",
"$",
"commandStack",
")",
"{",
"if",
"(",
"array_search",
"(",
"'composer.json'",
",",
"$",
"handler",
"->",
"getIntersectFiles",
"(",
")",
")",
"!==",
"FALSE",
")",
"{",
"$",
"environment",
"=",
"$",
"this",
"->",
"getEnvironment",
"(",
")",
";",
"$",
"clientDir",
"=",
"$",
"this",
"->",
"getClientDirectoryName",
"(",
")",
";",
"$",
"commandStack",
"->",
"addCommand",
"(",
"\"cd {$environment->getDockerDirectory()}/$clientDir/{$environment->getDataDirectory()}\"",
")",
";",
"if",
"(",
"file_exists",
"(",
"\"{$environment->getDockerDirectory()}/$clientDir/{$environment->getDataDirectory()}/composer.lock\"",
")",
")",
"{",
"$",
"commandStack",
"->",
"addCommand",
"(",
"\"composer update\"",
")",
";",
"}",
"else",
"{",
"$",
"commandStack",
"->",
"addCommand",
"(",
"\"composer install\"",
")",
";",
"}",
"$",
"commandStack",
"->",
"execute",
"(",
")",
";",
"}",
"}"
] | {@inheritdoc} | [
"{"
] | train | https://github.com/HedronDev/hedron/blob/3b4adec4912f2d7c0b7e7262dc36515fbc2e8e00/src/Parser/ComposerJson.php#L18-L31 |
fkooman/php-lib-http | src/fkooman/Http/Url.php | Url.getQueryString | public function getQueryString()
{
$requestUri = $this->getRequestUri();
if (false !== $qPos = mb_strpos($requestUri, '?')) {
// has query string
return mb_substr($requestUri, $qPos + 1);
}
return '';
} | php | public function getQueryString()
{
$requestUri = $this->getRequestUri();
if (false !== $qPos = mb_strpos($requestUri, '?')) {
// has query string
return mb_substr($requestUri, $qPos + 1);
}
return '';
} | [
"public",
"function",
"getQueryString",
"(",
")",
"{",
"$",
"requestUri",
"=",
"$",
"this",
"->",
"getRequestUri",
"(",
")",
";",
"if",
"(",
"false",
"!==",
"$",
"qPos",
"=",
"mb_strpos",
"(",
"$",
"requestUri",
",",
"'?'",
")",
")",
"{",
"// has query string",
"return",
"mb_substr",
"(",
"$",
"requestUri",
",",
"$",
"qPos",
"+",
"1",
")",
";",
"}",
"return",
"''",
";",
"}"
] | The query string.
@return string the query string, or empty string if no query string
is available | [
"The",
"query",
"string",
"."
] | train | https://github.com/fkooman/php-lib-http/blob/94956a480459b7f6b880d61f21ef03e683deb995/src/fkooman/Http/Url.php#L151-L161 |
fkooman/php-lib-http | src/fkooman/Http/Url.php | Url.getQueryStringAsArray | public function getQueryStringAsArray()
{
$queryString = $this->getQueryString();
if ('' === $queryString) {
return [];
}
$queryStringArray = [];
parse_str($queryString, $queryStringArray);
return $queryStringArray;
} | php | public function getQueryStringAsArray()
{
$queryString = $this->getQueryString();
if ('' === $queryString) {
return [];
}
$queryStringArray = [];
parse_str($queryString, $queryStringArray);
return $queryStringArray;
} | [
"public",
"function",
"getQueryStringAsArray",
"(",
")",
"{",
"$",
"queryString",
"=",
"$",
"this",
"->",
"getQueryString",
"(",
")",
";",
"if",
"(",
"''",
"===",
"$",
"queryString",
")",
"{",
"return",
"[",
"]",
";",
"}",
"$",
"queryStringArray",
"=",
"[",
"]",
";",
"parse_str",
"(",
"$",
"queryString",
",",
"$",
"queryStringArray",
")",
";",
"return",
"$",
"queryStringArray",
";",
"}"
] | The query string as array.
@return array the query string as array. The array will be empty if
the query string is empty | [
"The",
"query",
"string",
"as",
"array",
"."
] | train | https://github.com/fkooman/php-lib-http/blob/94956a480459b7f6b880d61f21ef03e683deb995/src/fkooman/Http/Url.php#L169-L181 |
fkooman/php-lib-http | src/fkooman/Http/Url.php | Url.getQueryParameter | public function getQueryParameter($key)
{
$queryStringArray = $this->getQueryStringAsArray();
if (array_key_exists($key, $queryStringArray)) {
return $queryStringArray[$key];
}
return null;
} | php | public function getQueryParameter($key)
{
$queryStringArray = $this->getQueryStringAsArray();
if (array_key_exists($key, $queryStringArray)) {
return $queryStringArray[$key];
}
return null;
} | [
"public",
"function",
"getQueryParameter",
"(",
"$",
"key",
")",
"{",
"$",
"queryStringArray",
"=",
"$",
"this",
"->",
"getQueryStringAsArray",
"(",
")",
";",
"if",
"(",
"array_key_exists",
"(",
"$",
"key",
",",
"$",
"queryStringArray",
")",
")",
"{",
"return",
"$",
"queryStringArray",
"[",
"$",
"key",
"]",
";",
"}",
"return",
"null",
";",
"}"
] | Return a specific query string parameter value.
@param string $key the query parameter key to get
@return mixed the query parameter value if it is set, or null if the
parameter is not available | [
"Return",
"a",
"specific",
"query",
"string",
"parameter",
"value",
"."
] | train | https://github.com/fkooman/php-lib-http/blob/94956a480459b7f6b880d61f21ef03e683deb995/src/fkooman/Http/Url.php#L191-L200 |
fkooman/php-lib-http | src/fkooman/Http/Url.php | Url.getRoot | public function getRoot()
{
$requestUri = $this->getRequestUri();
$pathInfo = $this->getPathInfo();
// remove query string from REQUEST_URI, if set
if (false !== $qPos = mb_strpos($requestUri, '?')) {
// has query string
$requestUri = mb_substr($requestUri, 0, $qPos + 1);
}
// remove PATH_INFO from REQUEST_URI, if set
if (false !== $pPos = mb_strrpos($requestUri, $pathInfo)) {
// has PATH_INFO
$requestUri = mb_substr($requestUri, 0, $pPos + 1);
}
return $requestUri;
} | php | public function getRoot()
{
$requestUri = $this->getRequestUri();
$pathInfo = $this->getPathInfo();
// remove query string from REQUEST_URI, if set
if (false !== $qPos = mb_strpos($requestUri, '?')) {
// has query string
$requestUri = mb_substr($requestUri, 0, $qPos + 1);
}
// remove PATH_INFO from REQUEST_URI, if set
if (false !== $pPos = mb_strrpos($requestUri, $pathInfo)) {
// has PATH_INFO
$requestUri = mb_substr($requestUri, 0, $pPos + 1);
}
return $requestUri;
} | [
"public",
"function",
"getRoot",
"(",
")",
"{",
"$",
"requestUri",
"=",
"$",
"this",
"->",
"getRequestUri",
"(",
")",
";",
"$",
"pathInfo",
"=",
"$",
"this",
"->",
"getPathInfo",
"(",
")",
";",
"// remove query string from REQUEST_URI, if set",
"if",
"(",
"false",
"!==",
"$",
"qPos",
"=",
"mb_strpos",
"(",
"$",
"requestUri",
",",
"'?'",
")",
")",
"{",
"// has query string",
"$",
"requestUri",
"=",
"mb_substr",
"(",
"$",
"requestUri",
",",
"0",
",",
"$",
"qPos",
"+",
"1",
")",
";",
"}",
"// remove PATH_INFO from REQUEST_URI, if set",
"if",
"(",
"false",
"!==",
"$",
"pPos",
"=",
"mb_strrpos",
"(",
"$",
"requestUri",
",",
"$",
"pathInfo",
")",
")",
"{",
"// has PATH_INFO",
"$",
"requestUri",
"=",
"mb_substr",
"(",
"$",
"requestUri",
",",
"0",
",",
"$",
"pPos",
"+",
"1",
")",
";",
"}",
"return",
"$",
"requestUri",
";",
"}"
] | Get the REQUEST_URI without PATH_INFO and QUERY_STRING, taking server
rewriting in consideration.
Example (without URL rewriting):
https://www.example.org/foo/index.php/bar?a=b will return:
'/foo/index.php'
Example (with URL rewriting to index.php):
https://www.example.org/foo/bar?a=b will return:
'/foo'
Example (with URL rewriting to index.php without sub folder):
https://www.example.org/bar?a=b will return:
'' | [
"Get",
"the",
"REQUEST_URI",
"without",
"PATH_INFO",
"and",
"QUERY_STRING",
"taking",
"server",
"rewriting",
"in",
"consideration",
"."
] | train | https://github.com/fkooman/php-lib-http/blob/94956a480459b7f6b880d61f21ef03e683deb995/src/fkooman/Http/Url.php#L218-L236 |
fkooman/php-lib-http | src/fkooman/Http/Url.php | Url.getAuthority | public function getAuthority()
{
$scheme = $this->getScheme();
$serverName = $this->getHost();
$serverPort = $this->getPort();
$authority = sprintf('%s://%s', $scheme, $serverName);
if (('https' === $scheme && 443 !== $serverPort) || ('http' === $scheme && 80 !== $serverPort)) {
$authority .= sprintf(':%d', $serverPort);
}
return $authority;
} | php | public function getAuthority()
{
$scheme = $this->getScheme();
$serverName = $this->getHost();
$serverPort = $this->getPort();
$authority = sprintf('%s://%s', $scheme, $serverName);
if (('https' === $scheme && 443 !== $serverPort) || ('http' === $scheme && 80 !== $serverPort)) {
$authority .= sprintf(':%d', $serverPort);
}
return $authority;
} | [
"public",
"function",
"getAuthority",
"(",
")",
"{",
"$",
"scheme",
"=",
"$",
"this",
"->",
"getScheme",
"(",
")",
";",
"$",
"serverName",
"=",
"$",
"this",
"->",
"getHost",
"(",
")",
";",
"$",
"serverPort",
"=",
"$",
"this",
"->",
"getPort",
"(",
")",
";",
"$",
"authority",
"=",
"sprintf",
"(",
"'%s://%s'",
",",
"$",
"scheme",
",",
"$",
"serverName",
")",
";",
"if",
"(",
"(",
"'https'",
"===",
"$",
"scheme",
"&&",
"443",
"!==",
"$",
"serverPort",
")",
"||",
"(",
"'http'",
"===",
"$",
"scheme",
"&&",
"80",
"!==",
"$",
"serverPort",
")",
")",
"{",
"$",
"authority",
".=",
"sprintf",
"(",
"':%d'",
",",
"$",
"serverPort",
")",
";",
"}",
"return",
"$",
"authority",
";",
"}"
] | Get the authority part of the URL. That is, the scheme, host and
optional port if it is not a standard port.
@return string the authority part of the URL | [
"Get",
"the",
"authority",
"part",
"of",
"the",
"URL",
".",
"That",
"is",
"the",
"scheme",
"host",
"and",
"optional",
"port",
"if",
"it",
"is",
"not",
"a",
"standard",
"port",
"."
] | train | https://github.com/fkooman/php-lib-http/blob/94956a480459b7f6b880d61f21ef03e683deb995/src/fkooman/Http/Url.php#L252-L264 |
alevilar/ristorantino-vendor | Compras/Model/PedidoEstado.php | PedidoEstado.beforeDelete | public function beforeDelete($cascade = true) {
$idInborrables = array(
COMPRAS_PEDIDO_ESTADO_PENDIENTE,
COMPRAS_PEDIDO_ESTADO_COMPLETADO,
COMPRAS_PEDIDO_ESTADO_PEDIDO,
);
if ( in_array($this->id, $idInborrables)) {
return false;
}
return true;
} | php | public function beforeDelete($cascade = true) {
$idInborrables = array(
COMPRAS_PEDIDO_ESTADO_PENDIENTE,
COMPRAS_PEDIDO_ESTADO_COMPLETADO,
COMPRAS_PEDIDO_ESTADO_PEDIDO,
);
if ( in_array($this->id, $idInborrables)) {
return false;
}
return true;
} | [
"public",
"function",
"beforeDelete",
"(",
"$",
"cascade",
"=",
"true",
")",
"{",
"$",
"idInborrables",
"=",
"array",
"(",
"COMPRAS_PEDIDO_ESTADO_PENDIENTE",
",",
"COMPRAS_PEDIDO_ESTADO_COMPLETADO",
",",
"COMPRAS_PEDIDO_ESTADO_PEDIDO",
",",
")",
";",
"if",
"(",
"in_array",
"(",
"$",
"this",
"->",
"id",
",",
"$",
"idInborrables",
")",
")",
"{",
"return",
"false",
";",
"}",
"return",
"true",
";",
"}"
] | Called before every deletion operation.
@param bool $cascade If true records that depend on this record will also be deleted
@return bool True if the operation should continue, false if it should abort
@link http://book.cakephp.org/2.0/en/models/callback-methods.html#beforedelete | [
"Called",
"before",
"every",
"deletion",
"operation",
"."
] | train | https://github.com/alevilar/ristorantino-vendor/blob/6b91a1e20cc0ba09a1968d77e3de6512cfa2d966/Compras/Model/PedidoEstado.php#L64-L75 |
gdbots/iam-php | src/Policy.php | Policy.isGranted | public function isGranted(string $action): bool
{
if (empty($this->allowed)) {
return false;
}
if (isset($this->denied[$action]) || isset($this->denied[self::WILDCARD])) {
return false;
}
$rules = $this->getRules($action);
foreach ($rules as $rule) {
if (isset($this->denied[$rule])) {
return false;
}
}
foreach ($rules as $rule) {
if (isset($this->allowed[$rule])) {
return true;
}
}
return false;
} | php | public function isGranted(string $action): bool
{
if (empty($this->allowed)) {
return false;
}
if (isset($this->denied[$action]) || isset($this->denied[self::WILDCARD])) {
return false;
}
$rules = $this->getRules($action);
foreach ($rules as $rule) {
if (isset($this->denied[$rule])) {
return false;
}
}
foreach ($rules as $rule) {
if (isset($this->allowed[$rule])) {
return true;
}
}
return false;
} | [
"public",
"function",
"isGranted",
"(",
"string",
"$",
"action",
")",
":",
"bool",
"{",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"allowed",
")",
")",
"{",
"return",
"false",
";",
"}",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"denied",
"[",
"$",
"action",
"]",
")",
"||",
"isset",
"(",
"$",
"this",
"->",
"denied",
"[",
"self",
"::",
"WILDCARD",
"]",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"rules",
"=",
"$",
"this",
"->",
"getRules",
"(",
"$",
"action",
")",
";",
"foreach",
"(",
"$",
"rules",
"as",
"$",
"rule",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"denied",
"[",
"$",
"rule",
"]",
")",
")",
"{",
"return",
"false",
";",
"}",
"}",
"foreach",
"(",
"$",
"rules",
"as",
"$",
"rule",
")",
"{",
"if",
"(",
"isset",
"(",
"$",
"this",
"->",
"allowed",
"[",
"$",
"rule",
"]",
")",
")",
"{",
"return",
"true",
";",
"}",
"}",
"return",
"false",
";",
"}"
] | Returns true if the policy should allow the action
to be carried out.
@param string $action
@return bool | [
"Returns",
"true",
"if",
"the",
"policy",
"should",
"allow",
"the",
"action",
"to",
"be",
"carried",
"out",
"."
] | train | https://github.com/gdbots/iam-php/blob/3dbfa602de183a7ac5fb5140304e8cf4c4b15ab3/src/Policy.php#L61-L86 |
gdbots/iam-php | src/Policy.php | Policy.getRules | private function getRules(string $action): array
{
$rules = [];
$parts = explode(self::DELIMITER, $action);
while (array_pop($parts)) {
$rules[] = implode(self::DELIMITER, $parts) . self::DELIMITER . self::WILDCARD;
}
$rules = array_reverse($rules);
$rules[0] = trim($rules[0], self::DELIMITER);
$rules[] = $action;
return $rules;
} | php | private function getRules(string $action): array
{
$rules = [];
$parts = explode(self::DELIMITER, $action);
while (array_pop($parts)) {
$rules[] = implode(self::DELIMITER, $parts) . self::DELIMITER . self::WILDCARD;
}
$rules = array_reverse($rules);
$rules[0] = trim($rules[0], self::DELIMITER);
$rules[] = $action;
return $rules;
} | [
"private",
"function",
"getRules",
"(",
"string",
"$",
"action",
")",
":",
"array",
"{",
"$",
"rules",
"=",
"[",
"]",
";",
"$",
"parts",
"=",
"explode",
"(",
"self",
"::",
"DELIMITER",
",",
"$",
"action",
")",
";",
"while",
"(",
"array_pop",
"(",
"$",
"parts",
")",
")",
"{",
"$",
"rules",
"[",
"]",
"=",
"implode",
"(",
"self",
"::",
"DELIMITER",
",",
"$",
"parts",
")",
".",
"self",
"::",
"DELIMITER",
".",
"self",
"::",
"WILDCARD",
";",
"}",
"$",
"rules",
"=",
"array_reverse",
"(",
"$",
"rules",
")",
";",
"$",
"rules",
"[",
"0",
"]",
"=",
"trim",
"(",
"$",
"rules",
"[",
"0",
"]",
",",
"self",
"::",
"DELIMITER",
")",
";",
"$",
"rules",
"[",
"]",
"=",
"$",
"action",
";",
"return",
"$",
"rules",
";",
"}"
] | Converts an action with potentially colon delimiters
into a set of permissions to check for.
@example
An action of "acme:blog:command:publish-article" becomes
an array of:
[
'*',
'acme:*',
'acme:blog:*',
'acme:blog:command:*',
'acme:blog:command:publish-article',
]
@param string $action
@return string[] | [
"Converts",
"an",
"action",
"with",
"potentially",
"colon",
"delimiters",
"into",
"a",
"set",
"of",
"permissions",
"to",
"check",
"for",
"."
] | train | https://github.com/gdbots/iam-php/blob/3dbfa602de183a7ac5fb5140304e8cf4c4b15ab3/src/Policy.php#L127-L141 |
rhosocial/yii2-user | models/log/UserLoginTrait.php | UserLoginTrait.getLoginLogs | public function getLoginLogs()
{
/* @var $this User */
$class = $this->loginLogClass;
try {
return $class::getLatests($this, Login::GET_ALL_LATESTS);
} catch (\Exception $ex) {
Yii::error($ex->getMessage(), __METHOD__);
return [];
}
} | php | public function getLoginLogs()
{
/* @var $this User */
$class = $this->loginLogClass;
try {
return $class::getLatests($this, Login::GET_ALL_LATESTS);
} catch (\Exception $ex) {
Yii::error($ex->getMessage(), __METHOD__);
return [];
}
} | [
"public",
"function",
"getLoginLogs",
"(",
")",
"{",
"/* @var $this User */",
"$",
"class",
"=",
"$",
"this",
"->",
"loginLogClass",
";",
"try",
"{",
"return",
"$",
"class",
"::",
"getLatests",
"(",
"$",
"this",
",",
"Login",
"::",
"GET_ALL_LATESTS",
")",
";",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"ex",
")",
"{",
"Yii",
"::",
"error",
"(",
"$",
"ex",
"->",
"getMessage",
"(",
")",
",",
"__METHOD__",
")",
";",
"return",
"[",
"]",
";",
"}",
"}"
] | Get login logs.
@return Login[] | [
"Get",
"login",
"logs",
"."
] | train | https://github.com/rhosocial/yii2-user/blob/96737a9d8ca7e9c42cd2b7736d6c0a90ede6e5bc/models/log/UserLoginTrait.php#L35-L45 |
rhosocial/yii2-user | models/log/UserLoginTrait.php | UserLoginTrait.recordLogin | public function recordLogin($config = [])
{
if (empty($this->loginLogClass)) {
return false;
}
$log = $this->create($this->loginLogClass, $config);
try {
return $log->save();
} catch (\Exception $ex) {
Yii::error($ex->getMessage(), __METHOD__);
}
} | php | public function recordLogin($config = [])
{
if (empty($this->loginLogClass)) {
return false;
}
$log = $this->create($this->loginLogClass, $config);
try {
return $log->save();
} catch (\Exception $ex) {
Yii::error($ex->getMessage(), __METHOD__);
}
} | [
"public",
"function",
"recordLogin",
"(",
"$",
"config",
"=",
"[",
"]",
")",
"{",
"if",
"(",
"empty",
"(",
"$",
"this",
"->",
"loginLogClass",
")",
")",
"{",
"return",
"false",
";",
"}",
"$",
"log",
"=",
"$",
"this",
"->",
"create",
"(",
"$",
"this",
"->",
"loginLogClass",
",",
"$",
"config",
")",
";",
"try",
"{",
"return",
"$",
"log",
"->",
"save",
"(",
")",
";",
"}",
"catch",
"(",
"\\",
"Exception",
"$",
"ex",
")",
"{",
"Yii",
"::",
"error",
"(",
"$",
"ex",
"->",
"getMessage",
"(",
")",
",",
"__METHOD__",
")",
";",
"}",
"}"
] | Record login.
@param array $config
@return mixed | [
"Record",
"login",
"."
] | train | https://github.com/rhosocial/yii2-user/blob/96737a9d8ca7e9c42cd2b7736d6c0a90ede6e5bc/models/log/UserLoginTrait.php#L68-L79 |
jnaxo/country-codes | src/Store/api/Collection.php | Collection.setData | public function setData($collection, $resource_name = '')
{
$this->collection = collect();
if (!$this->resource_name) {
$this->resource_name = empty($resource_name) ? 'undefined' : $resource_name;
}
$this->data = [];
foreach ($collection as $item) {
$this->collection->push(clone $item);
$i = new Item($item, $this->resource_name);
array_push($this->data, $i->getData());
}
$this->response['data'] = $this->data;
} | php | public function setData($collection, $resource_name = '')
{
$this->collection = collect();
if (!$this->resource_name) {
$this->resource_name = empty($resource_name) ? 'undefined' : $resource_name;
}
$this->data = [];
foreach ($collection as $item) {
$this->collection->push(clone $item);
$i = new Item($item, $this->resource_name);
array_push($this->data, $i->getData());
}
$this->response['data'] = $this->data;
} | [
"public",
"function",
"setData",
"(",
"$",
"collection",
",",
"$",
"resource_name",
"=",
"''",
")",
"{",
"$",
"this",
"->",
"collection",
"=",
"collect",
"(",
")",
";",
"if",
"(",
"!",
"$",
"this",
"->",
"resource_name",
")",
"{",
"$",
"this",
"->",
"resource_name",
"=",
"empty",
"(",
"$",
"resource_name",
")",
"?",
"'undefined'",
":",
"$",
"resource_name",
";",
"}",
"$",
"this",
"->",
"data",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"collection",
"as",
"$",
"item",
")",
"{",
"$",
"this",
"->",
"collection",
"->",
"push",
"(",
"clone",
"$",
"item",
")",
";",
"$",
"i",
"=",
"new",
"Item",
"(",
"$",
"item",
",",
"$",
"this",
"->",
"resource_name",
")",
";",
"array_push",
"(",
"$",
"this",
"->",
"data",
",",
"$",
"i",
"->",
"getData",
"(",
")",
")",
";",
"}",
"$",
"this",
"->",
"response",
"[",
"'data'",
"]",
"=",
"$",
"this",
"->",
"data",
";",
"}"
] | Set collection data
@param type $collection
@param string $resource_name | [
"Set",
"collection",
"data"
] | train | https://github.com/jnaxo/country-codes/blob/ab1f24886e1b49eef5fd4b4eb4fb37aa7faeacd9/src/Store/api/Collection.php#L87-L103 |
sciactive/nymph-server | src/Nymph.php | Nymph.configure | public static function configure($config = []) {
$defaults = include dirname(__DIR__).'/conf/defaults.php';
self::$config = array_replace_recursive($defaults, $config);
if (isset(self::$driver)) {
if (self::$driver->connected) {
self::$driver->disconnect();
}
self::$driver = null;
}
$class = '\\Nymph\\Drivers\\'.self::$config['driver'].'Driver';
$Nymph = new $class(self::$config);
if (class_exists('\\SciActive\\Hook')) {
\SciActive\Hook::hookObject($Nymph, 'Nymph->');
}
if (self::$config['pubsub']) {
\Nymph\PubSub\HookMethods::setup();
}
self::$driver = $Nymph;
} | php | public static function configure($config = []) {
$defaults = include dirname(__DIR__).'/conf/defaults.php';
self::$config = array_replace_recursive($defaults, $config);
if (isset(self::$driver)) {
if (self::$driver->connected) {
self::$driver->disconnect();
}
self::$driver = null;
}
$class = '\\Nymph\\Drivers\\'.self::$config['driver'].'Driver';
$Nymph = new $class(self::$config);
if (class_exists('\\SciActive\\Hook')) {
\SciActive\Hook::hookObject($Nymph, 'Nymph->');
}
if (self::$config['pubsub']) {
\Nymph\PubSub\HookMethods::setup();
}
self::$driver = $Nymph;
} | [
"public",
"static",
"function",
"configure",
"(",
"$",
"config",
"=",
"[",
"]",
")",
"{",
"$",
"defaults",
"=",
"include",
"dirname",
"(",
"__DIR__",
")",
".",
"'/conf/defaults.php'",
";",
"self",
"::",
"$",
"config",
"=",
"array_replace_recursive",
"(",
"$",
"defaults",
",",
"$",
"config",
")",
";",
"if",
"(",
"isset",
"(",
"self",
"::",
"$",
"driver",
")",
")",
"{",
"if",
"(",
"self",
"::",
"$",
"driver",
"->",
"connected",
")",
"{",
"self",
"::",
"$",
"driver",
"->",
"disconnect",
"(",
")",
";",
"}",
"self",
"::",
"$",
"driver",
"=",
"null",
";",
"}",
"$",
"class",
"=",
"'\\\\Nymph\\\\Drivers\\\\'",
".",
"self",
"::",
"$",
"config",
"[",
"'driver'",
"]",
".",
"'Driver'",
";",
"$",
"Nymph",
"=",
"new",
"$",
"class",
"(",
"self",
"::",
"$",
"config",
")",
";",
"if",
"(",
"class_exists",
"(",
"'\\\\SciActive\\\\Hook'",
")",
")",
"{",
"\\",
"SciActive",
"\\",
"Hook",
"::",
"hookObject",
"(",
"$",
"Nymph",
",",
"'Nymph->'",
")",
";",
"}",
"if",
"(",
"self",
"::",
"$",
"config",
"[",
"'pubsub'",
"]",
")",
"{",
"\\",
"Nymph",
"\\",
"PubSub",
"\\",
"HookMethods",
"::",
"setup",
"(",
")",
";",
"}",
"self",
"::",
"$",
"driver",
"=",
"$",
"Nymph",
";",
"}"
] | Apply configuration to Nymph.
$config should be an associative array of Nymph configuration. Use the
following form:
[
'driver' => 'MySQL',
'pubsub' => true,
'MySql' => [
'host' => '127.0.0.1'
]
]
@param array $config An associative array of Nymph's configuration. | [
"Apply",
"configuration",
"to",
"Nymph",
"."
] | train | https://github.com/sciactive/nymph-server/blob/3c18dbf45c2750d07c798e14534dba87387beeba/src/Nymph.php#L55-L76 |
sciactive/nymph-server | src/Nymph.php | Nymph.checkData | public static function checkData(
&$data,
&$sdata,
$selectors,
$guid = null,
$tags = null,
$typesAlreadyChecked = [],
$dataValsAreadyChecked = []
) {
return self::$driver->checkData(
$data,
$sdata,
$selectors,
$guid,
$tags,
$typesAlreadyChecked,
$dataValsAreadyChecked
);
} | php | public static function checkData(
&$data,
&$sdata,
$selectors,
$guid = null,
$tags = null,
$typesAlreadyChecked = [],
$dataValsAreadyChecked = []
) {
return self::$driver->checkData(
$data,
$sdata,
$selectors,
$guid,
$tags,
$typesAlreadyChecked,
$dataValsAreadyChecked
);
} | [
"public",
"static",
"function",
"checkData",
"(",
"&",
"$",
"data",
",",
"&",
"$",
"sdata",
",",
"$",
"selectors",
",",
"$",
"guid",
"=",
"null",
",",
"$",
"tags",
"=",
"null",
",",
"$",
"typesAlreadyChecked",
"=",
"[",
"]",
",",
"$",
"dataValsAreadyChecked",
"=",
"[",
"]",
")",
"{",
"return",
"self",
"::",
"$",
"driver",
"->",
"checkData",
"(",
"$",
"data",
",",
"$",
"sdata",
",",
"$",
"selectors",
",",
"$",
"guid",
",",
"$",
"tags",
",",
"$",
"typesAlreadyChecked",
",",
"$",
"dataValsAreadyChecked",
")",
";",
"}"
] | Check entity data to see if it matches given selectors.
@param array $data An array of unserialized entity data. The data array
should contain the cdate and mdate.
@param array $sdata An array of serialized entity data. If a value here is
checked, it will be unserialized and placed in the
$data array.
@param array $selectors An array of formatted selectors.
@param int|null $guid The guid. If left null, guid will not be checked, and
automatically considered passing.
@param array|null $tags The tags array. If left null, tags will not be
checked, and automatically considered passing.
@param array $typesAlreadyChecked An array of clause types that have
already been checked. They will be
considered passing.
@param array $dataValsAreadyChecked An array of data values that have
already been checked. They will be
considered passing if the value is
identical.
@return boolean Whether the entity data passes the given selectors. | [
"Check",
"entity",
"data",
"to",
"see",
"if",
"it",
"matches",
"given",
"selectors",
"."
] | train | https://github.com/sciactive/nymph-server/blob/3c18dbf45c2750d07c798e14534dba87387beeba/src/Nymph.php#L101-L119 |
sciactive/nymph-server | src/Nymph.php | Nymph.hsort | public static function hsort(
&$array,
$property = null,
$parentProperty = null,
$caseSensitive = false,
$reverse = false
) {
return self::$driver->hsort(
$array,
$property,
$parentProperty,
$caseSensitive,
$reverse
);
} | php | public static function hsort(
&$array,
$property = null,
$parentProperty = null,
$caseSensitive = false,
$reverse = false
) {
return self::$driver->hsort(
$array,
$property,
$parentProperty,
$caseSensitive,
$reverse
);
} | [
"public",
"static",
"function",
"hsort",
"(",
"&",
"$",
"array",
",",
"$",
"property",
"=",
"null",
",",
"$",
"parentProperty",
"=",
"null",
",",
"$",
"caseSensitive",
"=",
"false",
",",
"$",
"reverse",
"=",
"false",
")",
"{",
"return",
"self",
"::",
"$",
"driver",
"->",
"hsort",
"(",
"$",
"array",
",",
"$",
"property",
",",
"$",
"parentProperty",
",",
"$",
"caseSensitive",
",",
"$",
"reverse",
")",
";",
"}"
] | Sort an array of entities hierarchically by a specified property's value.
Entities will be placed immediately after their parents. The
$parentProperty property must hold either null, or the entity's parent.
@param array &$array The array of entities.
@param string|null $property The name of the property to sort entities by.
@param string|null $parentProperty The name of the property which holds the
parent of the entity.
@param bool $caseSensitive Sort case sensitively.
@param bool $reverse Reverse the sort order. | [
"Sort",
"an",
"array",
"of",
"entities",
"hierarchically",
"by",
"a",
"specified",
"property",
"s",
"value",
"."
] | train | https://github.com/sciactive/nymph-server/blob/3c18dbf45c2750d07c798e14534dba87387beeba/src/Nymph.php#L160-L174 |
sciactive/nymph-server | src/Nymph.php | Nymph.sort | public static function sort(
&$array,
$property = null,
$caseSensitive = false,
$reverse = false
) {
return self::$driver->sort(
$array,
$property,
$caseSensitive,
$reverse
);
} | php | public static function sort(
&$array,
$property = null,
$caseSensitive = false,
$reverse = false
) {
return self::$driver->sort(
$array,
$property,
$caseSensitive,
$reverse
);
} | [
"public",
"static",
"function",
"sort",
"(",
"&",
"$",
"array",
",",
"$",
"property",
"=",
"null",
",",
"$",
"caseSensitive",
"=",
"false",
",",
"$",
"reverse",
"=",
"false",
")",
"{",
"return",
"self",
"::",
"$",
"driver",
"->",
"sort",
"(",
"$",
"array",
",",
"$",
"property",
",",
"$",
"caseSensitive",
",",
"$",
"reverse",
")",
";",
"}"
] | Sort an array of entities by a specified property's value.
@param array &$array The array of entities.
@param string|null $property The name of the property to sort entities by.
@param bool $caseSensitive Sort case sensitively.
@param bool $reverse Reverse the sort order. | [
"Sort",
"an",
"array",
"of",
"entities",
"by",
"a",
"specified",
"property",
"s",
"value",
"."
] | train | https://github.com/sciactive/nymph-server/blob/3c18dbf45c2750d07c798e14534dba87387beeba/src/Nymph.php#L213-L225 |
ronaldborla/chikka | src/Borla/Chikka/Models/Carrier.php | Carrier.identifyNetwork | static function identifyNetwork($carrierCode) {
// Loop through codes
foreach (static::codes() as $network=> $codes) {
// If found
if (in_array($carrierCode, $codes)) {
// Return network
return $network;
}
}
// Return false
return false;
} | php | static function identifyNetwork($carrierCode) {
// Loop through codes
foreach (static::codes() as $network=> $codes) {
// If found
if (in_array($carrierCode, $codes)) {
// Return network
return $network;
}
}
// Return false
return false;
} | [
"static",
"function",
"identifyNetwork",
"(",
"$",
"carrierCode",
")",
"{",
"// Loop through codes",
"foreach",
"(",
"static",
"::",
"codes",
"(",
")",
"as",
"$",
"network",
"=>",
"$",
"codes",
")",
"{",
"// If found",
"if",
"(",
"in_array",
"(",
"$",
"carrierCode",
",",
"$",
"codes",
")",
")",
"{",
"// Return network",
"return",
"$",
"network",
";",
"}",
"}",
"// Return false",
"return",
"false",
";",
"}"
] | Identify network
@param string|int $carrierCode Carrier code parsed by Utilities::parseMobileNumber() | [
"Identify",
"network"
] | train | https://github.com/ronaldborla/chikka/blob/446987706f81d5a0efbc8bd6b7d3b259d0527719/src/Borla/Chikka/Models/Carrier.php#L83-L94 |
infusephp/infuse | src/SymfonyHttpBridge.php | SymfonyHttpBridge.convertSymfonyRequest | public function convertSymfonyRequest(SymfonyRequest $request)
{
$session = $request->getSession();
if ($session) {
$session = $session->all();
} else {
$session = [];
}
// decode request parameters
$parameters = $request->request->all();
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH']) && 'json' == $request->getContentType()) {
$parameters = json_decode($request->getContent(), true);
}
// NOTE: This is not entirely accurate to use $_FILES, however,
// because Symfony converts $_FILES into an UploadedFile object
// and Infuse expects the raw array, it makes compatibility difficult.
$req = new Request($request->query->all(), $parameters, $request->cookies->all(), $_FILES, $request->server->all(), $session);
$req->setParams($request->attributes->all());
return $req;
} | php | public function convertSymfonyRequest(SymfonyRequest $request)
{
$session = $request->getSession();
if ($session) {
$session = $session->all();
} else {
$session = [];
}
// decode request parameters
$parameters = $request->request->all();
if (in_array($request->getMethod(), ['POST', 'PUT', 'PATCH']) && 'json' == $request->getContentType()) {
$parameters = json_decode($request->getContent(), true);
}
// NOTE: This is not entirely accurate to use $_FILES, however,
// because Symfony converts $_FILES into an UploadedFile object
// and Infuse expects the raw array, it makes compatibility difficult.
$req = new Request($request->query->all(), $parameters, $request->cookies->all(), $_FILES, $request->server->all(), $session);
$req->setParams($request->attributes->all());
return $req;
} | [
"public",
"function",
"convertSymfonyRequest",
"(",
"SymfonyRequest",
"$",
"request",
")",
"{",
"$",
"session",
"=",
"$",
"request",
"->",
"getSession",
"(",
")",
";",
"if",
"(",
"$",
"session",
")",
"{",
"$",
"session",
"=",
"$",
"session",
"->",
"all",
"(",
")",
";",
"}",
"else",
"{",
"$",
"session",
"=",
"[",
"]",
";",
"}",
"// decode request parameters",
"$",
"parameters",
"=",
"$",
"request",
"->",
"request",
"->",
"all",
"(",
")",
";",
"if",
"(",
"in_array",
"(",
"$",
"request",
"->",
"getMethod",
"(",
")",
",",
"[",
"'POST'",
",",
"'PUT'",
",",
"'PATCH'",
"]",
")",
"&&",
"'json'",
"==",
"$",
"request",
"->",
"getContentType",
"(",
")",
")",
"{",
"$",
"parameters",
"=",
"json_decode",
"(",
"$",
"request",
"->",
"getContent",
"(",
")",
",",
"true",
")",
";",
"}",
"// NOTE: This is not entirely accurate to use $_FILES, however,",
"// because Symfony converts $_FILES into an UploadedFile object",
"// and Infuse expects the raw array, it makes compatibility difficult.",
"$",
"req",
"=",
"new",
"Request",
"(",
"$",
"request",
"->",
"query",
"->",
"all",
"(",
")",
",",
"$",
"parameters",
",",
"$",
"request",
"->",
"cookies",
"->",
"all",
"(",
")",
",",
"$",
"_FILES",
",",
"$",
"request",
"->",
"server",
"->",
"all",
"(",
")",
",",
"$",
"session",
")",
";",
"$",
"req",
"->",
"setParams",
"(",
"$",
"request",
"->",
"attributes",
"->",
"all",
"(",
")",
")",
";",
"return",
"$",
"req",
";",
"}"
] | Converts a Symfony request object into an Infuse request object.
@param SymfonyRequest $request
@return Request | [
"Converts",
"a",
"Symfony",
"request",
"object",
"into",
"an",
"Infuse",
"request",
"object",
"."
] | train | https://github.com/infusephp/infuse/blob/7520b237c69f3d8a07d95d7481b26027ced2ed83/src/SymfonyHttpBridge.php#L22-L44 |
infusephp/infuse | src/SymfonyHttpBridge.php | SymfonyHttpBridge.convertInfuseResponse | public function convertInfuseResponse(Response $res)
{
$response = new SymfonyResponse($res->getBody(), $res->getCode(), $res->headers());
// transfer cookies
foreach ($res->cookies() as $name => $params) {
list($value, $expire, $path, $domain, $secure, $httponly) = $params;
$cookie = new Cookie($name, $value, $expire, $path, $domain, $secure, $httponly);
$response->headers->setCookie($cookie);
}
return $response;
} | php | public function convertInfuseResponse(Response $res)
{
$response = new SymfonyResponse($res->getBody(), $res->getCode(), $res->headers());
// transfer cookies
foreach ($res->cookies() as $name => $params) {
list($value, $expire, $path, $domain, $secure, $httponly) = $params;
$cookie = new Cookie($name, $value, $expire, $path, $domain, $secure, $httponly);
$response->headers->setCookie($cookie);
}
return $response;
} | [
"public",
"function",
"convertInfuseResponse",
"(",
"Response",
"$",
"res",
")",
"{",
"$",
"response",
"=",
"new",
"SymfonyResponse",
"(",
"$",
"res",
"->",
"getBody",
"(",
")",
",",
"$",
"res",
"->",
"getCode",
"(",
")",
",",
"$",
"res",
"->",
"headers",
"(",
")",
")",
";",
"// transfer cookies",
"foreach",
"(",
"$",
"res",
"->",
"cookies",
"(",
")",
"as",
"$",
"name",
"=>",
"$",
"params",
")",
"{",
"list",
"(",
"$",
"value",
",",
"$",
"expire",
",",
"$",
"path",
",",
"$",
"domain",
",",
"$",
"secure",
",",
"$",
"httponly",
")",
"=",
"$",
"params",
";",
"$",
"cookie",
"=",
"new",
"Cookie",
"(",
"$",
"name",
",",
"$",
"value",
",",
"$",
"expire",
",",
"$",
"path",
",",
"$",
"domain",
",",
"$",
"secure",
",",
"$",
"httponly",
")",
";",
"$",
"response",
"->",
"headers",
"->",
"setCookie",
"(",
"$",
"cookie",
")",
";",
"}",
"return",
"$",
"response",
";",
"}"
] | Converts an Infuse response object into a Symfony response object.
@param Response $res
@return SymfonyResponse | [
"Converts",
"an",
"Infuse",
"response",
"object",
"into",
"a",
"Symfony",
"response",
"object",
"."
] | train | https://github.com/infusephp/infuse/blob/7520b237c69f3d8a07d95d7481b26027ced2ed83/src/SymfonyHttpBridge.php#L53-L65 |
ajgarlag/AjglCsv | src/Reader/ReaderFactory.php | ReaderFactory.createReader | public function createReader(
$type,
$filePath,
$delimiter = ReaderInterface::DELIMITER_DEFAULT,
$fileCharset = ReaderInterface::CHARSET_DEFAULT,
$mode = 'r'
) {
switch ($type) {
case 'php':
return new NativePhpReader($filePath, $delimiter, $fileCharset, $mode);
break;
case 'rfc':
return new RfcReader($filePath, $delimiter, $fileCharset, $mode);
break;
default:
throw new \InvalidArgumentException(
sprintf("Unsupported reader type '%s'", $type)
);
break;
}
} | php | public function createReader(
$type,
$filePath,
$delimiter = ReaderInterface::DELIMITER_DEFAULT,
$fileCharset = ReaderInterface::CHARSET_DEFAULT,
$mode = 'r'
) {
switch ($type) {
case 'php':
return new NativePhpReader($filePath, $delimiter, $fileCharset, $mode);
break;
case 'rfc':
return new RfcReader($filePath, $delimiter, $fileCharset, $mode);
break;
default:
throw new \InvalidArgumentException(
sprintf("Unsupported reader type '%s'", $type)
);
break;
}
} | [
"public",
"function",
"createReader",
"(",
"$",
"type",
",",
"$",
"filePath",
",",
"$",
"delimiter",
"=",
"ReaderInterface",
"::",
"DELIMITER_DEFAULT",
",",
"$",
"fileCharset",
"=",
"ReaderInterface",
"::",
"CHARSET_DEFAULT",
",",
"$",
"mode",
"=",
"'r'",
")",
"{",
"switch",
"(",
"$",
"type",
")",
"{",
"case",
"'php'",
":",
"return",
"new",
"NativePhpReader",
"(",
"$",
"filePath",
",",
"$",
"delimiter",
",",
"$",
"fileCharset",
",",
"$",
"mode",
")",
";",
"break",
";",
"case",
"'rfc'",
":",
"return",
"new",
"RfcReader",
"(",
"$",
"filePath",
",",
"$",
"delimiter",
",",
"$",
"fileCharset",
",",
"$",
"mode",
")",
";",
"break",
";",
"default",
":",
"throw",
"new",
"\\",
"InvalidArgumentException",
"(",
"sprintf",
"(",
"\"Unsupported reader type '%s'\"",
",",
"$",
"type",
")",
")",
";",
"break",
";",
"}",
"}"
] | {@inheritdoc} | [
"{"
] | train | https://github.com/ajgarlag/AjglCsv/blob/28872f58b9ef864893cac6faddfcb1229c2c2047/src/Reader/ReaderFactory.php#L22-L42 |
hametuha/wpametu | src/WPametu/Utility/Command.php | Command.table | protected static function table($header, $body){
$table = new \cli\Table();
$table->setHeaders( $header );
$table->setRows( $body );
$table->display();
} | php | protected static function table($header, $body){
$table = new \cli\Table();
$table->setHeaders( $header );
$table->setRows( $body );
$table->display();
} | [
"protected",
"static",
"function",
"table",
"(",
"$",
"header",
",",
"$",
"body",
")",
"{",
"$",
"table",
"=",
"new",
"\\",
"cli",
"\\",
"Table",
"(",
")",
";",
"$",
"table",
"->",
"setHeaders",
"(",
"$",
"header",
")",
";",
"$",
"table",
"->",
"setRows",
"(",
"$",
"body",
")",
";",
"$",
"table",
"->",
"display",
"(",
")",
";",
"}"
] | Show table
@param array $header
@param array $body | [
"Show",
"table"
] | train | https://github.com/hametuha/wpametu/blob/0939373800815a8396291143d2a57967340da5aa/src/WPametu/Utility/Command.php#L69-L74 |
nabab/bbn | src/bbn/user/preferences.php | preferences._init_user | private function _init_user(bbn\user $user): preferences
{
$this->user = $user;
$this->id_user = $this->user->get_id();
$this->id_group = $this->user->get_group();
return $this;
} | php | private function _init_user(bbn\user $user): preferences
{
$this->user = $user;
$this->id_user = $this->user->get_id();
$this->id_group = $this->user->get_group();
return $this;
} | [
"private",
"function",
"_init_user",
"(",
"bbn",
"\\",
"user",
"$",
"user",
")",
":",
"preferences",
"{",
"$",
"this",
"->",
"user",
"=",
"$",
"user",
";",
"$",
"this",
"->",
"id_user",
"=",
"$",
"this",
"->",
"user",
"->",
"get_id",
"(",
")",
";",
"$",
"this",
"->",
"id_group",
"=",
"$",
"this",
"->",
"user",
"->",
"get_group",
"(",
")",
";",
"return",
"$",
"this",
";",
"}"
] | Sets the user variables using a user object
@param bbn\user $user
@return preferences | [
"Sets",
"the",
"user",
"variables",
"using",
"a",
"user",
"object"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L84-L90 |
nabab/bbn | src/bbn/user/preferences.php | preferences._get_id_option | private function _get_id_option(string $id_option = null): ?string
{
if ( !$id_option && !($id_option = $this->get_current()) ){
return null;
}
if ( $id_option && !bbn\str::is_uid($id_option) ){
$id_option = $this->opt->from_path(...\func_get_args());
}
if ( $id_option && bbn\str::is_uid($id_option) ){
return $id_option;
}
return null;
} | php | private function _get_id_option(string $id_option = null): ?string
{
if ( !$id_option && !($id_option = $this->get_current()) ){
return null;
}
if ( $id_option && !bbn\str::is_uid($id_option) ){
$id_option = $this->opt->from_path(...\func_get_args());
}
if ( $id_option && bbn\str::is_uid($id_option) ){
return $id_option;
}
return null;
} | [
"private",
"function",
"_get_id_option",
"(",
"string",
"$",
"id_option",
"=",
"null",
")",
":",
"?",
"string",
"{",
"if",
"(",
"!",
"$",
"id_option",
"&&",
"!",
"(",
"$",
"id_option",
"=",
"$",
"this",
"->",
"get_current",
"(",
")",
")",
")",
"{",
"return",
"null",
";",
"}",
"if",
"(",
"$",
"id_option",
"&&",
"!",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id_option",
")",
")",
"{",
"$",
"id_option",
"=",
"$",
"this",
"->",
"opt",
"->",
"from_path",
"(",
"...",
"\\",
"func_get_args",
"(",
")",
")",
";",
"}",
"if",
"(",
"$",
"id_option",
"&&",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id_option",
")",
")",
"{",
"return",
"$",
"id_option",
";",
"}",
"return",
"null",
";",
"}"
] | Retrieves or confirm the ID of an option based on the same parameters as options::from_path
@param string|null $id_option
@return null|string | [
"Retrieves",
"or",
"confirm",
"the",
"ID",
"of",
"an",
"option",
"based",
"on",
"the",
"same",
"parameters",
"as",
"options",
"::",
"from_path"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L98-L110 |
nabab/bbn | src/bbn/user/preferences.php | preferences._insert | private function _insert(string $id_option, array $cfg): int
{
$json = ($tmp = $this->get_cfg(false, $cfg)) ? json_encode($tmp) : NULL;
return $this->db->insert($this->class_cfg['table'], [
$this->fields['id_option'] => $id_option,
$this->fields['num'] => $cfg[$this->fields['num']] ?? NULL,
$this->fields['text'] => $cfg[$this->fields['text']] ?? NULL,
$this->fields['id_link'] => $cfg[$this->fields['id_link']] ?? NULL,
$this->fields['id_alias'] => $cfg[$this->fields['id_alias']] ?? NULL,
$this->fields['id_user'] => $this->id_user,
$this->fields['cfg'] => $json
]);
} | php | private function _insert(string $id_option, array $cfg): int
{
$json = ($tmp = $this->get_cfg(false, $cfg)) ? json_encode($tmp) : NULL;
return $this->db->insert($this->class_cfg['table'], [
$this->fields['id_option'] => $id_option,
$this->fields['num'] => $cfg[$this->fields['num']] ?? NULL,
$this->fields['text'] => $cfg[$this->fields['text']] ?? NULL,
$this->fields['id_link'] => $cfg[$this->fields['id_link']] ?? NULL,
$this->fields['id_alias'] => $cfg[$this->fields['id_alias']] ?? NULL,
$this->fields['id_user'] => $this->id_user,
$this->fields['cfg'] => $json
]);
} | [
"private",
"function",
"_insert",
"(",
"string",
"$",
"id_option",
",",
"array",
"$",
"cfg",
")",
":",
"int",
"{",
"$",
"json",
"=",
"(",
"$",
"tmp",
"=",
"$",
"this",
"->",
"get_cfg",
"(",
"false",
",",
"$",
"cfg",
")",
")",
"?",
"json_encode",
"(",
"$",
"tmp",
")",
":",
"NULL",
";",
"return",
"$",
"this",
"->",
"db",
"->",
"insert",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"=>",
"$",
"id_option",
",",
"$",
"this",
"->",
"fields",
"[",
"'num'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'num'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_alias'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_alias'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"=>",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"fields",
"[",
"'cfg'",
"]",
"=>",
"$",
"json",
"]",
")",
";",
"}"
] | Actually inserts a row into the preferences table
@param string $id_option
@param array $cfg
@return int | [
"Actually",
"inserts",
"a",
"row",
"into",
"the",
"preferences",
"table"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L119-L131 |
nabab/bbn | src/bbn/user/preferences.php | preferences._retrieve_ids | private function _retrieve_ids(string $id_option = null, string $id_user = null, string $id_group = null): ?array
{
if ( ($id_user || $id_group) && ($id_option = $this->_get_id_option($id_option)) ){
$col_id = $this->db->csn($this->fields['id'], true);
$table = $this->db->tsn($this->class_cfg['table'], true);
$id_opt = $this->db->csn($this->fields['id_option'], true);
$num = $this->db->csn($this->fields['num'], true);
$text = $this->db->csn($this->fields['text'], true);
$user = $this->db->csn($this->fields['id_user'], true);
$group = $this->db->csn($this->fields['id_group'], true);
$public = $this->db->csn($this->fields['public'], true);
$cond = [];
$args = [$id_option];
if ( null !== $id_user ){
$cond[] = "$user = UNHEX(?)";
$args[] = $id_user;
}
if ( null !== $id_group ){
$cond[] = "$group = UNHEX(?)";
$args[] = $id_group;
}
// Not specific
if ( (null !== $id_user) && (null !== $id_group) ){
$cond[] = "$public = 1";
}
$cond = implode(' OR ', $cond);
$sql = <<< MYSQL
SELECT $col_id
FROM $table
WHERE $id_opt = UNHEX(?)
AND ($cond)
ORDER BY IFNULL($num, $text)
MYSQL;
array_unshift($args, $sql);
return $this->db->get_col_array(...$args);
}
return null;
} | php | private function _retrieve_ids(string $id_option = null, string $id_user = null, string $id_group = null): ?array
{
if ( ($id_user || $id_group) && ($id_option = $this->_get_id_option($id_option)) ){
$col_id = $this->db->csn($this->fields['id'], true);
$table = $this->db->tsn($this->class_cfg['table'], true);
$id_opt = $this->db->csn($this->fields['id_option'], true);
$num = $this->db->csn($this->fields['num'], true);
$text = $this->db->csn($this->fields['text'], true);
$user = $this->db->csn($this->fields['id_user'], true);
$group = $this->db->csn($this->fields['id_group'], true);
$public = $this->db->csn($this->fields['public'], true);
$cond = [];
$args = [$id_option];
if ( null !== $id_user ){
$cond[] = "$user = UNHEX(?)";
$args[] = $id_user;
}
if ( null !== $id_group ){
$cond[] = "$group = UNHEX(?)";
$args[] = $id_group;
}
// Not specific
if ( (null !== $id_user) && (null !== $id_group) ){
$cond[] = "$public = 1";
}
$cond = implode(' OR ', $cond);
$sql = <<< MYSQL
SELECT $col_id
FROM $table
WHERE $id_opt = UNHEX(?)
AND ($cond)
ORDER BY IFNULL($num, $text)
MYSQL;
array_unshift($args, $sql);
return $this->db->get_col_array(...$args);
}
return null;
} | [
"private",
"function",
"_retrieve_ids",
"(",
"string",
"$",
"id_option",
"=",
"null",
",",
"string",
"$",
"id_user",
"=",
"null",
",",
"string",
"$",
"id_group",
"=",
"null",
")",
":",
"?",
"array",
"{",
"if",
"(",
"(",
"$",
"id_user",
"||",
"$",
"id_group",
")",
"&&",
"(",
"$",
"id_option",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_option",
")",
")",
")",
"{",
"$",
"col_id",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
",",
"true",
")",
";",
"$",
"table",
"=",
"$",
"this",
"->",
"db",
"->",
"tsn",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"true",
")",
";",
"$",
"id_opt",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
",",
"true",
")",
";",
"$",
"num",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'num'",
"]",
",",
"true",
")",
";",
"$",
"text",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
",",
"true",
")",
";",
"$",
"user",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
",",
"true",
")",
";",
"$",
"group",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_group'",
"]",
",",
"true",
")",
";",
"$",
"public",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'public'",
"]",
",",
"true",
")",
";",
"$",
"cond",
"=",
"[",
"]",
";",
"$",
"args",
"=",
"[",
"$",
"id_option",
"]",
";",
"if",
"(",
"null",
"!==",
"$",
"id_user",
")",
"{",
"$",
"cond",
"[",
"]",
"=",
"\"$user = UNHEX(?)\"",
";",
"$",
"args",
"[",
"]",
"=",
"$",
"id_user",
";",
"}",
"if",
"(",
"null",
"!==",
"$",
"id_group",
")",
"{",
"$",
"cond",
"[",
"]",
"=",
"\"$group = UNHEX(?)\"",
";",
"$",
"args",
"[",
"]",
"=",
"$",
"id_group",
";",
"}",
"// Not specific",
"if",
"(",
"(",
"null",
"!==",
"$",
"id_user",
")",
"&&",
"(",
"null",
"!==",
"$",
"id_group",
")",
")",
"{",
"$",
"cond",
"[",
"]",
"=",
"\"$public = 1\"",
";",
"}",
"$",
"cond",
"=",
"implode",
"(",
"' OR '",
",",
"$",
"cond",
")",
";",
"$",
"sql",
"=",
" <<< MYSQL\nSELECT $col_id\nFROM $table\nWHERE $id_opt = UNHEX(?)\nAND ($cond)\nORDER BY IFNULL($num, $text)\nMYSQL",
";",
"array_unshift",
"(",
"$",
"args",
",",
"$",
"sql",
")",
";",
"return",
"$",
"this",
"->",
"db",
"->",
"get_col_array",
"(",
"...",
"$",
"args",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Returns preferences' IDs from the option's ID
@param null|string $id_option
@param null|string $id_user
@param null|string $id_group
@return array|null | [
"Returns",
"preferences",
"IDs",
"from",
"the",
"option",
"s",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L141-L178 |
nabab/bbn | src/bbn/user/preferences.php | preferences._get_links | private function _get_links(string $id_link, string $id_user = null, string $id_group = null): ?array
{
if ( $id_link = $this->_get_id_option($id_link) ){
$where = [
'logic' => 'AND',
'conditions' => [
[
'field' => $this->fields['id_link'],
'operator' => '=',
'value' => $id_link
]
]
];
if ( null !== $id_user ){
$cond[$this->fields['id_user']] = $id_user;
}
if ( null !== $id_group ){
$cond[$this->fields['id_group']] = $id_group;
}
// Not specific
if ( (null === $id_user) && (null === $id_group) ){
$cond[$this->fields['public']] = 1;
}
$where['conditions'][] = [
'logic' => 'OR',
'conditions' => $cond
];
return $this->db->rselect_all([
'tables' => [$this->class_cfg['table']],
'fields' => [$this->fields['id'], $this->fields['id_option']],
'where' => $where,
'order' => [$this->fields['text']]
]);
}
return null;
} | php | private function _get_links(string $id_link, string $id_user = null, string $id_group = null): ?array
{
if ( $id_link = $this->_get_id_option($id_link) ){
$where = [
'logic' => 'AND',
'conditions' => [
[
'field' => $this->fields['id_link'],
'operator' => '=',
'value' => $id_link
]
]
];
if ( null !== $id_user ){
$cond[$this->fields['id_user']] = $id_user;
}
if ( null !== $id_group ){
$cond[$this->fields['id_group']] = $id_group;
}
// Not specific
if ( (null === $id_user) && (null === $id_group) ){
$cond[$this->fields['public']] = 1;
}
$where['conditions'][] = [
'logic' => 'OR',
'conditions' => $cond
];
return $this->db->rselect_all([
'tables' => [$this->class_cfg['table']],
'fields' => [$this->fields['id'], $this->fields['id_option']],
'where' => $where,
'order' => [$this->fields['text']]
]);
}
return null;
} | [
"private",
"function",
"_get_links",
"(",
"string",
"$",
"id_link",
",",
"string",
"$",
"id_user",
"=",
"null",
",",
"string",
"$",
"id_group",
"=",
"null",
")",
":",
"?",
"array",
"{",
"if",
"(",
"$",
"id_link",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_link",
")",
")",
"{",
"$",
"where",
"=",
"[",
"'logic'",
"=>",
"'AND'",
",",
"'conditions'",
"=>",
"[",
"[",
"'field'",
"=>",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
",",
"'operator'",
"=>",
"'='",
",",
"'value'",
"=>",
"$",
"id_link",
"]",
"]",
"]",
";",
"if",
"(",
"null",
"!==",
"$",
"id_user",
")",
"{",
"$",
"cond",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"]",
"=",
"$",
"id_user",
";",
"}",
"if",
"(",
"null",
"!==",
"$",
"id_group",
")",
"{",
"$",
"cond",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_group'",
"]",
"]",
"=",
"$",
"id_group",
";",
"}",
"// Not specific",
"if",
"(",
"(",
"null",
"===",
"$",
"id_user",
")",
"&&",
"(",
"null",
"===",
"$",
"id_group",
")",
")",
"{",
"$",
"cond",
"[",
"$",
"this",
"->",
"fields",
"[",
"'public'",
"]",
"]",
"=",
"1",
";",
"}",
"$",
"where",
"[",
"'conditions'",
"]",
"[",
"]",
"=",
"[",
"'logic'",
"=>",
"'OR'",
",",
"'conditions'",
"=>",
"$",
"cond",
"]",
";",
"return",
"$",
"this",
"->",
"db",
"->",
"rselect_all",
"(",
"[",
"'tables'",
"=>",
"[",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
"]",
",",
"'fields'",
"=>",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"]",
",",
"'where'",
"=>",
"$",
"where",
",",
"'order'",
"=>",
"[",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
"]",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Gets the preferences which have the option's $id as id_link
@param string $id_link
@return array|null | [
"Gets",
"the",
"preferences",
"which",
"have",
"the",
"option",
"s",
"$id",
"as",
"id_link"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L186-L221 |
nabab/bbn | src/bbn/user/preferences.php | preferences.retrieve_ids | public function retrieve_ids(string $id_option = null): ?array
{
return $this->_retrieve_ids($id_option, $this->id_user, $this->id_group);
} | php | public function retrieve_ids(string $id_option = null): ?array
{
return $this->_retrieve_ids($id_option, $this->id_user, $this->id_group);
} | [
"public",
"function",
"retrieve_ids",
"(",
"string",
"$",
"id_option",
"=",
"null",
")",
":",
"?",
"array",
"{",
"return",
"$",
"this",
"->",
"_retrieve_ids",
"(",
"$",
"id_option",
",",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"id_group",
")",
";",
"}"
] | Returns preferences' IDs from the option's ID
@param null|string $id_option
@return null|array | [
"Returns",
"preferences",
"IDs",
"from",
"the",
"option",
"s",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L262-L265 |
nabab/bbn | src/bbn/user/preferences.php | preferences.retrieve_user_ids | public function retrieve_user_ids(string $id_option = null, string $id_user): ?array
{
return $this->_retrieve_ids($id_option, $id_user);
} | php | public function retrieve_user_ids(string $id_option = null, string $id_user): ?array
{
return $this->_retrieve_ids($id_option, $id_user);
} | [
"public",
"function",
"retrieve_user_ids",
"(",
"string",
"$",
"id_option",
"=",
"null",
",",
"string",
"$",
"id_user",
")",
":",
"?",
"array",
"{",
"return",
"$",
"this",
"->",
"_retrieve_ids",
"(",
"$",
"id_option",
",",
"$",
"id_user",
")",
";",
"}"
] | Returns preferences' IDs from the option's ID and the given user ID
@param null|string $id_option
@param string $id_user
@return array|null | [
"Returns",
"preferences",
"IDs",
"from",
"the",
"option",
"s",
"ID",
"and",
"the",
"given",
"user",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L274-L277 |
nabab/bbn | src/bbn/user/preferences.php | preferences.retrieve_group_ids | public function retrieve_group_ids(string $id_option = null, string $id_group): ?array
{
return $this->_retrieve_ids($id_option, null, $id_group);
} | php | public function retrieve_group_ids(string $id_option = null, string $id_group): ?array
{
return $this->_retrieve_ids($id_option, null, $id_group);
} | [
"public",
"function",
"retrieve_group_ids",
"(",
"string",
"$",
"id_option",
"=",
"null",
",",
"string",
"$",
"id_group",
")",
":",
"?",
"array",
"{",
"return",
"$",
"this",
"->",
"_retrieve_ids",
"(",
"$",
"id_option",
",",
"null",
",",
"$",
"id_group",
")",
";",
"}"
] | Returns preferences' IDs from the option's ID and the given group ID
@param null|string $id_option
@param string $id_group
@return array|null | [
"Returns",
"preferences",
"IDs",
"from",
"the",
"option",
"s",
"ID",
"and",
"the",
"given",
"group",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L286-L289 |
nabab/bbn | src/bbn/user/preferences.php | preferences.has | public function has(string $id_option = null, bool $force = false): bool
{
if ( !$force && $this->user->is_dev() ){
return true;
}
return (bool)$this->retrieve_ids($id_option);
} | php | public function has(string $id_option = null, bool $force = false): bool
{
if ( !$force && $this->user->is_dev() ){
return true;
}
return (bool)$this->retrieve_ids($id_option);
} | [
"public",
"function",
"has",
"(",
"string",
"$",
"id_option",
"=",
"null",
",",
"bool",
"$",
"force",
"=",
"false",
")",
":",
"bool",
"{",
"if",
"(",
"!",
"$",
"force",
"&&",
"$",
"this",
"->",
"user",
"->",
"is_dev",
"(",
")",
")",
"{",
"return",
"true",
";",
"}",
"return",
"(",
"bool",
")",
"$",
"this",
"->",
"retrieve_ids",
"(",
"$",
"id_option",
")",
";",
"}"
] | Returns true if the current user can access a preference, false otherwise
@param string|null $id_option
@param bool $force
@return bool | [
"Returns",
"true",
"if",
"the",
"current",
"user",
"can",
"access",
"a",
"preference",
"false",
"otherwise"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L298-L304 |
nabab/bbn | src/bbn/user/preferences.php | preferences.user_has | public function user_has(string $id_option, string $id_user): bool
{
return (bool)$this->_retrieve_ids($id_option, $id_user);
} | php | public function user_has(string $id_option, string $id_user): bool
{
return (bool)$this->_retrieve_ids($id_option, $id_user);
} | [
"public",
"function",
"user_has",
"(",
"string",
"$",
"id_option",
",",
"string",
"$",
"id_user",
")",
":",
"bool",
"{",
"return",
"(",
"bool",
")",
"$",
"this",
"->",
"_retrieve_ids",
"(",
"$",
"id_option",
",",
"$",
"id_user",
")",
";",
"}"
] | Checks if a user has the given preference
@param string $id_option
@param string $id_user
@return bool | [
"Checks",
"if",
"a",
"user",
"has",
"the",
"given",
"preference"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L313-L316 |
nabab/bbn | src/bbn/user/preferences.php | preferences.group_has | public function group_has(string $id_option, string $id_group): bool
{
return (bool)$this->_retrieve_ids($id_option, null, $id_group);
} | php | public function group_has(string $id_option, string $id_group): bool
{
return (bool)$this->_retrieve_ids($id_option, null, $id_group);
} | [
"public",
"function",
"group_has",
"(",
"string",
"$",
"id_option",
",",
"string",
"$",
"id_group",
")",
":",
"bool",
"{",
"return",
"(",
"bool",
")",
"$",
"this",
"->",
"_retrieve_ids",
"(",
"$",
"id_option",
",",
"null",
",",
"$",
"id_group",
")",
";",
"}"
] | Checks if a group has the given preference
@param string $id_option
@param string $id_group
@return bool | [
"Checks",
"if",
"a",
"group",
"has",
"the",
"given",
"preference"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L325-L328 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_cfg | public function get_cfg(string $id = null, array $cfg = null): ?array
{
if (
(null !== $cfg) ||
($cfg = $this->db->select_one(
$this->class_cfg['table'],
$this->fields['cfg'],
[$this->fields['id'] => $id ]
))
){
if ( bbn\str::is_json($cfg) ){
$cfg = json_decode($cfg, 1);
}
if ( \is_array($cfg) ){
$new = [];
foreach ( $cfg as $k => $v){
if ( !\in_array($k, $this->fields, true) ){
$new[$k] = $v;
}
}
return $new;
}
}
return null;
} | php | public function get_cfg(string $id = null, array $cfg = null): ?array
{
if (
(null !== $cfg) ||
($cfg = $this->db->select_one(
$this->class_cfg['table'],
$this->fields['cfg'],
[$this->fields['id'] => $id ]
))
){
if ( bbn\str::is_json($cfg) ){
$cfg = json_decode($cfg, 1);
}
if ( \is_array($cfg) ){
$new = [];
foreach ( $cfg as $k => $v){
if ( !\in_array($k, $this->fields, true) ){
$new[$k] = $v;
}
}
return $new;
}
}
return null;
} | [
"public",
"function",
"get_cfg",
"(",
"string",
"$",
"id",
"=",
"null",
",",
"array",
"$",
"cfg",
"=",
"null",
")",
":",
"?",
"array",
"{",
"if",
"(",
"(",
"null",
"!==",
"$",
"cfg",
")",
"||",
"(",
"$",
"cfg",
"=",
"$",
"this",
"->",
"db",
"->",
"select_one",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"$",
"this",
"->",
"fields",
"[",
"'cfg'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
")",
")",
"{",
"if",
"(",
"bbn",
"\\",
"str",
"::",
"is_json",
"(",
"$",
"cfg",
")",
")",
"{",
"$",
"cfg",
"=",
"json_decode",
"(",
"$",
"cfg",
",",
"1",
")",
";",
"}",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"cfg",
")",
")",
"{",
"$",
"new",
"=",
"[",
"]",
";",
"foreach",
"(",
"$",
"cfg",
"as",
"$",
"k",
"=>",
"$",
"v",
")",
"{",
"if",
"(",
"!",
"\\",
"in_array",
"(",
"$",
"k",
",",
"$",
"this",
"->",
"fields",
",",
"true",
")",
")",
"{",
"$",
"new",
"[",
"$",
"k",
"]",
"=",
"$",
"v",
";",
"}",
"}",
"return",
"$",
"new",
";",
"}",
"}",
"return",
"null",
";",
"}"
] | Gets the cfg array, normalized either from the DB or from the $cfg argument
@param string $id
@param null|array $cfg
@return null|array | [
"Gets",
"the",
"cfg",
"array",
"normalized",
"either",
"from",
"the",
"DB",
"or",
"from",
"the",
"$cfg",
"argument"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L363-L387 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_cfg_by_option | public function get_cfg_by_option(string $id_option, string $id_user = null): ?array
{
if (
($cfg = $this->db->select_one(
$this->class_cfg['table'],
$this->fields['cfg'],
[
$this->fields['id_option'] => $id_option,
$this->fields['id_user'] => $id_user ?: $this->id_user,
]
))
){
if ( bbn\str::is_json($cfg) ){
$cfg = json_decode($cfg, 1);
}
return $this->get_cfg(false, $cfg);
}
return null;
} | php | public function get_cfg_by_option(string $id_option, string $id_user = null): ?array
{
if (
($cfg = $this->db->select_one(
$this->class_cfg['table'],
$this->fields['cfg'],
[
$this->fields['id_option'] => $id_option,
$this->fields['id_user'] => $id_user ?: $this->id_user,
]
))
){
if ( bbn\str::is_json($cfg) ){
$cfg = json_decode($cfg, 1);
}
return $this->get_cfg(false, $cfg);
}
return null;
} | [
"public",
"function",
"get_cfg_by_option",
"(",
"string",
"$",
"id_option",
",",
"string",
"$",
"id_user",
"=",
"null",
")",
":",
"?",
"array",
"{",
"if",
"(",
"(",
"$",
"cfg",
"=",
"$",
"this",
"->",
"db",
"->",
"select_one",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"$",
"this",
"->",
"fields",
"[",
"'cfg'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"=>",
"$",
"id_option",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"=>",
"$",
"id_user",
"?",
":",
"$",
"this",
"->",
"id_user",
",",
"]",
")",
")",
")",
"{",
"if",
"(",
"bbn",
"\\",
"str",
"::",
"is_json",
"(",
"$",
"cfg",
")",
")",
"{",
"$",
"cfg",
"=",
"json_decode",
"(",
"$",
"cfg",
",",
"1",
")",
";",
"}",
"return",
"$",
"this",
"->",
"get_cfg",
"(",
"false",
",",
"$",
"cfg",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Gets the cfg array, normalized either from the DB or from the $cfg argument
@param string $id
@param null|array $cfg
@return null|array | [
"Gets",
"the",
"cfg",
"array",
"normalized",
"either",
"from",
"the",
"DB",
"or",
"from",
"the",
"$cfg",
"argument"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L396-L414 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_links | public function get_links(string $id): ?array
{
return $this->_get_links($id, $this->id_user, $this->id_group);
} | php | public function get_links(string $id): ?array
{
return $this->_get_links($id, $this->id_user, $this->id_group);
} | [
"public",
"function",
"get_links",
"(",
"string",
"$",
"id",
")",
":",
"?",
"array",
"{",
"return",
"$",
"this",
"->",
"_get_links",
"(",
"$",
"id",
",",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"id_group",
")",
";",
"}"
] | Gets the preferences which have the option's $id as id_link
@param string $id
@return array|null | [
"Gets",
"the",
"preferences",
"which",
"have",
"the",
"option",
"s",
"$id",
"as",
"id_link"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L422-L425 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get | public function get(string $id, bool $with_config = true): ?array
{
if ( bbn\str::is_uid($id) ){
$cols = implode(', ', array_map(function($a){
return $a;
}, $this->fields));
$table = $this->db->tsn($this->class_cfg['table'], true);
$uid = $this->db->csn($this->fields['id'], true);
$id_user = $this->db->csn($this->fields['id_user'], true);
$id_group = $this->db->csn($this->fields['id_group'], true);
$public = $this->db->csn($this->fields['public'], true);
$sql = <<< MYSQL
SELECT $cols
FROM $table
WHERE $uid = UNHEX(?)
AND ($id_user = UNHEX(?)
OR $id_group = UNHEX(?)
OR $public = 1)
MYSQL;
if ( $row = $this->db->get_row($sql, $id, $this->id_user, $this->id_group) ){
$cfg = $row['cfg'];
unset($row['cfg']);
if ( ($cfg = json_decode($cfg, true)) && $with_config ){
$row = bbn\x::merge_arrays($cfg, $row);
}
return $row;
}
}
return null;
} | php | public function get(string $id, bool $with_config = true): ?array
{
if ( bbn\str::is_uid($id) ){
$cols = implode(', ', array_map(function($a){
return $a;
}, $this->fields));
$table = $this->db->tsn($this->class_cfg['table'], true);
$uid = $this->db->csn($this->fields['id'], true);
$id_user = $this->db->csn($this->fields['id_user'], true);
$id_group = $this->db->csn($this->fields['id_group'], true);
$public = $this->db->csn($this->fields['public'], true);
$sql = <<< MYSQL
SELECT $cols
FROM $table
WHERE $uid = UNHEX(?)
AND ($id_user = UNHEX(?)
OR $id_group = UNHEX(?)
OR $public = 1)
MYSQL;
if ( $row = $this->db->get_row($sql, $id, $this->id_user, $this->id_group) ){
$cfg = $row['cfg'];
unset($row['cfg']);
if ( ($cfg = json_decode($cfg, true)) && $with_config ){
$row = bbn\x::merge_arrays($cfg, $row);
}
return $row;
}
}
return null;
} | [
"public",
"function",
"get",
"(",
"string",
"$",
"id",
",",
"bool",
"$",
"with_config",
"=",
"true",
")",
":",
"?",
"array",
"{",
"if",
"(",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
")",
"{",
"$",
"cols",
"=",
"implode",
"(",
"', '",
",",
"array_map",
"(",
"function",
"(",
"$",
"a",
")",
"{",
"return",
"$",
"a",
";",
"}",
",",
"$",
"this",
"->",
"fields",
")",
")",
";",
"$",
"table",
"=",
"$",
"this",
"->",
"db",
"->",
"tsn",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"true",
")",
";",
"$",
"uid",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
",",
"true",
")",
";",
"$",
"id_user",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
",",
"true",
")",
";",
"$",
"id_group",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_group'",
"]",
",",
"true",
")",
";",
"$",
"public",
"=",
"$",
"this",
"->",
"db",
"->",
"csn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'public'",
"]",
",",
"true",
")",
";",
"$",
"sql",
"=",
" <<< MYSQL\nSELECT $cols\nFROM $table\nWHERE $uid = UNHEX(?)\nAND ($id_user = UNHEX(?)\nOR $id_group = UNHEX(?)\nOR $public = 1)\nMYSQL",
";",
"if",
"(",
"$",
"row",
"=",
"$",
"this",
"->",
"db",
"->",
"get_row",
"(",
"$",
"sql",
",",
"$",
"id",
",",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"id_group",
")",
")",
"{",
"$",
"cfg",
"=",
"$",
"row",
"[",
"'cfg'",
"]",
";",
"unset",
"(",
"$",
"row",
"[",
"'cfg'",
"]",
")",
";",
"if",
"(",
"(",
"$",
"cfg",
"=",
"json_decode",
"(",
"$",
"cfg",
",",
"true",
")",
")",
"&&",
"$",
"with_config",
")",
"{",
"$",
"row",
"=",
"bbn",
"\\",
"x",
"::",
"merge_arrays",
"(",
"$",
"cfg",
",",
"$",
"row",
")",
";",
"}",
"return",
"$",
"row",
";",
"}",
"}",
"return",
"null",
";",
"}"
] | Returns the current user's preference based on the given id, his own profile and his group's
@param string $id
@param bool $with_config
@return array|null | [
"Returns",
"the",
"current",
"user",
"s",
"preference",
"based",
"on",
"the",
"given",
"id",
"his",
"own",
"profile",
"and",
"his",
"group",
"s"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L433-L462 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_all | public function get_all(string $id_option = null, bool $with_config = true): ?array
{
if ( $id_option = $this->_get_id_option($id_option) ){
$fields = $this->fields;
if ( !$with_config ){
unset($fields['cfg']);
}
$table = $this->db->tsn($this->class_table, true);
$cols = implode(', ', array_map(function($a) use($table){
return $a === $this->fields['id'] ? "$table.$a" : "IFNULL(aliases.$a, $table.$a) AS $a";
}, $fields));
$id_opt = $this->db->cfn($this->fields['id_option'], $this->class_table, true);
$id_user = $this->db->cfn($this->fields['id_user'], $this->class_table, true);
$id_group = $this->db->cfn($this->fields['id_group'], $this->class_table, true);
$num = $this->db->cfn($this->fields['num'], $this->class_table, true);
$text = $this->db->cfn($this->fields['text'], $this->class_table, true);
$id_alias = $this->db->cfn($this->fields['id_alias'], $this->class_table, true);
$public = $this->db->cfn($this->fields['public'], $this->class_table, true);
$sql = <<< MYSQL
SELECT $cols
FROM $table
LEFT JOIN $table as aliases
ON aliases.id = $id_alias
WHERE $id_opt = UNHEX(?)
AND ($id_user = UNHEX(?)
OR $id_group = UNHEX(?)
OR $public = 1)
ORDER BY IFNULL($num, $text)
MYSQL;
if ( $rows = $this->db->get_rows($sql, $id_option, $this->id_user, $this->id_group) ){
return $with_config ? array_map(function($a){
$cfg = $a['cfg'];
unset($a['cfg']);
if ( ($cfg = json_decode($cfg, true)) ){
$a = bbn\x::merge_arrays($cfg, $a);
}
return $a;
}, $rows) : $rows;
}
return [];
}
return null;
} | php | public function get_all(string $id_option = null, bool $with_config = true): ?array
{
if ( $id_option = $this->_get_id_option($id_option) ){
$fields = $this->fields;
if ( !$with_config ){
unset($fields['cfg']);
}
$table = $this->db->tsn($this->class_table, true);
$cols = implode(', ', array_map(function($a) use($table){
return $a === $this->fields['id'] ? "$table.$a" : "IFNULL(aliases.$a, $table.$a) AS $a";
}, $fields));
$id_opt = $this->db->cfn($this->fields['id_option'], $this->class_table, true);
$id_user = $this->db->cfn($this->fields['id_user'], $this->class_table, true);
$id_group = $this->db->cfn($this->fields['id_group'], $this->class_table, true);
$num = $this->db->cfn($this->fields['num'], $this->class_table, true);
$text = $this->db->cfn($this->fields['text'], $this->class_table, true);
$id_alias = $this->db->cfn($this->fields['id_alias'], $this->class_table, true);
$public = $this->db->cfn($this->fields['public'], $this->class_table, true);
$sql = <<< MYSQL
SELECT $cols
FROM $table
LEFT JOIN $table as aliases
ON aliases.id = $id_alias
WHERE $id_opt = UNHEX(?)
AND ($id_user = UNHEX(?)
OR $id_group = UNHEX(?)
OR $public = 1)
ORDER BY IFNULL($num, $text)
MYSQL;
if ( $rows = $this->db->get_rows($sql, $id_option, $this->id_user, $this->id_group) ){
return $with_config ? array_map(function($a){
$cfg = $a['cfg'];
unset($a['cfg']);
if ( ($cfg = json_decode($cfg, true)) ){
$a = bbn\x::merge_arrays($cfg, $a);
}
return $a;
}, $rows) : $rows;
}
return [];
}
return null;
} | [
"public",
"function",
"get_all",
"(",
"string",
"$",
"id_option",
"=",
"null",
",",
"bool",
"$",
"with_config",
"=",
"true",
")",
":",
"?",
"array",
"{",
"if",
"(",
"$",
"id_option",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_option",
")",
")",
"{",
"$",
"fields",
"=",
"$",
"this",
"->",
"fields",
";",
"if",
"(",
"!",
"$",
"with_config",
")",
"{",
"unset",
"(",
"$",
"fields",
"[",
"'cfg'",
"]",
")",
";",
"}",
"$",
"table",
"=",
"$",
"this",
"->",
"db",
"->",
"tsn",
"(",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"cols",
"=",
"implode",
"(",
"', '",
",",
"array_map",
"(",
"function",
"(",
"$",
"a",
")",
"use",
"(",
"$",
"table",
")",
"{",
"return",
"$",
"a",
"===",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"?",
"\"$table.$a\"",
":",
"\"IFNULL(aliases.$a, $table.$a) AS $a\"",
";",
"}",
",",
"$",
"fields",
")",
")",
";",
"$",
"id_opt",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"id_user",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"id_group",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_group'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"num",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'num'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"text",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"id_alias",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'id_alias'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"public",
"=",
"$",
"this",
"->",
"db",
"->",
"cfn",
"(",
"$",
"this",
"->",
"fields",
"[",
"'public'",
"]",
",",
"$",
"this",
"->",
"class_table",
",",
"true",
")",
";",
"$",
"sql",
"=",
" <<< MYSQL\nSELECT $cols\nFROM $table\n LEFT JOIN $table as aliases\n ON aliases.id = $id_alias\nWHERE $id_opt = UNHEX(?)\nAND ($id_user = UNHEX(?)\nOR $id_group = UNHEX(?)\nOR $public = 1)\nORDER BY IFNULL($num, $text)\nMYSQL",
";",
"if",
"(",
"$",
"rows",
"=",
"$",
"this",
"->",
"db",
"->",
"get_rows",
"(",
"$",
"sql",
",",
"$",
"id_option",
",",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"id_group",
")",
")",
"{",
"return",
"$",
"with_config",
"?",
"array_map",
"(",
"function",
"(",
"$",
"a",
")",
"{",
"$",
"cfg",
"=",
"$",
"a",
"[",
"'cfg'",
"]",
";",
"unset",
"(",
"$",
"a",
"[",
"'cfg'",
"]",
")",
";",
"if",
"(",
"(",
"$",
"cfg",
"=",
"json_decode",
"(",
"$",
"cfg",
",",
"true",
")",
")",
")",
"{",
"$",
"a",
"=",
"bbn",
"\\",
"x",
"::",
"merge_arrays",
"(",
"$",
"cfg",
",",
"$",
"a",
")",
";",
"}",
"return",
"$",
"a",
";",
"}",
",",
"$",
"rows",
")",
":",
"$",
"rows",
";",
"}",
"return",
"[",
"]",
";",
"}",
"return",
"null",
";",
"}"
] | Returns an array of the current user's preferences based on the given id_option, his own profile and his group's
@param null|string $id_option
@param bool $with_config
@return array|null | [
"Returns",
"an",
"array",
"of",
"the",
"current",
"user",
"s",
"preferences",
"based",
"on",
"the",
"given",
"id_option",
"his",
"own",
"profile",
"and",
"his",
"group",
"s"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L470-L513 |
nabab/bbn | src/bbn/user/preferences.php | preferences.set_by_option | public function set_by_option(string $id_option, array $cfg): int
{
if ( $id = $this->retrieve_user_ids($id_option, $this->id_user) ){
return $this->set($id[0], $cfg);
}
return $this->_insert($id_option, $cfg);
} | php | public function set_by_option(string $id_option, array $cfg): int
{
if ( $id = $this->retrieve_user_ids($id_option, $this->id_user) ){
return $this->set($id[0], $cfg);
}
return $this->_insert($id_option, $cfg);
} | [
"public",
"function",
"set_by_option",
"(",
"string",
"$",
"id_option",
",",
"array",
"$",
"cfg",
")",
":",
"int",
"{",
"if",
"(",
"$",
"id",
"=",
"$",
"this",
"->",
"retrieve_user_ids",
"(",
"$",
"id_option",
",",
"$",
"this",
"->",
"id_user",
")",
")",
"{",
"return",
"$",
"this",
"->",
"set",
"(",
"$",
"id",
"[",
"0",
"]",
",",
"$",
"cfg",
")",
";",
"}",
"return",
"$",
"this",
"->",
"_insert",
"(",
"$",
"id_option",
",",
"$",
"cfg",
")",
";",
"}"
] | Sets the permission row for the current user by the option's ID
@param string $id_option
@param array $cfg
@return int | [
"Sets",
"the",
"permission",
"row",
"for",
"the",
"current",
"user",
"by",
"the",
"option",
"s",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L643-L649 |
nabab/bbn | src/bbn/user/preferences.php | preferences.set | public function set(string $id, array $cfg = null): int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['cfg'] => $cfg ? json_encode($this->get_cfg(false, $cfg)) : null
], [
$this->fields['id'] => $id
]);
} | php | public function set(string $id, array $cfg = null): int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['cfg'] => $cfg ? json_encode($this->get_cfg(false, $cfg)) : null
], [
$this->fields['id'] => $id
]);
} | [
"public",
"function",
"set",
"(",
"string",
"$",
"id",
",",
"array",
"$",
"cfg",
"=",
"null",
")",
":",
"int",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'cfg'",
"]",
"=>",
"$",
"cfg",
"?",
"json_encode",
"(",
"$",
"this",
"->",
"get_cfg",
"(",
"false",
",",
"$",
"cfg",
")",
")",
":",
"null",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}"
] | Sets the permission config for the current user by the preference's ID
@param string $id
@param array $cfg
@return int | [
"Sets",
"the",
"permission",
"config",
"for",
"the",
"current",
"user",
"by",
"the",
"preference",
"s",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L658-L665 |
nabab/bbn | src/bbn/user/preferences.php | preferences.update | public function update(string $id, array $cfg): int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['text'] => $cfg[$this->fields['text']] ?? NULL,
$this->fields['num'] => $cfg[$this->fields['num']] ?? NULL,
$this->fields['id_link'] => $cfg[$this->fields['id_link']] ?? NULL,
$this->fields['id_alias'] => $cfg[$this->fields['id_alias']] ?? NULL,
$this->fields['id_user'] => $this->id_user,
$this->fields['cfg'] => ($mp = $this->get_cfg(false, $cfg)) ? json_encode($tmp) : NULL
], [
$this->fields['id'] => $id
]);
} | php | public function update(string $id, array $cfg): int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['text'] => $cfg[$this->fields['text']] ?? NULL,
$this->fields['num'] => $cfg[$this->fields['num']] ?? NULL,
$this->fields['id_link'] => $cfg[$this->fields['id_link']] ?? NULL,
$this->fields['id_alias'] => $cfg[$this->fields['id_alias']] ?? NULL,
$this->fields['id_user'] => $this->id_user,
$this->fields['cfg'] => ($mp = $this->get_cfg(false, $cfg)) ? json_encode($tmp) : NULL
], [
$this->fields['id'] => $id
]);
} | [
"public",
"function",
"update",
"(",
"string",
"$",
"id",
",",
"array",
"$",
"cfg",
")",
":",
"int",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'num'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'num'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_alias'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_alias'",
"]",
"]",
"??",
"NULL",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"=>",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"fields",
"[",
"'cfg'",
"]",
"=>",
"(",
"$",
"mp",
"=",
"$",
"this",
"->",
"get_cfg",
"(",
"false",
",",
"$",
"cfg",
")",
")",
"?",
"json_encode",
"(",
"$",
"tmp",
")",
":",
"NULL",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}"
] | Sets the permission row for the current user by the preference's ID
@param string $id
@param array $cfg
@return int | [
"Sets",
"the",
"permission",
"row",
"for",
"the",
"current",
"user",
"by",
"the",
"preference",
"s",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L674-L686 |
nabab/bbn | src/bbn/user/preferences.php | preferences.add | public function add(string $id_option = null, array $cfg): ?string
{
return (
($id_option = $this->_get_id_option($id_option)) &&
$this->_insert($id_option, $cfg)
) ? $this->db->last_id() : null;
} | php | public function add(string $id_option = null, array $cfg): ?string
{
return (
($id_option = $this->_get_id_option($id_option)) &&
$this->_insert($id_option, $cfg)
) ? $this->db->last_id() : null;
} | [
"public",
"function",
"add",
"(",
"string",
"$",
"id_option",
"=",
"null",
",",
"array",
"$",
"cfg",
")",
":",
"?",
"string",
"{",
"return",
"(",
"(",
"$",
"id_option",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_option",
")",
")",
"&&",
"$",
"this",
"->",
"_insert",
"(",
"$",
"id_option",
",",
"$",
"cfg",
")",
")",
"?",
"$",
"this",
"->",
"db",
"->",
"last_id",
"(",
")",
":",
"null",
";",
"}"
] | Adds a new preference for the given option for the current user.
@param null|string $id_option
@param array $cfg
@return null|string | [
"Adds",
"a",
"new",
"preference",
"for",
"the",
"given",
"option",
"for",
"the",
"current",
"user",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L703-L709 |
nabab/bbn | src/bbn/user/preferences.php | preferences.delete | public function delete($id): ?int
{
return $this->db->delete($this->class_cfg['table'], [$this->fields['id'] => $id]);
} | php | public function delete($id): ?int
{
return $this->db->delete($this->class_cfg['table'], [$this->fields['id'] => $id]);
} | [
"public",
"function",
"delete",
"(",
"$",
"id",
")",
":",
"?",
"int",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"delete",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}"
] | Deletes the given permission
@param $id
@return int|null | [
"Deletes",
"the",
"given",
"permission"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L717-L720 |
nabab/bbn | src/bbn/user/preferences.php | preferences.delete_user_option | public function delete_user_option(string $id_option, string $id_user = null): ?int
{
if ( $id_option = $this->_get_id_option($id_option) ){
return $this->db->delete($this->class_cfg['table'], [
$this->fields['id_option'] => $id_option,
$this->fields['id_user'] => $id_user ?: $this->id_user
]);
}
return null;
} | php | public function delete_user_option(string $id_option, string $id_user = null): ?int
{
if ( $id_option = $this->_get_id_option($id_option) ){
return $this->db->delete($this->class_cfg['table'], [
$this->fields['id_option'] => $id_option,
$this->fields['id_user'] => $id_user ?: $this->id_user
]);
}
return null;
} | [
"public",
"function",
"delete_user_option",
"(",
"string",
"$",
"id_option",
",",
"string",
"$",
"id_user",
"=",
"null",
")",
":",
"?",
"int",
"{",
"if",
"(",
"$",
"id_option",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_option",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"delete",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"=>",
"$",
"id_option",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"=>",
"$",
"id_user",
"?",
":",
"$",
"this",
"->",
"id_user",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Deletes all the given or current user's permissions for the given option
@param null|string $id_option
@param null|string $id_user
@return null|int | [
"Deletes",
"all",
"the",
"given",
"or",
"current",
"user",
"s",
"permissions",
"for",
"the",
"given",
"option"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L729-L738 |
nabab/bbn | src/bbn/user/preferences.php | preferences.delete_group_option | public function delete_group_option(string $id_option, string $id_group): ?int
{
if ( $id_option = $this->_get_id_option($id_option) ){
return $this->db->delete($this->class_cfg['table'], [
$this->fields['id_option'] => $id_option,
$this->fields['id_group'] => $id_group
]);
}
return null;
} | php | public function delete_group_option(string $id_option, string $id_group): ?int
{
if ( $id_option = $this->_get_id_option($id_option) ){
return $this->db->delete($this->class_cfg['table'], [
$this->fields['id_option'] => $id_option,
$this->fields['id_group'] => $id_group
]);
}
return null;
} | [
"public",
"function",
"delete_group_option",
"(",
"string",
"$",
"id_option",
",",
"string",
"$",
"id_group",
")",
":",
"?",
"int",
"{",
"if",
"(",
"$",
"id_option",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_option",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"delete",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"=>",
"$",
"id_option",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_group'",
"]",
"=>",
"$",
"id_group",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Deletes all the given group's permissions for the given option
@param null|string $id_option
@param string $id_group
@return int|null | [
"Deletes",
"all",
"the",
"given",
"group",
"s",
"permissions",
"for",
"the",
"given",
"option"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L747-L756 |
nabab/bbn | src/bbn/user/preferences.php | preferences.set_cfg | public function set_cfg(string $id = null, array $cfg = null): int
{
if ( null !== $cfg ){
$cfg = $this->get_cfg(null, $cfg);
$config = json_encode($cfg);
}
else{
$config = null;
}
return $this->db->update($this->class_cfg['table'], [
$this->fields['cfg'] => $config
], [
$this->fields['id'] => $id
]);
} | php | public function set_cfg(string $id = null, array $cfg = null): int
{
if ( null !== $cfg ){
$cfg = $this->get_cfg(null, $cfg);
$config = json_encode($cfg);
}
else{
$config = null;
}
return $this->db->update($this->class_cfg['table'], [
$this->fields['cfg'] => $config
], [
$this->fields['id'] => $id
]);
} | [
"public",
"function",
"set_cfg",
"(",
"string",
"$",
"id",
"=",
"null",
",",
"array",
"$",
"cfg",
"=",
"null",
")",
":",
"int",
"{",
"if",
"(",
"null",
"!==",
"$",
"cfg",
")",
"{",
"$",
"cfg",
"=",
"$",
"this",
"->",
"get_cfg",
"(",
"null",
",",
"$",
"cfg",
")",
";",
"$",
"config",
"=",
"json_encode",
"(",
"$",
"cfg",
")",
";",
"}",
"else",
"{",
"$",
"config",
"=",
"null",
";",
"}",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'cfg'",
"]",
"=>",
"$",
"config",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}"
] | Sets (or unsets) the cfg field of a given preference based on its ID
@param string $id
@param null|array $cfg
@return int | [
"Sets",
"(",
"or",
"unsets",
")",
"the",
"cfg",
"field",
"of",
"a",
"given",
"preference",
"based",
"on",
"its",
"ID"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L765-L779 |
nabab/bbn | src/bbn/user/preferences.php | preferences.set_text | public function set_text(string $id, string $text = null): ?int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['text'] => $text
], [
$this->fields['id'] => $id
]);
} | php | public function set_text(string $id, string $text = null): ?int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['text'] => $text
], [
$this->fields['id'] => $id
]);
} | [
"public",
"function",
"set_text",
"(",
"string",
"$",
"id",
",",
"string",
"$",
"text",
"=",
"null",
")",
":",
"?",
"int",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'text'",
"]",
"=>",
"$",
"text",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}"
] | Sets (or unsets) the text field of the given preference and returns the result of the executed query
@param string $id
@param null|string $text
@return null|int | [
"Sets",
"(",
"or",
"unsets",
")",
"the",
"text",
"field",
"of",
"the",
"given",
"preference",
"and",
"returns",
"the",
"result",
"of",
"the",
"executed",
"query"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L788-L795 |
nabab/bbn | src/bbn/user/preferences.php | preferences.set_link | public function set_link(string $id, string $id_link = null): ?int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['id_link'] => $id_link
], [
$this->fields['id'] => $id
]);
} | php | public function set_link(string $id, string $id_link = null): ?int
{
return $this->db->update($this->class_cfg['table'], [
$this->fields['id_link'] => $id_link
], [
$this->fields['id'] => $id
]);
} | [
"public",
"function",
"set_link",
"(",
"string",
"$",
"id",
",",
"string",
"$",
"id_link",
"=",
"null",
")",
":",
"?",
"int",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"=>",
"$",
"id_link",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}"
] | Sets (or unsets) the id_link field of the given preference and returns the result of the executed query
@param string $id
@param string $id_link
@return null|int | [
"Sets",
"(",
"or",
"unsets",
")",
"the",
"id_link",
"field",
"of",
"the",
"given",
"preference",
"and",
"returns",
"the",
"result",
"of",
"the",
"executed",
"query"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L804-L811 |
nabab/bbn | src/bbn/user/preferences.php | preferences.add_link | public function add_link(string $id_option, string $id_link): ?string
{
$id = $this->db->select_one($this->class_cfg['table'], $this->fields['id'], [
$this->fields['id_user'] => $this->id_user,
$this->fields['id_option'] => $id_option
]);
if ( $id ){
if ( $this->db->update($this->class_cfg['table'], [
$this->fields['id_link'] => $id_link
], ['id' => $id]) ){
return $id;
}
}
else if ( $this->db->insert($this->class_cfg['table'], [
$this->fields['id_user'] => $this->id_user,
$this->fields['id_option'] => $id_option,
$this->fields['id_link'] => $id_link
]) ){
return $this->db->last_id();
}
return null;
} | php | public function add_link(string $id_option, string $id_link): ?string
{
$id = $this->db->select_one($this->class_cfg['table'], $this->fields['id'], [
$this->fields['id_user'] => $this->id_user,
$this->fields['id_option'] => $id_option
]);
if ( $id ){
if ( $this->db->update($this->class_cfg['table'], [
$this->fields['id_link'] => $id_link
], ['id' => $id]) ){
return $id;
}
}
else if ( $this->db->insert($this->class_cfg['table'], [
$this->fields['id_user'] => $this->id_user,
$this->fields['id_option'] => $id_option,
$this->fields['id_link'] => $id_link
]) ){
return $this->db->last_id();
}
return null;
} | [
"public",
"function",
"add_link",
"(",
"string",
"$",
"id_option",
",",
"string",
"$",
"id_link",
")",
":",
"?",
"string",
"{",
"$",
"id",
"=",
"$",
"this",
"->",
"db",
"->",
"select_one",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"=>",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"=>",
"$",
"id_option",
"]",
")",
";",
"if",
"(",
"$",
"id",
")",
"{",
"if",
"(",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"=>",
"$",
"id_link",
"]",
",",
"[",
"'id'",
"=>",
"$",
"id",
"]",
")",
")",
"{",
"return",
"$",
"id",
";",
"}",
"}",
"else",
"if",
"(",
"$",
"this",
"->",
"db",
"->",
"insert",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'table'",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
"=>",
"$",
"this",
"->",
"id_user",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_option'",
"]",
"=>",
"$",
"id_option",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_link'",
"]",
"=>",
"$",
"id_link",
"]",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"last_id",
"(",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Sets (or unsets) the id_link field of the given preference and returns the result of the executed query
@param string $id_option
@param string $id_link
@return null|string The inserted or updated preference's ID | [
"Sets",
"(",
"or",
"unsets",
")",
"the",
"id_link",
"field",
"of",
"the",
"given",
"preference",
"and",
"returns",
"the",
"result",
"of",
"the",
"executed",
"query"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L820-L841 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_shared | public function get_shared(string $id): ?array
{
if ( bbn\str::is_uid($id) ){
return $this->db->rselect_all($this->class_table, [
$this->fields['id'],
$this->fields['id_user'],
$this->fields['id_group']
], [
$this->fields['id_alias'] => $id
]);
}
return null;
} | php | public function get_shared(string $id): ?array
{
if ( bbn\str::is_uid($id) ){
return $this->db->rselect_all($this->class_table, [
$this->fields['id'],
$this->fields['id_user'],
$this->fields['id_group']
], [
$this->fields['id_alias'] => $id
]);
}
return null;
} | [
"public",
"function",
"get_shared",
"(",
"string",
"$",
"id",
")",
":",
"?",
"array",
"{",
"if",
"(",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"rselect_all",
"(",
"$",
"this",
"->",
"class_table",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_user'",
"]",
",",
"$",
"this",
"->",
"fields",
"[",
"'id_group'",
"]",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id_alias'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Returns an array
@param string $id
@return array|null | [
"Returns",
"an",
"array"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L849-L861 |
nabab/bbn | src/bbn/user/preferences.php | preferences.make_public | public function make_public(string $id, bool $cancel = false): ?int
{
if ( $cfg = $this->get($id) ){
return $this->db->update($this->class_table, ['public' => $cancel ? 0 : 1], [
$this->fields['id'] => $id
]);
}
return null;
} | php | public function make_public(string $id, bool $cancel = false): ?int
{
if ( $cfg = $this->get($id) ){
return $this->db->update($this->class_table, ['public' => $cancel ? 0 : 1], [
$this->fields['id'] => $id
]);
}
return null;
} | [
"public",
"function",
"make_public",
"(",
"string",
"$",
"id",
",",
"bool",
"$",
"cancel",
"=",
"false",
")",
":",
"?",
"int",
"{",
"if",
"(",
"$",
"cfg",
"=",
"$",
"this",
"->",
"get",
"(",
"$",
"id",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_table",
",",
"[",
"'public'",
"=>",
"$",
"cancel",
"?",
"0",
":",
"1",
"]",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Makes (or unmakes) the given preference public.
@param string $id
@param bool $cancel
@return int|null | [
"Makes",
"(",
"or",
"unmakes",
")",
"the",
"given",
"preference",
"public",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L870-L878 |
nabab/bbn | src/bbn/user/preferences.php | preferences.share_with_group | public function share_with_group(string $id, string $id_group, bool $cancel = false): ?int
{
if ( $cfg = $this->get($id) ){
$id_share = $this->db->select_one($this->class_table, $this->fields['id'], [
'id_alias' => $id,
'id_group' => $id_group
]);
if ( $cancel && $id_share ){
return $this->db->delete($this->class_table, [$this->fields['id'] => $id_share]);
}
else if ( !$cancel && !$id_share ){
return $this->db->insert($this->class_table, [
'id_option' => $cfg['id_option'],
'id_alias' => $id,
'id_group' => $id_group
]);
}
return 0;
}
return null;
} | php | public function share_with_group(string $id, string $id_group, bool $cancel = false): ?int
{
if ( $cfg = $this->get($id) ){
$id_share = $this->db->select_one($this->class_table, $this->fields['id'], [
'id_alias' => $id,
'id_group' => $id_group
]);
if ( $cancel && $id_share ){
return $this->db->delete($this->class_table, [$this->fields['id'] => $id_share]);
}
else if ( !$cancel && !$id_share ){
return $this->db->insert($this->class_table, [
'id_option' => $cfg['id_option'],
'id_alias' => $id,
'id_group' => $id_group
]);
}
return 0;
}
return null;
} | [
"public",
"function",
"share_with_group",
"(",
"string",
"$",
"id",
",",
"string",
"$",
"id_group",
",",
"bool",
"$",
"cancel",
"=",
"false",
")",
":",
"?",
"int",
"{",
"if",
"(",
"$",
"cfg",
"=",
"$",
"this",
"->",
"get",
"(",
"$",
"id",
")",
")",
"{",
"$",
"id_share",
"=",
"$",
"this",
"->",
"db",
"->",
"select_one",
"(",
"$",
"this",
"->",
"class_table",
",",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
",",
"[",
"'id_alias'",
"=>",
"$",
"id",
",",
"'id_group'",
"=>",
"$",
"id_group",
"]",
")",
";",
"if",
"(",
"$",
"cancel",
"&&",
"$",
"id_share",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"delete",
"(",
"$",
"this",
"->",
"class_table",
",",
"[",
"$",
"this",
"->",
"fields",
"[",
"'id'",
"]",
"=>",
"$",
"id_share",
"]",
")",
";",
"}",
"else",
"if",
"(",
"!",
"$",
"cancel",
"&&",
"!",
"$",
"id_share",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"insert",
"(",
"$",
"this",
"->",
"class_table",
",",
"[",
"'id_option'",
"=>",
"$",
"cfg",
"[",
"'id_option'",
"]",
",",
"'id_alias'",
"=>",
"$",
"id",
",",
"'id_group'",
"=>",
"$",
"id_group",
"]",
")",
";",
"}",
"return",
"0",
";",
"}",
"return",
"null",
";",
"}"
] | Shares (or unshares) the given preference to the given group.
@param string $id
@param string $id_group
@param bool $cancel
@return int|null | [
"Shares",
"(",
"or",
"unshares",
")",
"the",
"given",
"preference",
"to",
"the",
"given",
"group",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L888-L909 |
nabab/bbn | src/bbn/user/preferences.php | preferences.add_bit | public function add_bit(string $id_usr_opt, array $cfg): ?string
{
if (
($id_usr_opt = $this->_get_id_option($id_usr_opt)) &&
($c = $this->class_cfg['arch']['user_options_bits'])
){
$to_cfg = $this->get_bit_cfg(null, $cfg);
if ( isset($to_cfg['items']) ){
unset($to_cfg['items']);
}
if ( !empty($to_cfg) ){
if ( !empty($cfg[$c['cfg']]) ){
if ( \bbn\str::is_json($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = json_decode($cfg[$c['cfg']], true);
}
if ( \is_array($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = array_merge($cfg[$c['cfg']], $to_cfg);
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
$cfg[$c['cfg']] = json_encode($cfg[$c['cfg']]);
}
if ( $this->db->insert($this->class_cfg['tables']['user_options_bits'], [
$c['id_user_option'] => $id_usr_opt,
$c['id_parent'] => $cfg[$c['id_parent']] ?? NULL,
$c['id_option'] => $cfg[$c['id_option']] ?? NULL,
$c['num'] => $cfg[$c['num']] ?? NULL,
$c['text'] => $cfg[$c['text']] ?? '',
$c['cfg'] => $cfg[$c['cfg']] ?? '',
]) ){
return $this->db->last_id();
}
}
return null;
} | php | public function add_bit(string $id_usr_opt, array $cfg): ?string
{
if (
($id_usr_opt = $this->_get_id_option($id_usr_opt)) &&
($c = $this->class_cfg['arch']['user_options_bits'])
){
$to_cfg = $this->get_bit_cfg(null, $cfg);
if ( isset($to_cfg['items']) ){
unset($to_cfg['items']);
}
if ( !empty($to_cfg) ){
if ( !empty($cfg[$c['cfg']]) ){
if ( \bbn\str::is_json($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = json_decode($cfg[$c['cfg']], true);
}
if ( \is_array($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = array_merge($cfg[$c['cfg']], $to_cfg);
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
$cfg[$c['cfg']] = json_encode($cfg[$c['cfg']]);
}
if ( $this->db->insert($this->class_cfg['tables']['user_options_bits'], [
$c['id_user_option'] => $id_usr_opt,
$c['id_parent'] => $cfg[$c['id_parent']] ?? NULL,
$c['id_option'] => $cfg[$c['id_option']] ?? NULL,
$c['num'] => $cfg[$c['num']] ?? NULL,
$c['text'] => $cfg[$c['text']] ?? '',
$c['cfg'] => $cfg[$c['cfg']] ?? '',
]) ){
return $this->db->last_id();
}
}
return null;
} | [
"public",
"function",
"add_bit",
"(",
"string",
"$",
"id_usr_opt",
",",
"array",
"$",
"cfg",
")",
":",
"?",
"string",
"{",
"if",
"(",
"(",
"$",
"id_usr_opt",
"=",
"$",
"this",
"->",
"_get_id_option",
"(",
"$",
"id_usr_opt",
")",
")",
"&&",
"(",
"$",
"c",
"=",
"$",
"this",
"->",
"class_cfg",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
")",
")",
"{",
"$",
"to_cfg",
"=",
"$",
"this",
"->",
"get_bit_cfg",
"(",
"null",
",",
"$",
"cfg",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"to_cfg",
"[",
"'items'",
"]",
")",
")",
"{",
"unset",
"(",
"$",
"to_cfg",
"[",
"'items'",
"]",
")",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"to_cfg",
")",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
")",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_json",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
")",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"json_decode",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
",",
"true",
")",
";",
"}",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
")",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"array_merge",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
",",
"$",
"to_cfg",
")",
";",
"}",
"else",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"$",
"to_cfg",
";",
"}",
"}",
"else",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"$",
"to_cfg",
";",
"}",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"json_encode",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
";",
"}",
"if",
"(",
"$",
"this",
"->",
"db",
"->",
"insert",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"[",
"$",
"c",
"[",
"'id_user_option'",
"]",
"=>",
"$",
"id_usr_opt",
",",
"$",
"c",
"[",
"'id_parent'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'id_parent'",
"]",
"]",
"??",
"NULL",
",",
"$",
"c",
"[",
"'id_option'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'id_option'",
"]",
"]",
"??",
"NULL",
",",
"$",
"c",
"[",
"'num'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'num'",
"]",
"]",
"??",
"NULL",
",",
"$",
"c",
"[",
"'text'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'text'",
"]",
"]",
"??",
"''",
",",
"$",
"c",
"[",
"'cfg'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"??",
"''",
",",
"]",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"last_id",
"(",
")",
";",
"}",
"}",
"return",
"null",
";",
"}"
] | Adds a bit to a preference
@param string $id_usr_opt The preference's ID
@param array $cfg The bit's values
@return string|null | [
"Adds",
"a",
"bit",
"to",
"a",
"preference"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L948-L987 |
nabab/bbn | src/bbn/user/preferences.php | preferences.delete_bit | public function delete_bit(string $id): ?int
{
if ( \bbn\str::is_uid($id) ){
return $this->db->delete($this->class_cfg['tables']['user_options_bits'], [
$this->class_cfg['arch']['user_options_bits']['id'] => $id
]);
}
return null;
} | php | public function delete_bit(string $id): ?int
{
if ( \bbn\str::is_uid($id) ){
return $this->db->delete($this->class_cfg['tables']['user_options_bits'], [
$this->class_cfg['arch']['user_options_bits']['id'] => $id
]);
}
return null;
} | [
"public",
"function",
"delete_bit",
"(",
"string",
"$",
"id",
")",
":",
"?",
"int",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
")",
"{",
"return",
"$",
"this",
"->",
"db",
"->",
"delete",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"[",
"$",
"this",
"->",
"class_cfg",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Deletes a preference's bit
@param string The bit's ID
@return int|null | [
"Deletes",
"a",
"preference",
"s",
"bit"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L996-L1004 |
nabab/bbn | src/bbn/user/preferences.php | preferences.update_bit | public function update_bit(string $id, array $cfg, $merge_config = false): ?int
{
if ( \bbn\str::is_uid($id) ){
$c = $this->class_cfg['arch']['user_options_bits'];
$fields = array_values($c);
if ( \array_key_exists($c['id'], $cfg) ){
unset($cfg[$c['id']]);
}
$to_cfg = $this->get_bit_cfg(null, $cfg);
if ( isset($to_cfg['items']) ){
unset($to_cfg['items']);
}
if ( !empty($to_cfg) ){
if ( !empty($merge_config) && !empty($cfg[$c['cfg']]) ){
if ( \bbn\str::is_json($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = json_decode($cfg[$c['cfg']], true);
}
if ( \is_array($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = array_merge($cfg[$c['cfg']], $to_cfg);
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
$cfg[$c['cfg']] = json_encode($cfg[$c['cfg']]);
}
return $this->db->update($this->class_cfg['tables']['user_options_bits'], [
$c['id_parent'] => $cfg[$c['id_parent']] ?? NULL,
$c['id_option'] => $cfg[$c['id_option']] ?? NULL,
$c['num'] => $cfg[$c['num']] ?? NULL,
$c['text'] => $cfg[$c['text']] ?? '',
$c['cfg'] => $cfg[$c['cfg']] ?? '',
], [
$c['id'] => $id
]);
}
return null;
} | php | public function update_bit(string $id, array $cfg, $merge_config = false): ?int
{
if ( \bbn\str::is_uid($id) ){
$c = $this->class_cfg['arch']['user_options_bits'];
$fields = array_values($c);
if ( \array_key_exists($c['id'], $cfg) ){
unset($cfg[$c['id']]);
}
$to_cfg = $this->get_bit_cfg(null, $cfg);
if ( isset($to_cfg['items']) ){
unset($to_cfg['items']);
}
if ( !empty($to_cfg) ){
if ( !empty($merge_config) && !empty($cfg[$c['cfg']]) ){
if ( \bbn\str::is_json($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = json_decode($cfg[$c['cfg']], true);
}
if ( \is_array($cfg[$c['cfg']]) ){
$cfg[$c['cfg']] = array_merge($cfg[$c['cfg']], $to_cfg);
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
}
else {
$cfg[$c['cfg']] = $to_cfg;
}
$cfg[$c['cfg']] = json_encode($cfg[$c['cfg']]);
}
return $this->db->update($this->class_cfg['tables']['user_options_bits'], [
$c['id_parent'] => $cfg[$c['id_parent']] ?? NULL,
$c['id_option'] => $cfg[$c['id_option']] ?? NULL,
$c['num'] => $cfg[$c['num']] ?? NULL,
$c['text'] => $cfg[$c['text']] ?? '',
$c['cfg'] => $cfg[$c['cfg']] ?? '',
], [
$c['id'] => $id
]);
}
return null;
} | [
"public",
"function",
"update_bit",
"(",
"string",
"$",
"id",
",",
"array",
"$",
"cfg",
",",
"$",
"merge_config",
"=",
"false",
")",
":",
"?",
"int",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
")",
"{",
"$",
"c",
"=",
"$",
"this",
"->",
"class_cfg",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
";",
"$",
"fields",
"=",
"array_values",
"(",
"$",
"c",
")",
";",
"if",
"(",
"\\",
"array_key_exists",
"(",
"$",
"c",
"[",
"'id'",
"]",
",",
"$",
"cfg",
")",
")",
"{",
"unset",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'id'",
"]",
"]",
")",
";",
"}",
"$",
"to_cfg",
"=",
"$",
"this",
"->",
"get_bit_cfg",
"(",
"null",
",",
"$",
"cfg",
")",
";",
"if",
"(",
"isset",
"(",
"$",
"to_cfg",
"[",
"'items'",
"]",
")",
")",
"{",
"unset",
"(",
"$",
"to_cfg",
"[",
"'items'",
"]",
")",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"to_cfg",
")",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"merge_config",
")",
"&&",
"!",
"empty",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
")",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_json",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
")",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"json_decode",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
",",
"true",
")",
";",
"}",
"if",
"(",
"\\",
"is_array",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
")",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"array_merge",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
",",
"$",
"to_cfg",
")",
";",
"}",
"else",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"$",
"to_cfg",
";",
"}",
"}",
"else",
"{",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"$",
"to_cfg",
";",
"}",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"=",
"json_encode",
"(",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
")",
";",
"}",
"return",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"[",
"$",
"c",
"[",
"'id_parent'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'id_parent'",
"]",
"]",
"??",
"NULL",
",",
"$",
"c",
"[",
"'id_option'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'id_option'",
"]",
"]",
"??",
"NULL",
",",
"$",
"c",
"[",
"'num'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'num'",
"]",
"]",
"??",
"NULL",
",",
"$",
"c",
"[",
"'text'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'text'",
"]",
"]",
"??",
"''",
",",
"$",
"c",
"[",
"'cfg'",
"]",
"=>",
"$",
"cfg",
"[",
"$",
"c",
"[",
"'cfg'",
"]",
"]",
"??",
"''",
",",
"]",
",",
"[",
"$",
"c",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
";",
"}",
"return",
"null",
";",
"}"
] | Updates a preference's bit
@param string $id The bit's ID
@param array The bit's values
@return int|null | [
"Updates",
"a",
"preference",
"s",
"bit"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L1013-L1053 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_bit | public function get_bit(string $id, bool $with_config = true): array
{
if (
\bbn\str::is_uid($id) &&
($bit = $this->db->rselect($this->class_cfg['tables']['user_options_bits'], [], [
$this->class_cfg['arch']['user_options_bits']['id'] => $id
]))
){
if ( !empty($with_config) ){
return $this->explode_bit_cfg($bit);
}
return $bit;
}
return [];
} | php | public function get_bit(string $id, bool $with_config = true): array
{
if (
\bbn\str::is_uid($id) &&
($bit = $this->db->rselect($this->class_cfg['tables']['user_options_bits'], [], [
$this->class_cfg['arch']['user_options_bits']['id'] => $id
]))
){
if ( !empty($with_config) ){
return $this->explode_bit_cfg($bit);
}
return $bit;
}
return [];
} | [
"public",
"function",
"get_bit",
"(",
"string",
"$",
"id",
",",
"bool",
"$",
"with_config",
"=",
"true",
")",
":",
"array",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
"&&",
"(",
"$",
"bit",
"=",
"$",
"this",
"->",
"db",
"->",
"rselect",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"[",
"]",
",",
"[",
"$",
"this",
"->",
"class_cfg",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
"[",
"'id'",
"]",
"=>",
"$",
"id",
"]",
")",
")",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"with_config",
")",
")",
"{",
"return",
"$",
"this",
"->",
"explode_bit_cfg",
"(",
"$",
"bit",
")",
";",
"}",
"return",
"$",
"bit",
";",
"}",
"return",
"[",
"]",
";",
"}"
] | Returns a single preference's bit
@param string $id The bit's ID
@return array | [
"Returns",
"a",
"single",
"preference",
"s",
"bit"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L1061-L1075 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_bits | public function get_bits(string $id_usr_opt, $id_parent = false, bool $with_config = true): array
{
$c = $this->class_cfg['arch']['user_options_bits'];
$t = $this;
$where = [
$c['id_user_option'] => $id_usr_opt
];
if ( is_null($id_parent) || \bbn\str::is_uid($id_parent) ){
$where[$c['id_parent']] = $id_parent;
}
if (
\bbn\str::is_uid($id_usr_opt) &&
($bits = $this->db->rselect_all($this->class_cfg['tables']['user_options_bits'], [], $where, [$c['num'] => 'ASC']))
){
if ( !empty($with_config) ){
return array_map(function($b) use($t){
return $t->explode_bit_cfg($b);
}, $bits);
}
return $bits;
}
return [];
} | php | public function get_bits(string $id_usr_opt, $id_parent = false, bool $with_config = true): array
{
$c = $this->class_cfg['arch']['user_options_bits'];
$t = $this;
$where = [
$c['id_user_option'] => $id_usr_opt
];
if ( is_null($id_parent) || \bbn\str::is_uid($id_parent) ){
$where[$c['id_parent']] = $id_parent;
}
if (
\bbn\str::is_uid($id_usr_opt) &&
($bits = $this->db->rselect_all($this->class_cfg['tables']['user_options_bits'], [], $where, [$c['num'] => 'ASC']))
){
if ( !empty($with_config) ){
return array_map(function($b) use($t){
return $t->explode_bit_cfg($b);
}, $bits);
}
return $bits;
}
return [];
} | [
"public",
"function",
"get_bits",
"(",
"string",
"$",
"id_usr_opt",
",",
"$",
"id_parent",
"=",
"false",
",",
"bool",
"$",
"with_config",
"=",
"true",
")",
":",
"array",
"{",
"$",
"c",
"=",
"$",
"this",
"->",
"class_cfg",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
";",
"$",
"t",
"=",
"$",
"this",
";",
"$",
"where",
"=",
"[",
"$",
"c",
"[",
"'id_user_option'",
"]",
"=>",
"$",
"id_usr_opt",
"]",
";",
"if",
"(",
"is_null",
"(",
"$",
"id_parent",
")",
"||",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id_parent",
")",
")",
"{",
"$",
"where",
"[",
"$",
"c",
"[",
"'id_parent'",
"]",
"]",
"=",
"$",
"id_parent",
";",
"}",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id_usr_opt",
")",
"&&",
"(",
"$",
"bits",
"=",
"$",
"this",
"->",
"db",
"->",
"rselect_all",
"(",
"$",
"this",
"->",
"class_cfg",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"[",
"]",
",",
"$",
"where",
",",
"[",
"$",
"c",
"[",
"'num'",
"]",
"=>",
"'ASC'",
"]",
")",
")",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"with_config",
")",
")",
"{",
"return",
"array_map",
"(",
"function",
"(",
"$",
"b",
")",
"use",
"(",
"$",
"t",
")",
"{",
"return",
"$",
"t",
"->",
"explode_bit_cfg",
"(",
"$",
"b",
")",
";",
"}",
",",
"$",
"bits",
")",
";",
"}",
"return",
"$",
"bits",
";",
"}",
"return",
"[",
"]",
";",
"}"
] | Returns the bits list of a preference
@param string $id The preference's ID
@param null|string $id_parent The bits'parent ID
@return array | [
"Returns",
"the",
"bits",
"list",
"of",
"a",
"preference"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L1084-L1106 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_full_bits | public function get_full_bits(string $id_usr_opt, string $id_parent = null, bool $with_config = true): array
{
if ( \bbn\str::is_uid($id_usr_opt) ){
$c = $this->class_cfg['arch']['user_options_bits'];
$t = $this;
return array_map(function($b) use($t, $c, $id_usr_opt, $with_config){
if ( !empty($with_config) ){
$b = $t->explode_bit_cfg($b);
}
$b['items'] = $t->get_full_bits($id_usr_opt, $b[$c['id']], $with_config);
return $b;
}, $this->db->rselect_all([
'table' => $this->class_cfg['tables']['user_options_bits'],
'fields' => [],
'where' => [
'conditions' => [[
'field' => $c['id_user_option'],
'value' => $id_usr_opt
], [
'field' => $c['id_parent'],
empty($id_parent) ? 'operator' : 'value' => $id_parent ?: 'isnull'
]]
],
'order' => [$c['num'] => 'ASC']
]));
}
return [];
} | php | public function get_full_bits(string $id_usr_opt, string $id_parent = null, bool $with_config = true): array
{
if ( \bbn\str::is_uid($id_usr_opt) ){
$c = $this->class_cfg['arch']['user_options_bits'];
$t = $this;
return array_map(function($b) use($t, $c, $id_usr_opt, $with_config){
if ( !empty($with_config) ){
$b = $t->explode_bit_cfg($b);
}
$b['items'] = $t->get_full_bits($id_usr_opt, $b[$c['id']], $with_config);
return $b;
}, $this->db->rselect_all([
'table' => $this->class_cfg['tables']['user_options_bits'],
'fields' => [],
'where' => [
'conditions' => [[
'field' => $c['id_user_option'],
'value' => $id_usr_opt
], [
'field' => $c['id_parent'],
empty($id_parent) ? 'operator' : 'value' => $id_parent ?: 'isnull'
]]
],
'order' => [$c['num'] => 'ASC']
]));
}
return [];
} | [
"public",
"function",
"get_full_bits",
"(",
"string",
"$",
"id_usr_opt",
",",
"string",
"$",
"id_parent",
"=",
"null",
",",
"bool",
"$",
"with_config",
"=",
"true",
")",
":",
"array",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id_usr_opt",
")",
")",
"{",
"$",
"c",
"=",
"$",
"this",
"->",
"class_cfg",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
";",
"$",
"t",
"=",
"$",
"this",
";",
"return",
"array_map",
"(",
"function",
"(",
"$",
"b",
")",
"use",
"(",
"$",
"t",
",",
"$",
"c",
",",
"$",
"id_usr_opt",
",",
"$",
"with_config",
")",
"{",
"if",
"(",
"!",
"empty",
"(",
"$",
"with_config",
")",
")",
"{",
"$",
"b",
"=",
"$",
"t",
"->",
"explode_bit_cfg",
"(",
"$",
"b",
")",
";",
"}",
"$",
"b",
"[",
"'items'",
"]",
"=",
"$",
"t",
"->",
"get_full_bits",
"(",
"$",
"id_usr_opt",
",",
"$",
"b",
"[",
"$",
"c",
"[",
"'id'",
"]",
"]",
",",
"$",
"with_config",
")",
";",
"return",
"$",
"b",
";",
"}",
",",
"$",
"this",
"->",
"db",
"->",
"rselect_all",
"(",
"[",
"'table'",
"=>",
"$",
"this",
"->",
"class_cfg",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"'fields'",
"=>",
"[",
"]",
",",
"'where'",
"=>",
"[",
"'conditions'",
"=>",
"[",
"[",
"'field'",
"=>",
"$",
"c",
"[",
"'id_user_option'",
"]",
",",
"'value'",
"=>",
"$",
"id_usr_opt",
"]",
",",
"[",
"'field'",
"=>",
"$",
"c",
"[",
"'id_parent'",
"]",
",",
"empty",
"(",
"$",
"id_parent",
")",
"?",
"'operator'",
":",
"'value'",
"=>",
"$",
"id_parent",
"?",
":",
"'isnull'",
"]",
"]",
"]",
",",
"'order'",
"=>",
"[",
"$",
"c",
"[",
"'num'",
"]",
"=>",
"'ASC'",
"]",
"]",
")",
")",
";",
"}",
"return",
"[",
"]",
";",
"}"
] | Returns the hierarchical bits list of a preference
@param string $id_usr_opt The preference's ID
@param string $id_parent The parent's ID of a bit. Default: null
@param bool $with_config Set it to false if you don't want the preference's cfg field values on the results.
@return array | [
"Returns",
"the",
"hierarchical",
"bits",
"list",
"of",
"a",
"preference"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L1116-L1143 |
nabab/bbn | src/bbn/user/preferences.php | preferences.get_tree | public function get_tree(string $id, bool $with_config = true): array
{
if (
\bbn\str::is_uid($id) &&
($p = $this->get($id, $with_config))
){
$p['items'] = $this->get_full_bits($id, null, $with_config);
return $p;
}
return [];
} | php | public function get_tree(string $id, bool $with_config = true): array
{
if (
\bbn\str::is_uid($id) &&
($p = $this->get($id, $with_config))
){
$p['items'] = $this->get_full_bits($id, null, $with_config);
return $p;
}
return [];
} | [
"public",
"function",
"get_tree",
"(",
"string",
"$",
"id",
",",
"bool",
"$",
"with_config",
"=",
"true",
")",
":",
"array",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
"&&",
"(",
"$",
"p",
"=",
"$",
"this",
"->",
"get",
"(",
"$",
"id",
",",
"$",
"with_config",
")",
")",
")",
"{",
"$",
"p",
"[",
"'items'",
"]",
"=",
"$",
"this",
"->",
"get_full_bits",
"(",
"$",
"id",
",",
"null",
",",
"$",
"with_config",
")",
";",
"return",
"$",
"p",
";",
"}",
"return",
"[",
"]",
";",
"}"
] | Returns a preference and its hierarchical bits list
@param string $id The preference's ID
@param bool $with_config Set it to false if you don't want the preference's cfg field values on the results. | [
"Returns",
"a",
"preference",
"and",
"its",
"hierarchical",
"bits",
"list"
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L1151-L1161 |
nabab/bbn | src/bbn/user/preferences.php | preferences.order_bit | public function order_bit(string $id, int $pos): ?bool
{
if (
\bbn\str::is_uid($id) &&
($cf = $this->get_class_cfg()) &&
($cfg = $cf['arch']['user_options_bits']) &&
($bit = $this->get_bit($id)) &&
($old = (int)$bit[$cfg['num']]) &&
!empty($pos) &&
($old !== $pos) &&
($bits = $this->get_bits($bit[$cfg['id_user_option']], $bit[$cfg['id_parent']] ?: false))
){
$past_new = false;
$past_old = false;
$p = 1;
$changed = 0;
foreach ( $bits as $ele ){
$upd = [];
if ( $past_old && !$past_new ){
$upd[$cfg['num']] = $p-1;
}
else if ( !$past_old && $past_new ){
$upd[$cfg['num']] = $p+1;
}
if ( $id === $ele['id'] ){
$upd[$cfg['num']] = $pos;
$past_old = 1;
}
else if ( $p === $pos ){
$upd[$cfg['num']] = $p + ($pos > $old ? -1 : 1);
$past_new = 1;
}
if ( !empty($upd) ){
$changed += $this->db->update($cf['tables']['user_options_bits'], $upd, [$cfg['id'] => $ele['id']]);
}
if ( $past_new && $past_old ){
break;
}
$p++;
}
return !!$changed;
}
return null;
} | php | public function order_bit(string $id, int $pos): ?bool
{
if (
\bbn\str::is_uid($id) &&
($cf = $this->get_class_cfg()) &&
($cfg = $cf['arch']['user_options_bits']) &&
($bit = $this->get_bit($id)) &&
($old = (int)$bit[$cfg['num']]) &&
!empty($pos) &&
($old !== $pos) &&
($bits = $this->get_bits($bit[$cfg['id_user_option']], $bit[$cfg['id_parent']] ?: false))
){
$past_new = false;
$past_old = false;
$p = 1;
$changed = 0;
foreach ( $bits as $ele ){
$upd = [];
if ( $past_old && !$past_new ){
$upd[$cfg['num']] = $p-1;
}
else if ( !$past_old && $past_new ){
$upd[$cfg['num']] = $p+1;
}
if ( $id === $ele['id'] ){
$upd[$cfg['num']] = $pos;
$past_old = 1;
}
else if ( $p === $pos ){
$upd[$cfg['num']] = $p + ($pos > $old ? -1 : 1);
$past_new = 1;
}
if ( !empty($upd) ){
$changed += $this->db->update($cf['tables']['user_options_bits'], $upd, [$cfg['id'] => $ele['id']]);
}
if ( $past_new && $past_old ){
break;
}
$p++;
}
return !!$changed;
}
return null;
} | [
"public",
"function",
"order_bit",
"(",
"string",
"$",
"id",
",",
"int",
"$",
"pos",
")",
":",
"?",
"bool",
"{",
"if",
"(",
"\\",
"bbn",
"\\",
"str",
"::",
"is_uid",
"(",
"$",
"id",
")",
"&&",
"(",
"$",
"cf",
"=",
"$",
"this",
"->",
"get_class_cfg",
"(",
")",
")",
"&&",
"(",
"$",
"cfg",
"=",
"$",
"cf",
"[",
"'arch'",
"]",
"[",
"'user_options_bits'",
"]",
")",
"&&",
"(",
"$",
"bit",
"=",
"$",
"this",
"->",
"get_bit",
"(",
"$",
"id",
")",
")",
"&&",
"(",
"$",
"old",
"=",
"(",
"int",
")",
"$",
"bit",
"[",
"$",
"cfg",
"[",
"'num'",
"]",
"]",
")",
"&&",
"!",
"empty",
"(",
"$",
"pos",
")",
"&&",
"(",
"$",
"old",
"!==",
"$",
"pos",
")",
"&&",
"(",
"$",
"bits",
"=",
"$",
"this",
"->",
"get_bits",
"(",
"$",
"bit",
"[",
"$",
"cfg",
"[",
"'id_user_option'",
"]",
"]",
",",
"$",
"bit",
"[",
"$",
"cfg",
"[",
"'id_parent'",
"]",
"]",
"?",
":",
"false",
")",
")",
")",
"{",
"$",
"past_new",
"=",
"false",
";",
"$",
"past_old",
"=",
"false",
";",
"$",
"p",
"=",
"1",
";",
"$",
"changed",
"=",
"0",
";",
"foreach",
"(",
"$",
"bits",
"as",
"$",
"ele",
")",
"{",
"$",
"upd",
"=",
"[",
"]",
";",
"if",
"(",
"$",
"past_old",
"&&",
"!",
"$",
"past_new",
")",
"{",
"$",
"upd",
"[",
"$",
"cfg",
"[",
"'num'",
"]",
"]",
"=",
"$",
"p",
"-",
"1",
";",
"}",
"else",
"if",
"(",
"!",
"$",
"past_old",
"&&",
"$",
"past_new",
")",
"{",
"$",
"upd",
"[",
"$",
"cfg",
"[",
"'num'",
"]",
"]",
"=",
"$",
"p",
"+",
"1",
";",
"}",
"if",
"(",
"$",
"id",
"===",
"$",
"ele",
"[",
"'id'",
"]",
")",
"{",
"$",
"upd",
"[",
"$",
"cfg",
"[",
"'num'",
"]",
"]",
"=",
"$",
"pos",
";",
"$",
"past_old",
"=",
"1",
";",
"}",
"else",
"if",
"(",
"$",
"p",
"===",
"$",
"pos",
")",
"{",
"$",
"upd",
"[",
"$",
"cfg",
"[",
"'num'",
"]",
"]",
"=",
"$",
"p",
"+",
"(",
"$",
"pos",
">",
"$",
"old",
"?",
"-",
"1",
":",
"1",
")",
";",
"$",
"past_new",
"=",
"1",
";",
"}",
"if",
"(",
"!",
"empty",
"(",
"$",
"upd",
")",
")",
"{",
"$",
"changed",
"+=",
"$",
"this",
"->",
"db",
"->",
"update",
"(",
"$",
"cf",
"[",
"'tables'",
"]",
"[",
"'user_options_bits'",
"]",
",",
"$",
"upd",
",",
"[",
"$",
"cfg",
"[",
"'id'",
"]",
"=>",
"$",
"ele",
"[",
"'id'",
"]",
"]",
")",
";",
"}",
"if",
"(",
"$",
"past_new",
"&&",
"$",
"past_old",
")",
"{",
"break",
";",
"}",
"$",
"p",
"++",
";",
"}",
"return",
"!",
"!",
"$",
"changed",
";",
"}",
"return",
"null",
";",
"}"
] | Orders a bit.
@param string $id The bit's ID
@param int $pos The new position
@return bool|null | [
"Orders",
"a",
"bit",
"."
] | train | https://github.com/nabab/bbn/blob/439fea2faa0de22fdaae2611833bab8061f40c37/src/bbn/user/preferences.php#L1237-L1280 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.