Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/Model/Entity/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Muffin\Tags\Model\Entity;

use Cake\ORM\Entity;
use Cake\Utility\Text;

class Tag extends Entity
{
Expand All @@ -15,4 +16,38 @@ class Tag extends Entity
'*' => false,
'label' => true,
];

/**
* Template used by `__toString()`. If empty, fallsback to `EntityTrait::__toString()`.
*
* @var string
*/
protected $_toStringTemplate = ':label';

/**
* Getter/setter for the `$_toStringTemplate` property.
*
* @param string $template String template. Supported placeholders are `:label` and `:id`.
* @return string
*/
public function toStringTemplate($template = null)
{
if ($template !== null) {
$this->_toStringTemplate = $template;
}
return $this->_toStringTemplate;
}

/**
* Returns the tag as a string (according to the `$_toStringTemplate` defined) or fallsback
* to the `EntityTrait::__toString()`.
*
* @return string
*/
public function __toString()
{
return !empty($this->_toStringTemplate) ?
Text::insert($this->toStringTemplate(), $this->toArray()) :
parent::__toString();
}
}