Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
kamshory committed Oct 13, 2024
1 parent 4837257 commit e2bea7e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 94 deletions.
139 changes: 45 additions & 94 deletions src/DataTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ class DataTable extends SetterGetter
*/
public function __construct($data = null)
{
if(isset($data))
{
if (isset($data)) {
$this->loadData($data);
}
$this->init();
Expand All @@ -111,26 +110,20 @@ public function __construct($data = null)
*/
public function loadData($data)
{
if($data != null)
{
if($data instanceof MagicObject)
{
if ($data != null) {
if ($data instanceof MagicObject) {
$values = $data->value();
try
{
try {
$this->_tableInfo = $data->tableInfo();
}
catch(Exception $e)
{
} catch (Exception $e) {
$this->_tableInfo = null;
}
foreach ($values as $key => $value) {
$key2 = PicoStringUtil::camelize(str_replace("-", "_", $key));
$this->set($key2, $value);
$this->_labels[$key2] = $data->label($key2);
}
}
else if (is_array($data) || is_object($data)) {
} elseif (is_array($data) || is_object($data)) {
foreach ($data as $key => $value) {
$key2 = PicoStringUtil::camelize(str_replace("-", "_", $key));
$this->set($key2, $value);
Expand All @@ -151,8 +144,7 @@ public function loadData($data)
public function addLanguage($code, $reference, $use = false)
{
$this->_lableLanguage[$code] = new PicoLanguage($reference);
if($use)
{
if ($use) {
$this->selectLanguage($code);
}
return $this;
Expand All @@ -166,12 +158,10 @@ public function addLanguage($code, $reference, $use = false)
*/
public function removeLanguage($code)
{
if(isset($this->_lableLanguage[$code]))
{
if (isset($this->_lableLanguage[$code])) {
unset($this->_lableLanguage[$code]);
}
if(!empty($this->_lableLanguage))
{
if (!empty($this->_lableLanguage)) {
$keys = array_keys($this->_lableLanguage);
$this->selectLanguage($keys[0]);
}
Expand Down Expand Up @@ -203,17 +193,15 @@ private function init()
$classList = $reflexClass->parseKeyValueAsObject($reflexClass->getFirstParameter(self::CLASS_LIST));
$prefLanguage = $reflexClass->parseKeyValueAsObject($reflexClass->getFirstParameter(self::ANNOTATION_LANGUAGE));
$defaultColumnName = $reflexClass->parseKeyValueAsObject($reflexClass->getFirstParameter(self::ANNOTATION_DEFAULT_COLUMN_LABEL));
if($defaultColumnName->issetContent())
{

if ($defaultColumnName->issetContent()) {
$this->_defaultColumnName = $defaultColumnName->getContent();
}
if($classList->issetContent())
{
if ($classList->issetContent()) {
$this->_classList = explode(" ", preg_replace('/\s+/', " ", $classList->getContent()));
$this->_classList = array_unique($this->_classList);
}
if($prefLanguage->issetContent())
{
if ($prefLanguage->issetContent()) {
$this->_currentLanguage = $prefLanguage->getContent();
}
$this->_tableIdentity = $reflexClass->parseKeyValueAsObject($reflexClass->getFirstParameter(self::ANNOTATION_TABLE));
Expand All @@ -232,28 +220,25 @@ protected function propertyList($reflectSelf = false, $asArrayProps = false)
$reflectionClass = $reflectSelf ? self::class : get_called_class();
$class = new ReflectionClass($reflectionClass);

// filter only the calling class properties
// skip parent properties
// Filter only the calling class properties
// Skip parent properties
$properties = array_filter(
$class->getProperties(),
function($property) use($class) {
function ($property) use ($class) {
return $property->getDeclaringClass()->getName() == $class->getName();
}
);
if($asArrayProps)
{

if ($asArrayProps) {
$result = array();
$index = 0;
foreach ($properties as $key) {
$prop = $key->name;
$result[$index] = $prop;

$index++;
}
return $result;
}
else
{
} else {
return $properties;
}
}
Expand All @@ -269,11 +254,9 @@ function($property) use($class) {
*/
private function annotationContent($reflexProp, $parameters, $annotation, $attribute)
{
if($parameters->get($annotation) != null)
{
if ($parameters->get($annotation) != null) {
$attrs = $reflexProp->parseKeyValueAsObject($parameters->get($annotation));
if($attrs->get($attribute) != null)
{
if ($attrs->get($attribute) != null) {
return $attrs->get($attribute);
}
}
Expand All @@ -292,25 +275,17 @@ private function annotationContent($reflexProp, $parameters, $annotation, $attri
private function label($reflexProp, $parameters, $key, $defaultLabel)
{
$label = $defaultLabel;
if(stripos($this->_defaultColumnName, "->"))
{
if (stripos($this->_defaultColumnName, "->")) {
$cn = explode("->", $this->_defaultColumnName);
$lbl = $this->annotationContent($reflexProp, $parameters, trim($cn[0]), trim($cn[1]));
$label = PicoStringUtil::selectNotNull($lbl, $defaultLabel);

}
else if($this->_defaultColumnName == self::ANNOTATION_LANGUAGE)
{
if(isset($this->_lableLanguage) && isset($this->_lableLanguage[$this->_currentLanguage]))
{
} elseif ($this->_defaultColumnName == self::ANNOTATION_LANGUAGE) {
if (isset($this->_lableLanguage) && isset($this->_lableLanguage[$this->_currentLanguage])) {
$label = $this->_lableLanguage[$this->_currentLanguage]->isset($key) ? $this->_lableLanguage[$this->_currentLanguage]->get($key) : $defaultLabel;
}
else
{
} else {
$lbl = $this->annotationContent($reflexProp, $parameters, "Label", "content");
$label = PicoStringUtil::selectNotNull($lbl, $defaultLabel);
}

}
return $label;
}
Expand All @@ -326,22 +301,18 @@ private function label($reflexProp, $parameters, $key, $defaultLabel)
*/
private function appendByProp($doc, $tbody, $props, $className)
{
foreach($props as $prop)
{
foreach ($props as $prop) {
$key = $prop->name;
$label = $key;
$value = $this->get($key);
if(is_scalar($value))
{
if (is_scalar($value)) {
$tr = $tbody->appendChild($doc->createElement(self::TAG_TR));

$reflexProp = new PicoAnnotationParser($className, $key, PicoAnnotationParser::PROPERTY);

if($reflexProp != null)
{
if ($reflexProp != null) {
$parameters = $reflexProp->getParametersAsObject();
if($parameters->issetLabel())
{
if ($parameters->issetLabel()) {
$label = $this->label($reflexProp, $parameters, $key, $label);
}
}
Expand All @@ -367,10 +338,8 @@ private function appendByProp($doc, $tbody, $props, $className)
*/
private function appendByValues($doc, $tbody, $values)
{
foreach($values as $propertyName=>$value)
{
if(is_scalar($value))
{
foreach ($values as $propertyName => $value) {
if (is_scalar($value)) {
$tr = $tbody->appendChild($doc->createElement(self::TAG_TR));
$label = $this->getLabel($propertyName);

Expand All @@ -394,22 +363,15 @@ private function appendByValues($doc, $tbody, $values)
private function getLabel($propertyName)
{
$label = "";
if(isset($this->_lableLanguage[$this->_currentLanguage]))
{
if (isset($this->_lableLanguage[$this->_currentLanguage])) {
$language = $this->_lableLanguage[$this->_currentLanguage];

$label = $language->get($propertyName);

}
else
{
if(isset($this->_labels[$propertyName]))
{
} else {
if (isset($this->_labels[$propertyName])) {
$label = $this->_labels[$propertyName];
}
}
if(empty($label))
{
if (empty($label)) {
$label = PicoStringUtil::camelToTitle($propertyName);
}
return $label;
Expand All @@ -423,10 +385,9 @@ private function getLabel($propertyName)
*/
public function addClass($className)
{
if(PicoTableUtil::isValidClassName($className))
{
if (PicoTableUtil::isValidClassName($className)) {
$this->_classList[] = $className;
// fix duplicated class
// Fix duplicated class
$this->_classList = array_unique($this->_classList);
}
return $this;
Expand All @@ -440,17 +401,10 @@ public function addClass($className)
*/
public function removeClass($className)
{
if(PicoTableUtil::isValidClassName($className))
{
$tmp = array();
foreach($this->_classList as $cls)
{
if($cls != $className)
{
$tmp[] = $cls;
}
}
$this->_classList = $tmp;
if (PicoTableUtil::isValidClassName($className)) {
$this->_classList = array_filter($this->_classList, function ($cls) use ($className) {
return $cls != $className;
});
}
return $this;
}
Expand Down Expand Up @@ -488,16 +442,13 @@ public function __toString()
$doc->formatOutput = true;

$props = $this->propertyList();
if(!empty($props))
{
if (!empty($props)) {
$this->appendByProp($doc, $tbody, $props, $className);
}
else
{
} else {
$values = $this->value();
$this->appendByValues($doc, $tbody, $values);
}
return $doc->saveHTML();
return $doc->saveHTML();
}

/**
Expand All @@ -509,4 +460,4 @@ public function getTableInfo()
{
return $this->_tableInfo;
}
}
}
1 change: 1 addition & 0 deletions src/SecretObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ private function secureKey()
* @param array $params Parameters for the method.
* @return mixed|null The result of the method call or null if not applicable.
*/

public function __call($method, $params) // NOSONAR
{
if (strncasecmp($method, "isset", 5) === 0) {
Expand Down
1 change: 1 addition & 0 deletions src/SetterGetter.php
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ function($property) use($class)
* @param array $params Parameters passed to the method.
* @return mixed|null The result of the method call or null if the method does not return a value.
*/

public function __call($method, $params) //NOSONAR
{
if (strncasecmp($method, "isset", 5) === 0)
Expand Down

0 comments on commit e2bea7e

Please sign in to comment.