Skip to content

Commit

Permalink
0.10.0
Browse files Browse the repository at this point in the history
- добавлен метод `setSmartyNativeOption()`, устанавливающий "родные" опции Smarty (для которых нет метода)
- теперь `assignJSON` записывает данные в поле data объекта `Result` (аналогичного Arris\Entity\Result, но включенного в код проекта напрямую, без зависимости от ядра микрофреймворка)
- теперь при рендере типа JSON вызвращается jsonized объект Result
- можно установить тип рендера JSON_RAW, в этом случае при рендере вернется не Result, а data-поле Result
  • Loading branch information
Karel Wintersky committed Mar 29, 2024
1 parent b45fe1d commit 39eef6f
Show file tree
Hide file tree
Showing 4 changed files with 406 additions and 9 deletions.
8 changes: 4 additions & 4 deletions src/Headers.php
Original file line number Diff line number Diff line change
Expand Up @@ -1334,12 +1334,12 @@ final class Headers implements HeadersInterface
*/
public array $current_headers;

public bool $must_send_headers;
public bool $need_send_headers;

public function __construct()
{
$this->current_headers = [];
$this->must_send_headers = true;
$this->need_send_headers = true;
}

/**
Expand All @@ -1364,7 +1364,7 @@ public function add(string $header_name = '', string $header_content = 'text/htm
'code' => $header_code
];

$this->must_send_headers = true;
$this->need_send_headers = true;

return $this;
}
Expand All @@ -1376,7 +1376,7 @@ public function add(string $header_name = '', string $header_content = 'text/htm
*/
public function send():bool
{
if (false === $this->must_send_headers) {
if (false === $this->need_send_headers) {
return false;
}

Expand Down
340 changes: 340 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,340 @@
<?php

namespace Arris\Template;

class Result implements \ArrayAccess, \Serializable
{
/**
* @var bool
*/
public bool $is_success = true;

/**
* @var bool
*/
public bool $is_error = false;

/**
* @var string
*/
public string $message = '';

/**
* @var string
*/
public string $code = '';

/**
* @var array
*/
public array $messages = [];

/**
* @var array
*/
public array $data = [];

public function __construct(bool $is_success = true)
{
$this->is_success = $is_success;
$this->is_error = !$is_success;
}

/**
* Устанавливаем произвольный property
*
* @param $key
* @param $value
* @return $this
*/
public function set($key, $value): Result
{
return $this->__set($key, $value);
}

/**
* Устанавливаем данные
*
* @param array $data
* @return $this
*/
public function setData(array $data = []): Result
{
foreach ($data as $k => $v) {
$this->__setData($k, $v);
}

return $this;
}

/**
* Устанавливает цифровой код (может использоваться как код ошибки)
*
* @param $code string|int
* @return $this
*/
public function setCode($code):Result
{
$this->code = $code;

return $this;
}

/**
* Устанавливаем единичное сообщение
*
* @param string $message
* @param ...$args
* @return $this
*/
public function setMessage(string $message = '', ...$args): Result
{
if (!empty($args[0])) {
$this->message = \vsprintf($message, $args[0]);
} else {
$this->message = $message;
}

/*if (func_num_args() > 1) {
$this->message = vsprintf($message, $args);
} else {
$this->message = $message;
}*/

return $this;
}

/**
* Добавляем сообщение к массиву сообщений
*
* @param string $message
* @param ...$args
* @return $this
*/
public function addMessage(string $message, ...$args)
{
if (func_num_args() > 1) {
$this->messages[] = \vsprintf($message, $args);
} else {
$this->messages[] = $message;
}

return $this;
}

/**
* Устанавливает признак: результат УСПЕШЕН
*
* @return $this
*/
public function success(string $message = '', ...$args): Result
{
$this->is_success = true;
$this->is_error = false;

$this->setMessage($message, $args);

return $this;
}

/**
* Устанавливает признак: результат ОШИБОЧЕН
*
* @return $this
*/
public function error(string $message = '', ...$args): Result
{
$this->is_success = false;
$this->is_error = true;

$this->setMessage($message, $args);

return $this;
}

/* === Getters === */

/**
* @return string
*/
public function getMessage():string
{
return $this->message;
}

/**
* @param bool $implode
* @param string $glue
* @param array $brackets
* @return array|string
*/
public function getMessages(bool $implode = false, string $glue = ',', array $brackets = ['[', ']'])
{
if ($implode === false) {
return $this->messages;
}

$imploded = \implode($glue, $this->messages);

if (!empty($brackets)) {
switch (\count($brackets)) {
case 0: {
$brackets[1] = $brackets[0] = '';
break;
}
case 1: {
$brackets[1] = $brackets[0];
break;
}
}

$imploded = $brackets[0] . $imploded . $brackets[1];
}

return $imploded;
}

/**
* Возвращает код
*
* @return string|int
*/
public function getCode()
{
return $this->code;
}

public function getData(): array
{
return $this->data;
}

public function getAll():array
{
return (array)$this;
}

/**
* Getter.
* Handles access to non-existing property
*
* ? если ключ не найден в списке property, то нужно проверить его в массиве $data и только потом вернуть null
*
* @param string $key
* @return null
*/
public function __get(string $key)
{
if (\property_exists($this, $key)) {
return $this->{$key};
} elseif (\array_key_exists($key, $this->data)) {
return $this->data[$key];
} else {
return null;
}
}

/**
* Setter
* Handles access to non-existing property
*
* ? если ключ не найден в списке property, то нужно добавить его в массив $data
*
* @param string $key
* @param $value
* @return self
*/
public function __set(string $key, $value = null): self
{
$this->{$key} = $value;

return $this;
}

public function __setData(string $key, $value = null): self
{
$this->data[$key] = $value;

return $this;
}

/**
* @param $offset
* @return bool
*/
public function offsetExists($offset): bool
{
return \property_exists($this, $offset) || \array_key_exists($offset, $this->data);
}

/**
* @todo: переделать?
*
* @param $offset
* @return mixed|null
*/
public function offsetGet($offset)
{
return $this->__get($offset);
}

/**
* @todo: переделать?
*
* @param $offset
* @param $value
* @return void
*/
public function offsetSet($offset, $value)
{
$this->{$offset} = $value;
}

/**
* @todo: переделать?
*
* @param $offset
* @return void
*/
public function offsetUnset($offset)
{
unset($this->{$offset});
}

/**
* Сериализатор
*
* @return false|string|null
*/
public function serialize()
{
return \json_encode([
'is_success' => $this->is_success,
'is_error' => $this->is_error,
'message' => $this->message,
'messages' => $this->messages,
'code' => $this->code,
'data' => $this->data
], JSON_HEX_APOS | JSON_HEX_QUOT | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE | JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_INVALID_UTF8_SUBSTITUTE /*| JSON_THROW_ON_ERROR*/);
}

/**
* Десериализатор
*
* @param $data
* @return $this
*/
public function unserialize($data): Result
{
$json = \json_decode($data, true);
$this->is_success = array_key_exists('is_success', $json) ? $json['is_success'] : true;
$this->is_error = array_key_exists('is_error', $json) ? $json['is_error'] : false;
$this->message = array_key_exists('message', $json) ? $json['message'] : '';
$this->code = array_key_exists('code', $json) ? $json['code'] : '';
$this->data = array_key_exists('data', $json) ? $json['data'] : '';
$this->messages = array_key_exists('messages', $json) ? $json['messages'] : '';
unset($json);

return $this;
}
}
Loading

0 comments on commit 39eef6f

Please sign in to comment.