Refactored Model class.

This commit is contained in:
Dmitry Khomutov 2018-02-24 01:49:59 +07:00
parent be68f11aed
commit d0c69d2ef0
No known key found for this signature in database
GPG key ID: EC19426474B37AAC
10 changed files with 6 additions and 157 deletions

View file

@ -7,7 +7,6 @@ use Symfony\Component\Cache\Simple\ArrayCache;
class Model
{
public static $sleepable = [];
protected $getters = [];
protected $setters = [];
protected $data = [];
@ -35,82 +34,6 @@ class Model
return $this->tableName;
}
/**
* @param integer $depth
* @param integer $currentDepth
*
* @return array
*/
public function toArray($depth = 2, $currentDepth = 0)
{
if (isset(static::$sleepable) && is_array(static::$sleepable) && count(static::$sleepable)) {
$sleepable = static::$sleepable;
} else {
$sleepable = array_keys($this->getters);
}
$rtn = [];
foreach ($sleepable as $property) {
$rtn[$property] = $this->propertyToArray($property, $currentDepth, $depth);
}
return $rtn;
}
/**
* @param string $property
* @param integer $currentDepth
* @param integer $depth
*
* @return mixed
*/
protected function propertyToArray($property, $currentDepth, $depth)
{
$rtn = null;
if (array_key_exists($property, $this->getters)) {
$method = $this->getters[$property];
$rtn = $this->{$method}();
if (is_object($rtn) || is_array($rtn)) {
$rtn = ($depth > $currentDepth) ? $this->valueToArray($rtn, $currentDepth, $depth) : null;
}
}
return $rtn;
}
/**
* @param mixed $value
* @param integer $currentDepth
* @param integer $depth
*
* @return mixed
*/
protected function valueToArray($value, $currentDepth, $depth)
{
$rtn = null;
if (!is_null($value)) {
if (is_object($value) && method_exists($value, 'toArray')) {
$rtn = $value->toArray($depth, $currentDepth + 1);
} elseif (is_array($value)) {
$childArray = [];
foreach ($value as $k => $v) {
$childArray[$k] = $this->valueToArray($v, $currentDepth + 1, $depth);
}
$rtn = $childArray;
} else {
$rtn = (is_string($value) && !mb_check_encoding($value, 'UTF-8'))
? mb_convert_encoding($value, 'UTF-8')
: $value;
}
}
return $rtn;
}
/**
* @return array
*/

View file

@ -300,18 +300,14 @@ class BuildController extends Controller
*/
protected function formatBuilds($builds)
{
Project::$sleepable = ['id', 'title', 'reference', 'type'];
$rtn = ['count' => $builds['count'], 'items' => []];
/** @var Build $build */
foreach ($builds['items'] as $build) {
$item = $build->toArray(1);
$header = new View('Build/header-row');
$header = new View('Build/header-row');
$header->build = $build;
$item['header_row'] = $header->render();
$rtn['items'][$item['id']] = $item;
$rtn['items'][$build->getId()]['header_row'] = $header->render();
}
ksort($rtn['items']);

View file

@ -34,21 +34,11 @@ class Build extends Model
const SOURCE_WEBHOOK = 4;
const SOURCE_WEBHOOK_PULL_REQUEST = 5;
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'build';
/**
* @var string
*/
protected $modelName = 'Build';
/**
* @var integer
*/

View file

@ -12,21 +12,11 @@ class BuildError extends Model
const SEVERITY_NORMAL = 2;
const SEVERITY_LOW = 3;
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'build_error';
/**
* @var string
*/
protected $modelName = 'BuildError';
/**
* @var array
*/

View file

@ -7,21 +7,11 @@ use b8\Store\Factory;
class BuildMeta extends Model
{
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'build_meta';
/**
* @var string
*/
protected $modelName = 'BuildMeta';
/**
* @var array
*/

View file

@ -6,21 +6,11 @@ use PHPCensor\Model;
class Environment extends Model
{
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'environment';
/**
* @var string
*/
protected $modelName = 'Environment';
/**
* @var array
*/

View file

@ -14,21 +14,11 @@ use Symfony\Component\Yaml\Dumper as YamlDumper;
*/
class Project extends Model
{
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'project';
/**
* @var string
*/
protected $modelName = 'Project';
/**
* @var array
*/

View file

@ -7,21 +7,11 @@ use b8\Store\Factory;
class ProjectGroup extends Model
{
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'project_group';
/**
* @var string
*/
protected $modelName = 'ProjectGroup';
/**
* @var array
*/

View file

@ -10,21 +10,11 @@ use PHPCensor\Model;
*/
class User extends Model
{
/**
* @var array
*/
public static $sleepable = [];
/**
* @var string
*/
protected $tableName = 'user';
/**
* @var string
*/
protected $modelName = 'User';
/**
* @var array
*/

View file

@ -12,17 +12,17 @@ class EnvironmentStore extends Store
/**
* @var string
*/
protected $tableName = 'environment';
protected $tableName = 'environment';
/**
* @var string
*/
protected $modelName = '\PHPCensor\Model\Environment';
protected $modelName = '\PHPCensor\Model\Environment';
/**
* @var string
*/
protected $primaryKey = 'id';
protected $primaryKey = 'id';
/**
* Get a Environment by primary key (Id)