Skip to content

Commit

Permalink
update model
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobmorais committed Nov 13, 2024
1 parent e918fcc commit 776ac68
Showing 1 changed file with 39 additions and 44 deletions.
83 changes: 39 additions & 44 deletions src/ModelAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#[\AllowDynamicProperties]
abstract class ModelAbstract
{
private array $dataModelArray = [];

/**
* @param array|null $params
*/
Expand All @@ -35,7 +33,7 @@ public function __construct(array $params = null)
*/
public function __get($name)
{
return $this->{$name} ?? ($this->dataModelArray[$name]?? "");
return property_exists($this, $name) ? $this->{$name} : null;
}

/**
Expand All @@ -45,10 +43,9 @@ public function __get($name)
*/
public function __set($key, $value): void
{
if (property_exists($this, $key)){
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
$this->dataModelArray[$key] = $value;
}

/**
Expand All @@ -57,26 +54,15 @@ public function __set($key, $value): void
*/
function __isset($key) {

$result = isset($this->dataModelArray[$key]);
if (property_exists($this, $key)){
$result = isset($this->{$key});
}

return $result;
}
return property_exists($this, $key) && isset($this->{$key});

/**
* @return array
*/
public function getDataModelArray():array {
return $this->dataModelArray;
}

/**
* @param array $params
* @return void
*/
public function fromMapToModel(array $params): void
public function fromMapToModel(array $params)
{
$reflection = new \ReflectionClass($this);

Expand All @@ -86,20 +72,20 @@ public function fromMapToModel(array $params): void
$property->setAccessible(true);
$property->setValue($this, $item);
}

$this->dataModelArray[$key] = $item;

}

return $this;
}

/**
* @param string $json
* @return void
*/
public function fromJsonToModel(string $json): void
public function fromJsonToModel(string $json)
{
$params = json_decode($json, true);
$this->fromMapToModel($params);
$params = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
return $this->fromMapToModel($params);

}

/**
Expand All @@ -112,8 +98,7 @@ public function toMap($objArray = null): ?array
if (is_array($data) || is_object($data)) {
$result = [];
foreach ($data as $key => $value) {
if(strlen($value) > 0)
$result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value;
$result[$key] = (is_array($value) || is_object($value)) ? $this->toMap($value) : $value;
}

return $result;
Expand All @@ -122,6 +107,9 @@ public function toMap($objArray = null): ?array
return null;
}

/**
* @return \stdClass
*/
public function toObject(): \stdClass{
$reflection = new \ReflectionClass($this);
$objeto = new \stdClass;
Expand All @@ -141,35 +129,42 @@ public function toObject(): \stdClass{
return $objeto;
}

/**
* @return array
*/
public function toArray(): array
{
$array = [];
foreach ((new \ReflectionClass($this))->getProperties() as $prop) {
$prop->setAccessible(true);
$method = 'get' . ucfirst($prop->getName());

$array[$prop->getName()] = method_exists($this, $method)
? $this->{$method}()
: $prop->getValue($this);
}
return $array;
}

/**
* @return string
*/
public function toJson(): string
{
return json_encode($this->toMap(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
return json_encode($this->toArray(), JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_THROW_ON_ERROR);
}

/**
* @return string
*/
public function toString(): string
{
$data = (object) $this->toMap();
$re_2 = new ReflectionObject($data);
$classname = get_class($this);
if ($pos = strrpos($classname, '\\')) {
$classname = substr($classname, $pos + 1);
}

return $classname . ' {' . implode(', ', array_map(
function ($p_0) use ($data) {
$p_0->setAccessible(true);

return $p_0->getName() . ': ' . $p_0->getValue($data);
},
$re_2->getProperties()
)) . '}';
$classname = (new \ReflectionClass($this))->getShortName();
$properties = array_map(
fn($prop) => "{$prop->getName()}: '" . ($prop->getValue($this) ?? '') . "'",
(new \ReflectionClass($this))->getProperties()
);
return "$classname {" . implode(', ', $properties) . '}';
}

}

}

0 comments on commit 776ac68

Please sign in to comment.