Skip to content

Commit

Permalink
2.5 version
Browse files Browse the repository at this point in the history
  • Loading branch information
akbarali1 committed Apr 2, 2024
1 parent 66fc940 commit b8e2c2d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -203,4 +203,8 @@ $object = ClientData::arrayToClassProperty([
]);
```

Return string: `public readonly int $id;public string $full_name;`
Return string: `public readonly int $id;public string $full_name;`

# 2.5 version

`createProperty` method added to create a property return string and bug fix `arrayToClassProperty`
32 changes: 22 additions & 10 deletions src/DataObjectBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,39 +206,52 @@ public function all(bool $trim_nulls = false): array
return $this->toArray($trim_nulls);
}

public static function arrayToClassProperty(array $array): string
public static function arrayToClassProperty(array $array, bool $camelCase = false): string
{
$string = '';
foreach ($array as $key => $value) {
$type = is_int($value) ? 'int' : (is_float($value) ? 'float' : (is_string($value) ? 'string' : '?string'));
$type = match (gettype($value)) {
'integer' => 'int',
'double' => 'float',
'string' => 'string',
'array' => 'array',
default => '?string',
};
if (str_contains($key, 'id')) {
$type = 'readonly int';
}
$string .= 'public '.$type.' $'.$key.';';
$key = $camelCase ? Str::camel($key) : $key;
$string .= 'public '.$type.' $'.$key.';'.PHP_EOL;
}

return $string;
}

public static function createProperty(mixed $model)
/**
* @param mixed $model
* @param bool $camelCase
* @return string
* @throws DataObjectException
*/
public static function createProperty(mixed $model, bool $camelCase = false): string
{
if (is_array($model)) {
return self::arrayToClassProperty($model);
return self::arrayToClassProperty($model, $camelCase);
}

if ($model instanceof Model) {
return self::arrayToClassProperty($model->toArray());
return self::arrayToClassProperty($model->toArray(), $camelCase);
}

if (is_object($model) && method_exists($model, 'first') && method_exists($model, 'toArray')) {
return self::arrayToClassProperty($model->first()->toArray());
return self::arrayToClassProperty($model->first()->toArray(), $camelCase);
}

if (is_object($model) && method_exists($model, 'toArray')) {
return self::arrayToClassProperty($model->toArray());
return self::arrayToClassProperty($model->toArray(), $camelCase);
}

throw new \Exception('Invalid model type');
throw new DataObjectException('Invalid model type', DataObjectException::INVALID_MODEL_TYPE);
}

/*public static function savePropertyToFile(mixed $model): string
Expand All @@ -250,7 +263,6 @@ public static function createProperty(mixed $model)
$content = str_replace('}', $allProperties."\n}", $content);
fwrite($file, $content);
fclose($file);
return 'Properties added successfully';
}*/
}
23 changes: 23 additions & 0 deletions src/DataObjectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace Akbarali\DataObject;

use Throwable;

class DataObjectException extends \Exception
{
/**
* OperationException constructor.
* @param string $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(string $message = "", int $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}

public const INVALID_MODEL_TYPE = -1000;

}

0 comments on commit b8e2c2d

Please sign in to comment.