Skip to content

Commit

Permalink
Improve ObjectTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed May 4, 2018
1 parent 1cfd348 commit 2917345
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions system/src/Grav/Framework/Object/Base/ObjectTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,44 @@
*/
trait ObjectTrait
{
static protected $prefix;
/** @var string */
static protected $type;

/**
* @var string
*/
private $_key;
private $key;

/**
* @return string
*/
protected function getTypePrefix()
{
return '';
}

/**
* @param bool $prefix
* @return string
*/
public function getType($prefix = true)
{
$type = $prefix ? $this->getTypePrefix() : '';

if (static::$type) {
return ($prefix ? static::$prefix : '') . static::$type;
return $type . static::$type;
}

$class = get_class($this);
return ($prefix ? static::$prefix : '') . strtolower(substr($class, strrpos($class, '\\') + 1));
return $type . strtolower(substr($class, strrpos($class, '\\') + 1));
}

/**
* @return string
*/
public function getKey()
{
return $this->_key ?: $this->getType() . '@' . spl_object_hash($this);
return $this->key ?: $this->getType() . '@' . spl_object_hash($this);
}

/**
Expand Down Expand Up @@ -159,10 +169,13 @@ public function __toString()

/**
* @param string $key
* @return $this
*/
protected function setKey($key)
{
$this->_key = (string) $key;
$this->key = (string) $key;

return $this;
}

abstract protected function doHasProperty($property);
Expand Down

0 comments on commit 2917345

Please sign in to comment.