/** * @param null $path * @param null $default * * @return array|mixed */ static final function config($path = NULL, $default = NULL) { if (!static::$_config) { static::$_config = Kohana::$config->load('restfulapi')->as_array(); } return NULL !== $path ? Kohana_Arr::path(static::$_config, $path, $default) : static::$_config; }
protected static function config($path, $default = NULL, $delimeter = NULL) { if (!isset(static::$_configData)) { static::$_configData = Kohana::$config->load(static::$_configKey)->as_array(); } return Kohana_Arr::path(static::$_configData, $path, $default, $delimeter); }
public function __construct($errors, $message = NULL, $variables = NULL, $errno = NULL, array $debug = NULL, Exception $previous = NULL) { if (class_exists('ORM_Validation_Exception') && $errors instanceof ORM_Validation_Exception) { $errors = $errors->errors('model'); } elseif ($errors instanceof RestfulAPI_Validation) { $errors = $errors->errors(); } elseif ($errors instanceof Validation) { $errors = $errors->errors('validation'); } $this->_errors = Kohana_Arr::flatten($errors); parent::__construct(422, $message, $variables, $errno, $debug, $previous); }
/** * Retrieves muliple single-key values from a list of arrays. * * // Get all of the "id" values from a result * $ids = Arr::pluck($result, 'id'); * * [!!] A list of arrays is an array that contains arrays, eg: array(array $a, array $b, array $c, ...) * * @param array $array list of arrays to check * @param string $key key to pluck * @param bool $preserve preserve keys * @return array */ public static function pluck($array, $key, $preserve = FALSE) { $values = array(); if ($preserve) { $values = parent::pluck($array, $key); } else { foreach ($array as $k => $row) { if (isset($row[$key])) { $values[$k] = $row[$key]; } } } return $values; }
/** * @param null $key * @param null $default * @param null $removeKeyPrefix * * @return array|mixed */ public function data($key = NULL, $default = NULL, $removeKeyPrefix = NULL) { $data = parent::data(); if (NULL !== $key && !Kohana_Arr::is_array($key)) { return Kohana_Arr::get($data, $key, $default); } if (NULL === $key) { $key = array_keys($data); } $result = []; foreach ($key as $_key) { $newKey = NULL !== $removeKeyPrefix && strpos($_key, $removeKeyPrefix) === 0 ? substr($_key, strlen($removeKeyPrefix)) : $_key; $result[$newKey] = Kohana_Arr::get($data, $_key, $default); } return $result; }
/** * @param mixed $source * @throws \Exception */ public function __construct($source) { $this->source = $source; if ($source instanceof File) { $this->data = array('url' => site_url($source->path . '/' . $source->fullname), 'path' => realpath(APPPATH . '../' . $source->path . '/' . $source->fullname)); } elseif (is_array($source)) { $this->data = array('url' => \Kohana_Arr::getValidate($source, 'url', null, function ($val) { if (!is_string($val)) { throw new \Exception(__METHOD__ . ' url must be a string.'); } return true; }), 'path' => \Kohana_Arr::getValidate($source, 'path', null, function ($val) { if (!is_string($val)) { throw new \Exception(__METHOD__ . ' path must be a string.'); } return true; })); } else { throw new \Exception('Invalid source given. Expected array or \\File ' . gettype($source) . ' given.'); } }
/** * @param null $data * @param int $httpCode * @param null|array $headers * * @throws HTTP_Exception_Redirect */ static function json($data = NULL, $httpCode = 200, $headers = NULL) { if ($data instanceof HTTP_Exception_Redirect) { throw $data; } $response = Kohana_Response::factory(); try { $response->headers(['cache-control' => 'no-cache, no-store, max-age=0, must-revalidate', 'content-type' => 'application/json; charset=utf-8']); if (Kohana_Arr::is_array($headers)) { $response->headers($headers); } $response->status($httpCode); } catch (Exception $e) { $response->status($httpCode = 500); $data = $e; } if ($data instanceof Exception) { if ($data instanceof HTTP_Exception) { $response->status($httpCode = $data->getCode()); } elseif ($httpCode < 400) { $response->status($httpCode = 500); } $data = Helpers_Arr::exception($data); } if (NULL === $data && $httpCode == 200) { $response->status(204); } elseif (NULL !== $data) { try { $response->body(json_encode($data, JSON_UNESCAPED_UNICODE)); } catch (Exception $e) { $response->body(json_encode(Helpers_Arr::exception($e), JSON_UNESCAPED_UNICODE), 500); } } $response->send_headers(TRUE); exit($response); }
/** * Возвращает слово во множестевенном/единственном числе на основании $n * * @param int $n * @param string $one * @param null|string $two * @param null|string $five * * @return string */ public static function plural($n, $one, $two = NULL, $five = NULL) { if (is_array($one)) { $values = [Kohana_Arr::get($one, 0)]; $values[1] = Kohana_Arr::get($one, 1, $values[0]); $values[2] = Kohana_Arr::get($one, 2, $values[1]); } else { $values = [$one]; $values[1] = NULL !== $two ? $two : $one; $values[2] = NULL !== $five ? $five : $values[1]; } $idx = $n % 10 == 1 && $n % 100 != 11 ? 0 : ($n % 10 >= 2 && $n % 10 <= 4 && ($n % 100 < 10 || $n % 100 >= 20) ? 1 : 2); return $values[$idx]; }
function __construct($name, $data = NULL, $defaults = NULL) { $this->initial_value_is_set = FALSE; $this->defaults_is_set = FALSE; if (is_array($name)) { $data = $name; $name = Kohana_Arr::get($data, self::KEY_NAME); if (!$name) { throw new Kohana_Exception('Field :name not defined', [':name' => self::KEY_NAME]); } } $this->_name = $name; $this->_strict_hidden = TRUE; $this->setDefaults($defaults); $this->fillData($data); }
/** * @param string|array $key * @param null $default * @param null $castType * @param bool $nullable * * @return array|mixed * @throws InvalidArgumentException */ public static function getQuery($key, $default = NULL, $castType = NULL, $nullable = TRUE) { if (!is_scalar($key) && !is_array($key)) { throw new InvalidArgumentException('$key must be scalar or array'); } if (is_scalar($key)) { $result = static::_getQuery($key, $default, $castType, $nullable); } else { $isAssoc = Kohana_Arr::is_assoc($key); $result = []; foreach ($key as $keyName => $keyData) { $resultKey = $keyData; if ($isAssoc) { $queryKey = $keyName; } else { $queryKey = $keyData; } $result[$resultKey] = static::_getQuery($queryKey, $default, $castType, $nullable); } } return $result; }
/** * @param mixed $value * @param array $validationRules * @param string $file Validation errors file (default: validation) * * @return mixed * @throws Validation_Exception */ protected function validateValue($value, array $validationRules = NULL, $file = NULL) { $validationRules = Helpers_Arr::merge([['not_empty']], NULL === $validationRules ? [] : $validationRules); $validation = Validation::factory(['key' => $value])->rules('key', $validationRules); if (!$validation->check()) { if (NULL === $file) { $file = 'validation'; } $errors = Kohana_Arr::flatten($validation->errors($file)); throw new Validation_Exception($validation, Kohana_Arr::get($errors, 'key', 'Undefined validation error')); } return $value; }