Skip to content

Commit

Permalink
version 1.0-alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
siberfx committed Dec 28, 2023
0 parents commit 76b78d2
Show file tree
Hide file tree
Showing 15 changed files with 1,305 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
; This file is for unifying the coding style for different editors and IDEs.
; More information at http://editorconfig.org

root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor
.idea/
composer.lock
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2023 Selim Görmüş <[email protected]>

> 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.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Siberfx\BiletAll


## Install

1) In your terminal:

``` bash
$ composer require siberfx/biletall-php
```


## Testing

``` bash
// TODO
```

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
56 changes: 56 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"name": "siberfx/biletall-php",
"description": "BiletAll Bus Api Implementation for laravel.",
"keywords": [
"laravel",
"bilet all",
"biletall",
"bus"
],
"homepage": "https://github.com/siberfx/biletall-php",
"license": "MIT",
"authors": [
{
"name": "Selim Görmüş",
"email": "[email protected]",
"role": "Lead Developer"
}
],
"require": {
"ext-soap": "*",
"ext-json": "*",
"ext-dom": "*",
"php": "^8.1",
"laravel/framework": "^10.0",
"laravel/helpers": "^1.7"
},
"require-dev": {
"phpunit/phpunit": "^9.5"
},
"autoload": {
"files": [
"src/Helpers/helper.php"
],
"psr-4": {
"Siberfx\\BiletAll\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Siberfx\\BiletAll\\": "tests"
}
},
"scripts": {
"test": "phpunit"
},
"extra": {
"branch-alias": {
"dev-master": "1.0-dev"
},
"laravel": {
"providers": [
"Siberfx\\BiletAll\\BiletAllServiceProvider"
]
}
}
}
72 changes: 72 additions & 0 deletions src/BiletAllServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

namespace Siberfx\BiletAll;

use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;

class BiletAllServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Where the route file lives, both inside the package and in the app (if overwritten).
*
* @var string
*/
public $routeFilePath = '/routes/siberfx/biletall.php';

/**
* Perform post-registration booting of services.
*
* @return void
*/
public function boot()
{
$this->setupRoutes($this->app->router);

$this->mergeConfigFrom(
__DIR__ . '/config/biletall.php', 'biletall'
);

// publish config file
$this->publishes([__DIR__.'/config' => config_path()], 'config');

}


/**
* Register any package services.
*
* @return void
*/
public function register()
{

}


/**
* Define the routes for the application.
*
* @param \Illuminate\Routing\Router $router
* @return void
*/
public function setupRoutes(Router $router)
{
// by default, use the routes file provided in vendor
$routeFilePathInUse = __DIR__.$this->routeFilePath;

// but if there's a file with the same name in routes/backpack, use that one
if (file_exists(base_path().$this->routeFilePath)) {
$routeFilePathInUse = base_path().$this->routeFilePath;
}

$this->loadRoutesFrom($routeFilePathInUse);
}
}
31 changes: 31 additions & 0 deletions src/Helpers/BooleanParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Siberfx\BiletAll\Helpers;

class BooleanParser
{

public static function parse(string $binaryString = '', array $valueMap = [])
{
$length = strlen($binaryString);

for ($i = 0; $i < $length; $i++) {
$currentChar = $binaryString[$i];
$booleanValue = self::charToBoolean($currentChar);

$valueMap[$i] = $booleanValue;
}

return collect($valueMap)->map(fn($value, $id) => [
'id' => $id,
'value' => $value,
])
->where('value', 1);
}

private static function charToBoolean($char): int
{
// Assuming '1' is true and '0' is false
return $char == '1';
}
}
41 changes: 41 additions & 0 deletions src/Helpers/BusSpecHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Siberfx\BiletAll\Helpers;

class BusSpecHelper
{

public static function handle(string $string = '')
{
$defaultSet = '11111111001000100000000000000000000000000000000000';

if (empty($string)) {
return [];
}

if (app()->environment('local')) {
$string = str_limit($defaultSet, 23);
}


// @todo test data for 23 characters string

$ids = BooleanParser::parse(str_limit($string, 23))->pluck('id')->toArray();

if (empty($ids)) {
return [];
}

return collect(config('biletall.set'))->whereIn('tip', $ids)->get()->map(function ($item) {
return [
'id' => $item->id,
'title' => $item->title,
'description' => $item->description,
'image' => !empty($item->image) ? asset($item->image) : ''
];
});

}


}
78 changes: 78 additions & 0 deletions src/Helpers/XmlToArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

namespace Siberfx\BiletAll\Helpers;

use DOMDocument;

class XmlToArray
{
/**
* Convert valid XML to an array.
*
* @param string $xml
* @param bool $outputRoot
* @return array
*/
public static function convert(string $xml, bool $outputRoot = false)
{
$array = self::xmlStringToArray($xml);
if (!$outputRoot && array_key_exists('@root', $array)) {
unset($array['@root']);
}
return $array;
}

protected static function xmlStringToArray($xmlstr)
{
$doc = new DOMDocument();
$doc->loadXML($xmlstr);
$root = $doc->documentElement;
$output = self::domNodeToArray($root);
$output['@root'] = $root->tagName;
return $output;
}

protected static function domNodeToArray($node)
{
$output = [];
switch ($node->nodeType) {
case XML_CDATA_SECTION_NODE:
case XML_TEXT_NODE:
$output = trim($node->textContent);
break;
case XML_ELEMENT_NODE:
for ($i = 0, $m = $node->childNodes->length; $i < $m; $i++) {
$child = $node->childNodes->item($i);
$v = self::domNodeToArray($child);
if (isset($child->tagName)) {
$t = $child->tagName;
if (!isset($output[$t])) {
$output[$t] = [];
}
$output[$t][] = $v;
} elseif ($v || $v === '0') {
$output = (string)$v;
}
}
if ($node->attributes->length && !is_array($output)) { // Has attributes but isn't an array
$output = ['@content' => $output]; // Change output into an array.
}
if (is_array($output)) {
if ($node->attributes->length) {
$a = [];
foreach ($node->attributes as $attrName => $attrNode) {
$a[$attrName] = (string)$attrNode->value;
}
$output['@attributes'] = $a;
}
foreach ($output as $t => $v) {
if ((count($v) === 1) && is_array($v) && $t !== '@attributes') {
$output[$t] = $v[0];
}
}
}
break;
}
return $output;
}
}
6 changes: 6 additions & 0 deletions src/Helpers/helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

function xml_to_array($xml, $outputRoot = false): array|string
{
return \Siberfx\BiletAll\Helpers\XmlToArray::convert($xml, $outputRoot);
}
Loading

0 comments on commit 76b78d2

Please sign in to comment.