<?php
/*
* created on 1.2.2008
*/
class RootObj implements JsonSerializable
{
private $_data;
public function __construct($data = null)
{
$this->_data = $data;
}
public function &__get($name)
{
return $this->_data[$name];
}
public function __set($name, $value)
{
$this->_data[$name] = $value;
}
public function __isset($name)
{
return isset($this->_data[$name]);
}
public static function createObjectsFromArray($className, $array, $defaults = [], $idAsKey = false)
{
$results = [];
foreach ($array as $data) {
$result = new $className ($data);
if ($idAsKey) {
if (is_object($data) && isset($data->id)) {
$results[$data->id] = $result;
} else {
$results[$data['id']] = $result;
}
} else {
$results[] = $result;
}
foreach ((array)$defaults as $key => $value) {
$result->{$key} = $value;
}
}
return $results;
}
public function asArray()
{
return $this->_data;
}
public static function collectionToIds($collection)
{
$ids = [];
foreach ($collection as $item) {
$id = null;
if (is_array($item)) {
$id = $item['id'];
} else {
$id = $item->id;
}
if ($id != null) {
$ids[] = $id;
}
}
return $ids;
}
public function getSaveSql($table)
{
$sqlSnippets = [];
foreach ($this->_data as $key => $value) {
if ($key == 'id') {
continue;
}
if (is_array($value)) {
continue;
}
if (is_object($value)) {
continue;
}
if (is_bool($value)) {
$value = $value ? 1 : 0;
}
if ($value === null) {
$sqlSnippet = "{$key} = null";
} else {
$sqlSnippet = "{$key} = '" . addslashes($value) . "'";
}
$sqlSnippets[] = $sqlSnippet;
}
return ($this->_data['id'] > 0 ? 'update' : 'insert into') . " {$table} set " . implode(',', $sqlSnippets) . ($this->_data['id'] > 0 ? " where id = {$this->_data['id']}" : '');
}
/**
* @param string $propName
* @param null|string $lang
*
* @return string
*/
public function _($propName, $lang = null)
{
if ($lang === null) {
$lang = LangUtils::getLang();
}
$localizedPropName = "{$propName}_{$lang}";
if ($lang === null || $lang === 'cs') {
$localizedPropName = $propName;
}
return $this->{$localizedPropName};
}
public function __toString()
{
$out = get_class($this) . ': ';
$itemsTxt = [];
foreach (array_merge($this->_data, get_object_vars_compatible($this)) as $key => $value) {
$value = json_encode($value);
$itemsTxt[] = "{$key} = {$value}";
}
$out .= implode(',', $itemsTxt);
return $out;
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
// we do not want to include classes like Maternia_Translate or ServiceManager
$attr = get_object_vars_compatible($this);
foreach ($attr as $k => $a) {
if (is_object($a) && ! ($a instanceof stdClass)) {
unset($attr[$k]);
}
}
return add($this->_data, $attr);
}
public function __debugInfo()
{
$class = new \ReflectionClass($this);
$properties = [];
/** @var \ReflectionProperty $staticProperty */
foreach ($class->getStaticProperties() as $staticProperty) {
var_dump($staticProperty);
$staticProperty->setAccessible(true);
$properties[$staticProperty->getName()] = $staticProperty->getValue();
}
/** @var \ReflectionProperty $staticProperty */
foreach ($class->getProperties() as $property) {
$property->setAccessible(true);
$properties[$property->getName()] = $property->getValue($this);
}
var_dump($properties, $class->getParentClass());
$data = $properties['_data'];
unset($properties['_data']);
return array_merge($properties, $data);
}
}
$a = new RootObj();
$a->sfdgsdfg = 'sfgsdfg';
class Bb extends RootObj {
private $test = 'sdfs';
private static $sss = 'sdfs';
}
$b = new Bb();
$b->sfsdfgsdfg = 'asf';
var_dump($a, $b);
preferences:
50.5 ms | 402 KiB | 5 Q