Skip to content

Commit

Permalink
string
Browse files Browse the repository at this point in the history
  • Loading branch information
nineyang committed Sep 6, 2017
1 parent f7515b6 commit 29945f8
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/conf/css.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
/**
* User: Nine
* Date: 2017/9/6
* Time: 下午10:24
*/

return [
'.nine-span' => "color:#df5000;font-size:14px"
];
52 changes: 50 additions & 2 deletions src/decorator/DecoratorComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ abstract class DecoratorComponent
*/
protected $_tail;

/**
* @var array
*/
public $classList = [];

/**
* @var AbstractDump
*/
Expand All @@ -36,12 +41,55 @@ abstract class DecoratorComponent
*/
public function __construct(AbstractDump $dump)
{
$this->dump = $dump;
$this->initStyle();
}

/**
* 初始化css
*/
protected function initStyle()
{
$config = require_once __DIR__ . '/../conf/css.php';
$styleStr = "<style>";
if (!empty($config)) {
foreach ($config as $k => $v) {
$styleStr .= $k . "{" . $v . "}";
}
$styleStr .= "</style>";
}
echo $styleStr;
}

/**
* 添加样式
* @param $className
* @return $this
*/
public function addClass($className)
{
array_push($this->classList, $className);
return $this;
}

/**
* 添加css并转发给display
* @return mixed
*/
public function show()
{
// 设置tag
$parts = explode("\\", static::class);
$class = strtolower(array_pop($parts));
$this->_head = "<" . $class . ">";
// 设置class
$classStr = implode(' ', $this->classList);
$this->_head = "<" . $class . " class='$classStr'>";
$this->_tail = "</" . $class . ">";
$this->dump = $dump;
return static::display();
}

/**
* @return mixed
*/
abstract public function display();
}
1 change: 1 addition & 0 deletions src/decorator/Span.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Span extends DecoratorComponent

public function display()
{
echo "<style>.test{color:red;}</style>";
echo $this->_head . $this->dump->value . $this->_tail;
}
}
4 changes: 3 additions & 1 deletion src/render/DumpString.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ public function __construct($value)
$this->value = $value;
}


public function render()
{
$decorator = new Span($this);
$decorator->display();
$decorator->addClass('nine-span')
->show();
}
}

0 comments on commit 29945f8

Please sign in to comment.