commit 9e1fd4b8b72b2f365653d1e129a51c8a96fbc684 Author: Kavalar Date: Thu Jun 8 00:40:39 2023 +0300 first version diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cb756e9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor +.idea \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..69b61ce --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +Интеграция пакета cg-select для PHP + +**Ссылка на документацию** + +**Пакет на npmjs** + +Для запуска примера:
+``` +php -S localhost:8088 -t=examples +``` +Пример будет доступен по ссылке http://localhost:8088 \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a58da12 --- /dev/null +++ b/composer.json @@ -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": {} +} diff --git a/examples/category.php b/examples/category.php new file mode 100644 index 0000000..9c6601f --- /dev/null +++ b/examples/category.php @@ -0,0 +1,48 @@ + 'Choose a car', + 'label' => 'Example', + 'searchMode' => true, + 'items' => [ + [ + 'category' => 'Russia', + 'categoryItems' => [ + ['id' => 1, 'title' => 'Москва', 'value' => 1], + 'Саратов', + 'Волгоград', + 'Донецк', + ] + ], + [ + 'category' => 'USA', + 'categoryItems' => [ + 'Alabama', + 'Texas', + 'Los-Angeles', + ] + ], + ] +]) + +?> + + + PHP CG-Select Category Example + + + + +
+
+ + +
+
+ + + + \ No newline at end of file diff --git a/examples/index.php b/examples/index.php new file mode 100644 index 0000000..fc61290 --- /dev/null +++ b/examples/index.php @@ -0,0 +1,43 @@ + 'Choose a car', + 'label' => 'Example', + 'name' => 'some', + 'items' => [ + 'BMW', + [ + 'id' => 1, + 'title' => 'Opel', + 'value' => 23, + ], + 'Lada' + ], + 'styles' => [ + 'head' => [ + 'width' => '500px', + ] + ], +]) + +?> + + + PHP CG-Select Example + + + + +
+
+ + +
+
+ + + + \ No newline at end of file diff --git a/src/CGSelect.php b/src/CGSelect.php new file mode 100644 index 0000000..43bbae6 --- /dev/null +++ b/src/CGSelect.php @@ -0,0 +1,23 @@ +driver = new CGSelectDriver($selector, $options); + } + + public function getJS(){ + return $this->driver->getJS(); + } + + public static function renderCDN($version = 'latest') + { + CGSelectDriver::renderCDN($version); + } + +} \ No newline at end of file diff --git a/src/CGSelectDriver.php b/src/CGSelectDriver.php new file mode 100644 index 0000000..fd4a570 --- /dev/null +++ b/src/CGSelectDriver.php @@ -0,0 +1,182 @@ +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 ""; + } + + /** + * @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 .= "},"; + } + +} \ No newline at end of file