first version

This commit is contained in:
Kavalar 2023-06-08 00:40:39 +03:00
commit 9e1fd4b8b7
7 changed files with 328 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
vendor
.idea

11
README.md Normal file
View File

@ -0,0 +1,11 @@
<b>Интеграция пакета <a href='https://cg-select.itguild.info/'>cg-select</a> для PHP</b>
**<a href='https://cg-select.itguild.info/docs/index.html'>Ссылка на документацию</a>**
**<a href='https://www.npmjs.com/package/cg-select'>Пакет на npmjs</a>**
Для запуска примера:<br>
```
php -S localhost:8088 -t=examples
```
Пример будет доступен по ссылке http://localhost:8088

19
composer.json Normal file
View File

@ -0,0 +1,19 @@
{
"name": "itguild/php-cg-select",
"description": "Wrapper for CG-Select",
"type": "composer-plugin",
"license": "ISC",
"autoload": {
"psr-4": {
"Itguild\\PhpCgSelect\\": "src/"
}
},
"authors": [
{
"name": "Kavalar",
"email": "apuc06@mail.ru"
}
],
"minimum-stability": "stable",
"require": {}
}

48
examples/category.php Normal file
View File

@ -0,0 +1,48 @@
<?php
require_once '../vendor/autoload.php';
$widget = new \Itguild\PhpCgSelect\CGSelect(".cg-dropdown_selector", [
'placeholder' => 'Choose a car',
'label' => 'Example',
'searchMode' => true,
'items' => [
[
'category' => 'Russia',
'categoryItems' => [
['id' => 1, 'title' => 'Москва', 'value' => 1],
'Саратов',
'Волгоград',
'Донецк',
]
],
[
'category' => 'USA',
'categoryItems' => [
'Alabama',
'Texas',
'Los-Angeles',
]
],
]
])
?>
<html>
<head>
<title>PHP CG-Select Category Example</title>
<?php \Itguild\PhpCgSelect\CGSelect::renderCDN(); ?>
</head>
<body>
<div>
<form action="">
<button class="cg-dropdown_selector"></button>
<button type="submit">Оправить</button>
</form>
</div>
<script>
<?php echo $widget->getJS() ?>
</script>
</body>
</html>

43
examples/index.php Normal file
View File

@ -0,0 +1,43 @@
<?php
require_once '../vendor/autoload.php';
$widget = new \Itguild\PhpCgSelect\CGSelect(".cg-dropdown_selector", [
'placeholder' => 'Choose a car',
'label' => 'Example',
'name' => 'some',
'items' => [
'BMW',
[
'id' => 1,
'title' => 'Opel',
'value' => 23,
],
'Lada'
],
'styles' => [
'head' => [
'width' => '500px',
]
],
])
?>
<html>
<head>
<title>PHP CG-Select Example</title>
<?php \Itguild\PhpCgSelect\CGSelect::renderCDN(); ?>
</head>
<body>
<div>
<form action="">
<button class="cg-dropdown_selector"></button>
<button type="submit">Оправить</button>
</form>
</div>
<script>
<?php echo $widget->getJS() ?>
</script>
</body>
</html>

23
src/CGSelect.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Itguild\PhpCgSelect;
class CGSelect
{
protected $driver;
public function __construct(string $selector, array $options = [])
{
$this->driver = new CGSelectDriver($selector, $options);
}
public function getJS(){
return $this->driver->getJS();
}
public static function renderCDN($version = 'latest')
{
CGSelectDriver::renderCDN($version);
}
}

182
src/CGSelectDriver.php Normal file
View File

@ -0,0 +1,182 @@
<?php
namespace Itguild\PhpCgSelect;
/**
*
*/
class CGSelectDriver
{
public array $initData = [];
public string $itemsStr = '';
public string $stylesStr = '';
public string $paramsStr = '';
public string $html = '';
public string $js = '';
public function __construct(string $selector, array $options = [])
{
$this->initData['selector'] = $selector;
$this->initData = array_merge($this->initData, $options);
}
/**
* @return void
*/
public function createJS(): void
{
$this->js .= 'const dropdown = new CGSelect({';
$this->js .= $this->getParams();
$this->js .= $this->getItems();
$this->js .= $this->getStyles();
$this->js .= '});';
}
/**
* @return string
*/
public function getJS(): string
{
$this->createJS();
return $this->js;
}
/**
* @param string $version
* @return void
*/
public static function renderCDN(string $version = 'latest'): void
{
echo "<script src='https://cdn.itguild.info/items/cg-select/$version/main.js'></script>";
}
/**
* @return string
*/
public function getParams(): string
{
return $this->createParams($this->initData);
}
/**
* @param array $data
* @return string
*/
private function createParams(array $data): string
{
foreach ($data as $key => $datum) {
if (is_string($datum)) {
$this->paramsStr .= "$key: '$datum',";
}
if (is_integer($datum)) {
$this->paramsStr .= "$key: $datum,";
}
if (is_bool($datum)) {
$this->paramsStr .= "$key: $datum,";
}
}
return $this->paramsStr;
}
/**
* @param array $data
* @return string
*/
private function createParamsLocal(array $data): string
{
$paramsStr = '';
foreach ($data as $key => $datum) {
if (is_string($datum)) {
$paramsStr .= "$key: '$datum',";
}
if (is_integer($datum)) {
$paramsStr .= "$key: $datum,";
}
if (is_bool($datum)) {
$paramsStr .= "$key: $datum,";
}
}
return $paramsStr;
}
/**
* @return string|bool
*/
public function getItems(): string|bool
{
if (isset($this->initData['items'])) {
$this->createItems($this->initData['items']);
return $this->itemsStr;
}
return false;
}
/**
* @param array $data
* @return void
*/
private function createItems(array $data): void
{
$this->itemsStr .= "items: [";
foreach ($data as $datum) {
if (is_string($datum)) {
$this->itemsStr .= "'$datum',";
}
if (is_array($datum)) {
$this->itemsStr .= "{";
if (isset($datum['category']) && isset($datum['categoryItems'])) {
$this->itemsStr .= "category: '" . $datum['category'] . "',";
$this->itemsStr .= "categoryItems: [";
foreach ($datum['categoryItems'] as $item) {
if (is_string($item)) {
$this->itemsStr .= "'$item',";
}
if (is_array($item)) {
$this->itemsStr .= "{";
$this->itemsStr .= $this->createParamsLocal($item);
$this->itemsStr .= "},";
}
}
$this->itemsStr .= "],";
} else {
$this->itemsStr .= $this->createParamsLocal($datum);
}
$this->itemsStr .= "},";
}
}
$this->itemsStr .= "],";
}
/**
* @return string|bool
*/
public function getStyles(): string|bool
{
if (isset($this->initData['styles'])) {
$this->createStyle($this->initData['styles']);
return $this->stylesStr;
}
return false;
}
/**
* @param array $styles
* @return void
*/
public function createStyle(array $styles): void
{
$this->stylesStr .= "styles: {";
foreach ($styles as $key => $style) {
$this->stylesStr .= "$key: {" . $this->createParamsLocal($style) . "}";
}
$this->stylesStr .= "},";
}
}