Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
izica committed Jun 17, 2018
1 parent b434be4 commit e31205d
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 1 deletion.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2018 izica

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
110 changes: 110 additions & 0 deletions PhpStyles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php

/**
* Class PhpStylesInline
*/
class PhpStyles
{
/**
* @var string
*/
public $styles = [];
private $classname = '';
private $media = false;
private $mediaFrom = '';
private $mediaTo = '';


public function inline()
{
return new PhpStylesInline();
}

/**
* @param bool $condition
* @return string
*/

public function render($condition = true)
{
if (!$condition) {
return '';
}
$this->classname = uniqid('php-styles-');

$array = [];
foreach ($this->styles as $key => $value) {
$array[$key] = $key . ': ' . $value;
}
$css = implode(';', $array);
echo "<style>";
if($this->media){
echo "@media (min-width: {$this->mediaFrom}px) and (max-width: {$this->mediaTo}px) {";
}
echo ".{$this->classname}{";
echo $css;
echo '}';
if($this->media) {
echo '}';
}
echo "</style>";

return $this->classname;
}

/**
* @return string
*/
public function media($minWidth = 0, $maxWidth = 9999)
{
$this->media = true;
$this->mediaFrom =$minWidth;
$this->mediaTo = $maxWidth;
return $this;
}

/**
* @param $key
* @param $value
* @param $condition
* @return $this
*/
public function set($key, $value, $condition = true)
{
if ($condition) {
$this->styles[$key] = $value;
}
return $this;
}

/**
* @param $value
* @param $condition
* @return $this
*/
public function opacity($value, $condition)
{
return $this->set('opacity', $value, $condition);
}
}

/**
* Class PhpStyles
*/
class PhpStylesInline extends PhpStyles
{
public function render($condition = true)
{
if (!$condition) {
return '';
}

$css = '';
$array = [];
foreach ($this->styles as $key => $value) {
$array[$key] = $key . ': ' . $value;
}
$css = implode(';', $array);
return "style='{$css}'";
}
}
23 changes: 23 additions & 0 deletions PhpStylesInline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
require_once('PhpStyles.php');

/**
* Class PhpStyles
*/
class PhpStylesInline extends PhpStyles
{
public function render($condition = true)
{
if (!$condition) {
return '';
}

$css = '';
$array = [];
foreach ($this->styles as $key => $value) {
$array[$key] = $key . ': ' . $value;
}
$css = implode(';', $array);
return "style='{$css}'";
}
}
51 changes: 50 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,50 @@
# php-css-styles
![browser screen](https://raw.githubusercontent.com/izica/php-browser-log/master/screen.png "browser screen")
## Install
```
composer require izica/php-styles
```

## Usage
generate inline tag style
```php
$sStyles = (new PhpStylesInline())->opacity(0, $sContact == '')->render();
or
$sStyles = styles(true)->opacity(0, $sContact == '')->render();
or
$sStyles = styles()->inline->opacity(0, $sContact == '')->render();
```
insert style
```html
<div <?=$sStyles?>>
<?=$sContact;?>
</div>
```

generate style with class(class styles supports media query)
```php
$sClassname = (new PhpStyles())->media(0, 1024)->opacity(0, $sContact == '')->render();
or
$sClassname = styles()->media(0, 1024)->opacity(0, $sContact == '')->render();
```

insert style
```html
<div class="<?=$sClassname?>">
<?=$sContact;?>
</div>
```

## Documentation
* styles() - returns PhpStyles
* styles(true) - returns PhpStylesInline
* PhpStyles
* inline() - returns PhpStyles
* media(sizeFrom: number, sizeTo: number)
* set(key: string, value: string or number, condition: bool(not required)) - returns $this(if condition == false, not set)
* render(condition: bool(not required))- returns unique class name(if condition == false, returns empty string)
* opacity(value, condition(not required))
* PhpStylesInline
* media(sizeFrom: number, sizeTo: number)
* set(key: string, value: string or number, condition: bool(not required)) - returns $this(if condition == false, not set)
* render(condition: bool(not required))- returns unique class name(if condition == false, returns empty string)
* opacity(value, condition(not required))
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "izica/php-styles",
"description": "inline css styles generator(conditions support)",
"authors": [
{
"name": "Golovarchuk Artyom",
"email": "[email protected]"
}
],
"keywords": ["css", "php", "css-inline", "generator", "styles"],
"license": "MIT",
"require": {
"php": ">=5.6.0"
},
"autoload": {
"classmap": [
"PhpStyles.php",
"PhpStylesInline.php"
],
"files": [
"styles.php"
]
}
}
14 changes: 14 additions & 0 deletions styles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?
require_once('PhpStyles.php');
require_once('PhpStylesInline.php');

/**
* @return PhpStylesInline
*/
function styles($inline = false)
{
if($inline){
return new PhpStylesInline();
}
return new PhpStyles();
}

0 comments on commit e31205d

Please sign in to comment.