2016-07-18
@used-by responseF()
@used-by responseL()
@uses df_first()
@uses df_last()
@param string[] ...$k
@return Ev|string|null
3682
private function response(...$k) {
		$ev = dfc($this, function($f) {return
 			call_user_func($f, $this->responses())
		;}, [dfa(['L' => 'df_last', 'F' => 'df_first'], substr(df_caller_f(), -1))]); /** @var Ev|null $ev */
		return/** @var int $c */!($c = count($k)) || !$ev ? $ev : $ev->r(1 < $c ? $k : df_first($k));
	}
652491
public function getKey($offset = null)
    {
        if (null === $offset) {
            $offset = $this->offset;
        }

        if ((null !== ($op = $this->getId(--$offset)))
            && ((DocLexer::T_COLON === $op)
                || (DocLexer::T_EQUALS === $op))) {
            return $this->getValue(--$offset);
        }

        return null;
    }
835944
private function json($k = null, $s = null) {return df_eta(@df_json_decode($this->v(
		$k ?: df_caller_f(), $s
	)));}
1078232
static function clean($k = null, $d = null) {return dfak(function() {return
		dfa_unset(df_request(), self::extraKeysRaw())
	;}, $k, $d);}
412729
public static function getLast() {
		
		$class = get_called_class();

		// check about single primary key
		if ($class::hasCompoundKey()) {
			return NULL;
		}
		
		// check if auto-increment key
		$db = Database::getInstance();
		if (!$db->isAutoIncrement($class::TABLE_NAME)) {
			return NULL;
		}
		
		// cast to string
		$tableKey = (is_array($class::TABLE_KEY) and array_key_exists(0, $class::TABLE_KEY)) ? $class::TABLE_KEY[0] : $class::TABLE_KEY;

		$db->setQuery('SELECT * FROM `' . $class::TABLE_NAME . '` ORDER BY `' . $tableKey . '` DESC LIMIT 1');
		$obj = $db->loadObject();

		return (is_a($obj, 'stdClass') ? new $class($obj) : NULL);
		
	}
1201633
public static function firstOrNull($fields = null)
    {
        static $request;
        if (!$request) {
            $request = Di::getDefault()->getShared('request');
        }

        if (!$request->isAjax()) {
            return null;
        }

        $model = new static();
        return static::get($request->getId($model->getPrimaryKey()));
    }
517871
public function isSelected($k): bool
    {
        $res = (\is_array($this->selected) && \in_array($k, $this->selected, true)) || $k === $this->selected;

        return $res;
    }
856432
function max($key = null) {
        if($key !== null) {
            $data = \array_column($this->data, $key);
        } else {
            $data = $this->data;
        }
        
        return \max($data);
    }
981316
public function ViewBag($k=null, $v=null)
    {
		if($v !== null){
			$this->viewBag->$k = $v;
		}elseif($k !== null){
			return $this->viewBag->$k;
		}else{
			return $this->viewBag;
		}
		return $this;
	}
233284
public function load($key)
    {
        try {
            $sth = $this->pdo->prepare(
                'SELECT * FROM sessions WHERE id = :id'
            );

            $sth->bindValue(':id', $key, \PDO::PARAM_INT);
            $sth->execute();

            $session = $sth->fetchObject();

            if ($session instanceof \stdClass) {
                return array(
                    'id'            => $session->id,
                    'last_activity' => $session->last_activity,
                    'data'          => unserialize($session->data)
                );
            }

            return null;
        } catch (\PDOException $pdoe) {
            return null;
        }
    }
1195305
public static function set($k,$v=null) {
        if(is_array($k)) {
            return self::$o->config = array_merge(self::$o->config,$k);
        }
        return self::$o->config[$k]=$v;
    }
915879
public function getPkv()
    {
        $pk = $this->getPk();

        try {
            if (is_array($pk)) {
                $pkv = [];
                foreach ($pk as $pkItem) {
                    $val = $this->getAttrVal($pkItem);
                    $pkv[$pkItem] = $val;
                }
            } else {
                $pkv = $this->getAttrVal($pk);
            }
        } catch (\Exception $e) {
            return null;
        }

        return $pkv;
    }
604231
public function get($key)
        {
            if (Arrays::is($key)) {
                $result = [];

                foreach($key as $column) {
                    $result[$column] = isset($this->_data[$column]) ? $this->_data[$column] : null;
                }

                return $result;
            } else {
                return isset($this->_data[$key]) ? $this->_data[$key] : null;
            }
        }
183342
public function fetchPairs($key, $value = null){
		$clone = clone $this;

		// If executed w/o needed cols, force exec w/ them.
		if(!in_array('*', $clone->columns)){
			if($value !== null)
				$clone->columns = array($key, $value);
			elseif(!in_array($key, $clone->columns))
				$clone->columns[] = $key;
		}
		$k = substr($key, ($pk = strrpos($key, '.')) ? $pk+1 : 0);
		$v = $value === null ? null : substr($value, ($pv = strrpos($value, '.')) ? $pv+1 : 0);

		$rows = array();
		while($row = $clone->fetch()){
			if(!$row)
				return array();
			$rows[$row[$k]] = $value === null ? $row : $row->$v;
		}

		return $rows;
	}
1012958
final protected function resPath($k) {$this->addFilterResAV(function(array $a) use($k) {return
		dfa_deep($a, $k, $a)
	;});}
748924
public function returnOriginalData($key = null)
    {
        $this->_prepareData($key);

        $mergedData = array_merge($this->_DATA, $this->_originalDATA);
        $data       = $this->_removeNewKeys($mergedData);

        if (array_key_exists($key, $data)) {
            return $data[$key];
        }

        return null;
    }
1310617
static function calculate(S $s, $k = null, $kType = 'waitPeriodType') {
		$k = $k ?: df_caller_f(); /** @type string */
		return dfcf(function(S $s, $k, $kType) {
			$r = $s->nat($k); /** @var int $r */
			return self::$WORKING_DAYS === $s->v($kType) ? $r :
				df_num_calendar_days_by_num_working_days(ZD::now(), $r, $s->scope())
			;
		}, [$s, $k, $kType]);
	}