<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\model\concern;
use InvalidArgumentException;
use think\db\Raw;
use think\helper\Str;
use think\model\Relation;
trait Attribute
{
protected $pk = 'id';
protected $schema = [];
protected $field = [];
protected $type = [];
protected $disuse = [];
protected $readonly = [];
private $data = [];
private $origin = [];
protected $json = [];
protected $jsonType = [];
protected $jsonAssoc = false;
protected $strict = true;
private $set = [];
private $withAttr = [];
public function getPk()
{
return $this->pk;
}
protected function isPk(string $key): bool
{
$pk = $this->getPk();
if (is_string($pk) && $pk == $key) {
return true;
} elseif (is_array($pk) && in_array($key, $pk)) {
return true;
}
return false;
}
public function getKey()
{
$pk = $this->getPk();
if (is_string($pk) && array_key_exists($pk, $this->data)) {
return $this->data[$pk];
}
return;
}
public function allowField(array $field)
{
$this->field = $field;
return $this;
}
public function readOnly(array $field)
{
$this->readonly = $field;
return $this;
}
protected function getRealFieldName(string $name): string
{
return $this->strict ? $name : Str::snake($name);
}
public function data(array $data, bool $set = false, array $allow = [])
{
$this->data = [];
foreach ($this->disuse as $key) {
if (array_key_exists($key, $data)) {
unset($data[$key]);
}
}
if (!empty($allow)) {
$result = [];
foreach ($allow as $name) {
if (isset($data[$name])) {
$result[$name] = $data[$name];
}
}
$data = $result;
}
if ($set) {
$this->setAttrs($data);
} else {
$this->data = $data;
}
return $this;
}
public function appendData(array $data, bool $set = false)
{
if ($set) {
$this->setAttrs($data);
} else {
$this->data = array_merge($this->data, $data);
}
return $this;
}
public function getOrigin(string $name = null)
{
if (is_null($name)) {
return $this->origin;
}
return array_key_exists($name, $this->origin) ? $this->origin[$name] : null;
}
public function getData(string $name = null)
{
if (is_null($name)) {
return $this->data;
}
$fieldName = $this->getRealFieldName($name);
if (array_key_exists($fieldName, $this->data)) {
return $this->data[$fieldName];
} elseif (array_key_exists($fieldName, $this->relation)) {
return $this->relation[$fieldName];
}
throw new InvalidArgumentException('property not exists:' . static::class . '->' . $name);
}
public function getChangedData(): array
{
$data = $this->force ? $this->data : array_udiff_assoc($this->data, $this->origin, function ($a, $b) {
if ((empty($a) || empty($b)) && $a !== $b) {
return 1;
}
return is_object($a) || $a != $b ? 1 : 0;
});
foreach ($this->readonly as $key => $field) {
if (isset($data[$field])) {
unset($data[$field]);
}
}
return $data;
}
public function set(string $name, $value): void
{
$name = $this->getRealFieldName($name);
$this->data[$name] = $value;
}
public function setAttrs(array $data): void
{
foreach ($data as $key => $value) {
$this->setAttr($key, $value, $data);
}
}
public function setAttr(string $name, $value, array $data = []): void
{
$name = $this->getRealFieldName($name);
if (isset($this->set[$name])) {
return;
}
if (is_null($value) && $this->autoWriteTimestamp && in_array($name, [$this->createTime, $this->updateTime])) {
$value = $this->autoWriteTimestamp();
} else {
$method = 'set' . Str::studly($name) . 'Attr';
if (method_exists($this, $method)) {
$array = $this->data;
$value = $this->$method($value, array_merge($this->data, $data));
$this->set[$name] = true;
if (is_null($value) && $array !== $this->data) {
return;
}
} elseif (isset($this->type[$name])) {
$value = $this->writeTransform($value, $this->type[$name]);
}
}
$this->data[$name] = $value;
}
protected function writeTransform($value, $type)
{
if (is_null($value)) {
return;
}
if ($value instanceof Raw) {
return $value;
}
if (is_array($type)) {
[$type, $param] = $type;
} elseif (strpos($type, ':')) {
[$type, $param] = explode(':', $type, 2);
}
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
if (empty($param)) {
$value = (float) $value;
} else {
$value = (float) number_format($value, (int) $param, '.', '');
}
break;
case 'boolean':
$value = (bool) $value;
break;
case 'timestamp':
if (!is_numeric($value)) {
$value = strtotime($value);
}
break;
case 'datetime':
$value = is_numeric($value) ? $value : strtotime($value);
$value = $this->formatDateTime('Y-m-d H:i:s.u', $value, true);
break;
case 'object':
if (is_object($value)) {
$value = json_encode($value, JSON_FORCE_OBJECT);
}
break;
case 'array':
$value = (array) $value;
case 'json':
$option = !empty($param) ? (int) $param : JSON_UNESCAPED_UNICODE;
$value = json_encode($value, $option);
break;
case 'serialize':
$value = serialize($value);
break;
default:
if (is_object($value) && false !== strpos($type, '\\') && method_exists($value, '__toString')) {
$value = $value->__toString();
}
}
return $value;
}
public function getAttr(string $name)
{
try {
$relation = false;
$value = $this->getData($name);
} catch (InvalidArgumentException $e) {
$relation = $this->isRelationAttr($name);
$value = null;
}
return $this->getValue($name, $value, $relation);
}
protected function getValue(string $name, $value, $relation = false)
{
$fieldName = $this->getRealFieldName($name);
$method = 'get' . Str::studly($name) . 'Attr';
if (isset($this->withAttr[$fieldName])) {
if ($relation) {
$value = $this->getRelationValue($relation);
}
if (in_array($fieldName, $this->json) && is_array($this->withAttr[$fieldName])) {
$value = $this->getJsonValue($fieldName, $value);
} else {
$closure = $this->withAttr[$fieldName];
$value = $closure($value, $this->data);
}
} elseif (method_exists($this, $method)) {
if ($relation) {
$value = $this->getRelationValue($relation);
}
$value = $this->$method($value, $this->data);
} elseif (isset($this->type[$fieldName])) {
$value = $this->readTransform($value, $this->type[$fieldName]);
} elseif ($this->autoWriteTimestamp && in_array($fieldName, [$this->createTime, $this->updateTime])) {
$value = $this->getTimestampValue($value);
} elseif ($relation) {
$value = $this->getRelationValue($relation);
$this->relation[$name] = $value;
}
return $value;
}
protected function getJsonValue($name, $value)
{
foreach ($this->withAttr[$name] as $key => $closure) {
if ($this->jsonAssoc) {
$value[$key] = $closure($value[$key], $value);
} else {
$value->$key = $closure($value->$key, $value);
}
}
return $value;
}
protected function getRelationValue(string $relation)
{
$modelRelation = $this->$relation();
return $modelRelation instanceof Relation ? $this->getRelationData($modelRelation) : null;
}
protected function readTransform($value, $type)
{
if (is_null($value)) {
return;
}
if (is_array($type)) {
[$type, $param] = $type;
} elseif (strpos($type, ':')) {
[$type, $param] = explode(':', $type, 2);
}
switch ($type) {
case 'integer':
$value = (int) $value;
break;
case 'float':
if (empty($param)) {
$value = (float) $value;
} else {
$value = (float) number_format($value, (int) $param, '.', '');
}
break;
case 'boolean':
$value = (bool) $value;
break;
case 'timestamp':
if (!is_null($value)) {
$format = !empty($param) ? $param : $this->dateFormat;
$value = $this->formatDateTime($format, $value, true);
}
break;
case 'datetime':
if (!is_null($value)) {
$format = !empty($param) ? $param : $this->dateFormat;
$value = $this->formatDateTime($format, $value);
}
break;
case 'json':
$value = json_decode($value, true);
break;
case 'array':
$value = empty($value) ? [] : json_decode($value, true);
break;
case 'object':
$value = empty($value) ? new \stdClass() : json_decode($value);
break;
case 'serialize':
try {
$value = unserialize($value);
} catch (\Exception $e) {
$value = null;
}
break;
default:
if (false !== strpos($type, '\\')) {
$value = new $type($value);
}
}
return $value;
}
public function withAttribute($name, callable $callback = null)
{
if (is_array($name)) {
foreach ($name as $key => $val) {
$this->withAttribute($key, $val);
}
} else {
$name = $this->getRealFieldName($name);
if (strpos($name, '.')) {
[$name, $key] = explode('.', $name);
$this->withAttr[$name][$key] = $callback;
} else {
$this->withAttr[$name] = $callback;
}
}
return $this;
}
}