代码初始化

This commit is contained in:
2025-08-07 20:21:47 +08:00
commit 50f3a2dbb0
2191 changed files with 374790 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
<?php
declare(strict_types=1);
namespace Yansongda\Supports\Traits;
use Yansongda\Supports\Str;
trait Accessable
{
/**
* __get.
*
* @return mixed
*/
public function __get(string $key)
{
return $this->get($key);
}
/**
* Whether or not an data exists by key.
*/
public function __isset(string $key): bool
{
return !is_null($this->get($key));
}
/**
* Unsets an data by key.
*/
public function __unset(string $key)
{
$this->offsetUnset($key);
}
/**
* __set.
*
* @param mixed $value
*/
public function __set(string $key, $value): void
{
$this->set($key, $value);
}
/**
* get.
*
* @param mixed $default
*
* @return mixed
*/
public function get(?string $key = null, $default = null)
{
if (is_null($key)) {
return method_exists($this, 'toArray') ? $this->toArray() : $default;
}
$method = 'get'.Str::studly($key);
if (method_exists($this, $method)) {
return $this->{$method}();
}
return $default;
}
/**
* set.
*
* @param mixed $value
*/
public function set(string $key, $value): self
{
$method = 'set'.Str::studly($key);
if (method_exists($this, $method)) {
$this->{$method}($value);
}
return $this;
}
/**
* Whether a offset exists.
*
* @see https://php.net/manual/en/arrayaccess.offsetexists.php
*
* @param mixed $offset an offset to check for
*
* @return bool true on success or false on failure.
*
* The return value will be casted to boolean if non-boolean was returned.
*/
#[\ReturnTypeWillChange]
public function offsetExists($offset)
{
return !is_null($this->get($offset));
}
/**
* Offset to retrieve.
*
* @see https://php.net/manual/en/arrayaccess.offsetget.php
*
* @param mixed $offset the offset to retrieve
*
* @return mixed can return all value types
*/
#[\ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
}
/**
* Offset to set.
*
* @see https://php.net/manual/en/arrayaccess.offsetset.php
*
* @param mixed $offset the offset to assign the value to
* @param mixed $value the value to set
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetSet($offset, $value)
{
$this->set($offset, $value);
}
/**
* Offset to unset.
*
* @see https://php.net/manual/en/arrayaccess.offsetunset.php
*
* @param mixed $offset the offset to unset
*
* @return void
*/
#[\ReturnTypeWillChange]
public function offsetUnset($offset)
{
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Yansongda\Supports\Traits;
use ReflectionClass;
use Yansongda\Supports\Str;
trait Arrayable
{
/**
* toArray.
*/
public function toArray(): array
{
$result = [];
foreach ((new ReflectionClass($this))->getProperties() as $item) {
$k = $item->getName();
$method = 'get'.Str::studly($k);
$result[Str::snake($k)] = method_exists($this, $method) ? $this->{$method}() : $this->{$k};
}
return $result;
}
}

View File

@@ -0,0 +1,65 @@
<?php
declare(strict_types=1);
namespace Yansongda\Supports\Traits;
trait Serializable
{
public function __serialize(): array
{
if (method_exists($this, 'toArray')) {
return $this->toArray();
}
return [];
}
public function __unserialize(array $data): void
{
$this->unserializeArray($data);
}
public function __toString(): string
{
return $this->toJson();
}
public function serialize(): ?string
{
return serialize($this);
}
public function unserialize($data): void
{
unserialize($data);
}
/**
* toJson.
*/
public function toJson(int $option = JSON_UNESCAPED_UNICODE): string
{
return json_encode($this->__serialize(), $option);
}
/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->__serialize();
}
public function unserializeArray(array $data): self
{
foreach ($data as $key => $item) {
if (method_exists($this, 'set')) {
$this->set($key, $item);
}
}
return $this;
}
}