diff options
author | vincent-peugnet <v.peugnet@free.fr> | 2020-04-22 22:13:28 +0200 |
---|---|---|
committer | vincent-peugnet <v.peugnet@free.fr> | 2020-04-22 22:13:28 +0200 |
commit | bd16dc7c6fc630a6b59ca3b041dde146ab50c72b (patch) | |
tree | b2f6a42c0bdb23606c1e9fdba0c28f0aee6b1786 /app/class/Colors.php | |
parent | eba4d7aa0ee5ea1bcb29e8254c28a272023ea54f (diff) | |
download | wcms-bd16dc7c6fc630a6b59ca3b041dde146ab50c72b.tar.gz wcms-bd16dc7c6fc630a6b59ca3b041dde146ab50c72b.zip |
colors refactor with exceptions
Diffstat (limited to 'app/class/Colors.php')
-rw-r--r-- | app/class/Colors.php | 102 |
1 files changed, 71 insertions, 31 deletions
diff --git a/app/class/Colors.php b/app/class/Colors.php index 234adb2..3c6fb19 100644 --- a/app/class/Colors.php +++ b/app/class/Colors.php @@ -5,37 +5,47 @@ namespace Wcms; class Colors extends Item { - protected $file = MODEL::CSS_DIR . 'tagcolors.css'; + protected $file = 'tagcolors.css'; protected $rawcss = ""; protected $tagcolor = []; - - public function __construct(array $taglist = []) + public function __construct(string $file = 'tagcolors.css', array $taglist = []) { - if ($this->readcssfile()) { - $this->parsetagcss(); + $this->setfile($file); + if (file_exists($this->file)) { + $this->rawcss = $this->readcssfile(); + $this->tagcolor = $this->parsetagcss($this->rawcss); } + if (!empty($taglist)) { - $this->removeaddtags($taglist); - $this->tocss(); - $this->writecssfile(); + $this->tagcolor = $this->removeaddtags($taglist); + $this->rawcss = $this->tocss($this->tagcolor); + $this->writecssfile($this->file, $this->rawcss); } } - public function readcssfile(): bool + /** + * Read file containing css + * @return string raw css or empty string + */ + public function readcssfile(): string { - if (MODEL::dircheck(MODEL::CSS_DIR) && file_exists($this->file)) { - $this->rawcss = file_get_contents($this->file); - return true; - } else { - return false; + $rawcss = file_get_contents($this->file); + if (is_string($rawcss)) { + return $rawcss; } + return ''; } - public function removeaddtags(array $taglist = []) + /** + * Check if new tags have been created and generate them a background color + * @param array $taglist associative array using tag as key + * @return array associative array of `tag => background-color` + */ + public function removeaddtags(array $taglist = []): array { $tagcolor = []; foreach ($taglist as $tag => $tagcount) { @@ -45,52 +55,73 @@ class Colors extends Item $tagcolor[$tag] = '#' . dechex(rand(50, 255)) . dechex(rand(50, 255)) . dechex(rand(50, 255)); } } - $this->tagcolor = $tagcolor; + return $tagcolor; } /** - * Transform a CSS string in a array of `tag => background-color` - * - * @return array Ouput array using TAG as key and Hex Color as value + * Transform a CSS string in a array of datas + * @param string $rawcss CSS string to parse + * @return array associative array of `tag => background-color` */ - public function parsetagcss() + public function parsetagcss(string $rawcss): array { $pattern = '%.tag\_([a-z0-9\-\_]*)\s*\{\s*background-color:\s*(#[A-Fa-f0-9]{6})\;\s*\}%'; - preg_match_all($pattern, $this->rawcss, $matches); + preg_match_all($pattern, $rawcss, $matches); $tagcolor = array_combine($matches[1], $matches[2]); if ($tagcolor !== false) { - $this->tagcolor = $tagcolor; - return true; + return $tagcolor; } else { - return false; + return []; } } - public function tocss() + /** + * Generate CSS string from datas + * @param array $tagcolor associative array of `tag => background-color` + * @return string CSS + */ + public function tocss(array $tagcolor): string { $css = ""; - foreach ($this->tagcolor as $tag => $color) { + foreach ($tagcolor as $tag => $color) { $css .= PHP_EOL . '.tag_' . $tag . ' { background-color: ' . $color . '; }'; } - $this->rawcss = $css; + return $css; } - public function writecssfile() + /** + * Write css in the file + * @param string $rawcss + * @throws \InvalidArgumentException If cant create + */ + public function writecssfile(string $file, string $rawcss) { - if (MODEL::dircheck(MODEL::CSS_DIR)) { - return file_put_contents($this->file, $this->rawcss); + accessfile($file, true); + if (!file_put_contents($file, $rawcss)) { + throw new \InvalidArgumentException("cant create file : $this->file", 1); } } + /** + * Update tagcolors based on datas + * @param array $tagcolor associative array of `tag => background-color` + */ + public function update(array $tagcolor) + { + $this->settagcolor($tagcolor); + $this->rawcss = $this->tocss($this->tagcolor); + $this->writecssfile($this->file, $this->rawcss); + } + public function htmlcolorpicker(): string { $html = '<ul>'; foreach ($this->tagcolor as $tag => $color) { $i = '<input type="color" name="tagcolor[' . $tag . ']" value="' . $color . '" id="color_' . $tag . '">'; $l = '<label for="color_' . $tag . '" >' . $tag . '</label>'; - $html .= '\n<li>' . $i . $l . '</li>'; + $html .= '<li>' . $i . $l . '</li>'; } $html .= PHP_EOL . '</ul>'; return $html; @@ -111,6 +142,15 @@ class Colors extends Item // _______________________ S E T _________________________ + /** + * @throws \InvalidArgumentException If cant access file + */ + public function setfile(string $path) + { + accessfile($path); + $this->file = $path; + } + public function setrawcss($rawcss) { if (is_string($rawcss)) { |