Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
develop
Branches (7)
Tags (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
develop
Branches (7)
Tags (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
develop
Branches (7)
Tags (81)
develop
gh-pages
4.8
master
feature/mailer
refactor/email
feature/queue
v4.7.3
v4.7.2
v4.7.1
v4.7.0
v4.6.5
v4.6.4
v4.6.3
v4.6.2
v4.6.1
v4.6.0
v4.5.8
v4.5.7
v4.5.6
v4.5.5
v4.5.4
v4.5.3
v4.5.2
v4.5.1
v4.5.0
v4.4.8
CodeIgniter4
/
system
/
API
/
BaseTransformer.php
CodeIgniter4
/
system
/
API
/
BaseTransformer.php
BaseTransformer.php 7.13 KB
Copy Edit Raw Blame History
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
<?php
declare(strict_types=1);
/**
* This file is part of CodeIgniter 4 framework.
*
* (c) CodeIgniter Foundation <admin@codeigniter.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace CodeIgniter\API;
use CodeIgniter\HTTP\IncomingRequest;
use InvalidArgumentException;
/**
* Base class for transforming resources into arrays.
* Fulfills common functionality of the TransformerInterface,
* and provides helper methods for conditional inclusion/exclusion of values.
*
* Supports the following query variables from the request:
* - fields: Comma-separated list of fields to include in the response
* (e.g., ?fields=id,name,email)
* If not provided, all fields from toArray() are included.
* - include: Comma-separated list of related resources to include
* (e.g., ?include=posts,comments)
* This looks for methods named `include{Resource}()` on the transformer,
* and calls them to get the related data, which are added as a new key to the output.
*
* Example:
*
* class UserTransformer extends BaseTransformer
* {
* public function toArray(mixed $resource): array
* {
* return [
* 'id' => $resource['id'],
* 'name' => $resource['name'],
* 'email' => $resource['email'],
* 'created_at' => $resource['created_at'],
* 'updated_at' => $resource['updated_at'],
* ];
* }
*
* protected function includePosts(): array
* {
* $posts = model('PostModel')->where('user_id', $this->resource['id'])->findAll();
* return (new PostTransformer())->transformMany($posts);
* }
* }
*/
abstract class BaseTransformer implements TransformerInterface
{
/**
* Nesting depth of the transformation currently in progress (0 = root).
*/
private static int $depth = 0;
/**
* @var list<string>|null
*/
private ?array $fields = null;
/**
* @var list<string>|null
*/
private ?array $includes = null;
protected mixed $resource = null;
public function __construct(
private ?IncomingRequest $request = null,
) {
// An explicitly provided request is always honored - its scope is
// intentional. Only the implicit global fallback is suppressed for
// nested transformers, which is the actual leak vector.
$explicitRequest = $request instanceof IncomingRequest;
$this->request = $request ?? request();
if ($explicitRequest || self::$depth === 0) {
$fields = $this->request->getGet('fields');
$this->fields = is_string($fields)
? array_map(trim(...), explode(',', $fields))
: $fields;
$includes = $this->request->getGet('include');
$this->includes = is_string($includes)
? array_map(trim(...), explode(',', $includes))
: $includes;
}
}
/**
* Converts the resource to an array representation.
* This is overridden by child classes to define the
* API-safe resource representation.
*
* @param mixed $resource The resource being transformed
*/
abstract public function toArray(mixed $resource): array;
/**
* Transforms the given resource into an array using
* the $this->toArray().
*/
public function transform(array|object|null $resource = null): array
{
// Store the resource so include methods can access it
$this->resource = $resource;
if ($resource === null) {
$data = $this->toArray(null);
} elseif (is_object($resource) && method_exists($resource, 'toArray')) {
$data = $this->toArray($resource->toArray());
} else {
$data = $this->toArray((array) $resource);
}
$data = $this->limitFields($data);
return $this->insertIncludes($data);
}
/**
* Transforms a collection of resources using $this->transform() on each item.
*
* If the request's 'fields' query variable is set, only those fields will be included
* in the transformed output.
*/
public function transformMany(array $resources): array
{
return array_map($this->transform(...), $resources);
}
/**
* Define which fields can be requested via the 'fields' query parameter.
* Override in child classes to restrict available fields.
* Return null to allow all fields from toArray().
*
* @return list<string>|null
*/
protected function getAllowedFields(): ?array
{
return null;
}
/**
* Define which related resources can be included via the 'include' query parameter.
* Override in child classes to restrict available includes.
* Return null to allow all includes that have corresponding methods.
* Return an empty array to disable all includes.
*
* @return list<string>|null
*/
protected function getAllowedIncludes(): ?array
{
return null;
}
/**
* Limits the given data array to only the fields specified
*
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*
* @throws InvalidArgumentException
*/
private function limitFields(array $data): array
{
if ($this->fields === null || $this->fields === []) {
return $data;
}
$allowedFields = $this->getAllowedFields();
// If whitelist is defined, validate against it
if ($allowedFields !== null) {
$invalidFields = array_diff($this->fields, $allowedFields);
if ($invalidFields !== []) {
throw ApiException::forInvalidFields(implode(', ', $invalidFields));
}
}
return array_intersect_key($data, array_flip($this->fields));
}
/**
* Checks the request for 'include' query variable, and if present,
* calls the corresponding include{Resource} methods to add related data.
*
* @param array<string, mixed> $data
*
* @return array<string, mixed>
*/
private function insertIncludes(array $data): array
{
if ($this->includes === null) {
return $data;
}
$allowedIncludes = $this->getAllowedIncludes();
if ($allowedIncludes === []) {
return $data; // No includes allowed
}
// If whitelist is defined, filter the requested includes
if ($allowedIncludes !== null) {
$invalidIncludes = array_diff($this->includes, $allowedIncludes);
if ($invalidIncludes !== []) {
throw ApiException::forInvalidIncludes(implode(', ', $invalidIncludes));
}
}
self::$depth++;
try {
foreach ($this->includes as $include) {
$method = 'include' . ucfirst($include);
if (method_exists($this, $method)) {
$data[$include] = $this->{$method}();
} else {
throw ApiException::forMissingInclude($include);
}
}
} finally {
self::$depth--;
}
return $data;
}
}
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wfdaj/CodeIgniter4.git
git@gitee.com:wfdaj/CodeIgniter4.git
wfdaj
CodeIgniter4
CodeIgniter4
develop
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /