Skip to content

Commit b4fc454

Browse files
committed
First Commit
0 parents  commit b4fc454

19 files changed

Lines changed: 3242 additions & 0 deletions

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/.idea/
2+
/.vs/
3+
/.vscode/
4+
/vendor/
5+
/composer.lock
6+
/.phpunit.result.cache
7+
/nbproject/private/
8+
/*.log

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 InitORM
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# InitORM (QueryBuilder)
2+
3+
```
4+
composer require initorm/query-builder
5+
```
6+
7+
Test :
8+
```
9+
php vendor/bin/phpunit
10+
```

composer.json

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"name": "initorm/query-builder",
3+
"description": "InitORM QueryBuilder Library",
4+
"type": "library",
5+
"license": "MIT",
6+
"autoload": {
7+
"psr-4": {
8+
"InitORM\\QueryBuilder\\": "src/"
9+
}
10+
},
11+
"autoload-dev": {
12+
"psr-4": {
13+
"Test\\InitORM\\QueryBuilder\\": "tests/"
14+
}
15+
},
16+
"authors": [
17+
{
18+
"name": "Muhammet ŞAFAK",
19+
"email": "info@muhammetsafak.com.tr",
20+
"role": "Developer",
21+
"homepage": "https://www.muhammetsafak.com.tr"
22+
}
23+
],
24+
"minimum-stability": "stable",
25+
"require": {
26+
"php": ">=8.0",
27+
"ext-pdo": "*"
28+
},
29+
"require-dev": {
30+
"phpunit/phpunit": "^10.5"
31+
}
32+
}

phpunit.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<phpunit colors="true" bootstrap="vendor/autoload.php">
3+
<testsuites>
4+
<testsuite name="Unit Tests">
5+
<directory>tests</directory>
6+
</testsuite>
7+
</testsuites>
8+
</phpunit>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* InitORM QueryBuilder
4+
*
5+
* This file is part of InitORM QueryBuilder.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2023 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
namespace InitORM\QueryBuilder\Exceptions;
16+
17+
use Exception;
18+
19+
class QueryBuilderException extends Exception
20+
{
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
/**
3+
* InitORM QueryBuilder
4+
*
5+
* This file is part of InitORM QueryBuilder.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2023 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
namespace InitORM\QueryBuilder\Exceptions;
16+
17+
use InvalidArgumentException;
18+
19+
class QueryBuilderInvalidArgumentException extends InvalidArgumentException
20+
{
21+
}

src/ParameterInterface.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
/**
3+
* InitORM QueryBuilder
4+
*
5+
* This file is part of InitORM QueryBuilder.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2023 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
namespace InitORM\QueryBuilder;
16+
17+
interface ParameterInterface
18+
{
19+
20+
/**
21+
* @param string $key
22+
* @param mixed $value
23+
* @return self
24+
*/
25+
public function set(string $key, mixed $value): self;
26+
27+
/**
28+
* @param string|RawQuery $key
29+
* @param mixed $value
30+
* @return string
31+
*/
32+
public function add(RawQuery|string $key, mixed $value): string;
33+
34+
35+
/**
36+
* @param array|ParameterInterface ...$arrays
37+
* @return self
38+
*/
39+
public function merge(array|ParameterInterface ...$arrays): self;
40+
41+
/**
42+
* @param string|null $key
43+
* @param mixed|null $default
44+
* @return array|mixed|null
45+
*/
46+
public function get(?string $key = null, mixed $default = null): mixed;
47+
48+
/**
49+
* @return array
50+
*/
51+
public function all(): array;
52+
53+
/**
54+
* @return self
55+
*/
56+
public function reset(): self;
57+
58+
}

src/Parameters.php

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
/**
3+
* InitORM QueryBuilder
4+
*
5+
* This file is part of InitORM QueryBuilder.
6+
*
7+
* @author Muhammet ŞAFAK <info@muhammetsafak.com.tr>
8+
* @copyright Copyright © 2023 Muhammet ŞAFAK
9+
* @license ./LICENSE MIT
10+
* @version 1.0
11+
* @link https://www.muhammetsafak.com.tr
12+
*/
13+
14+
declare(strict_types=1);
15+
namespace InitORM\QueryBuilder;
16+
17+
use Closure;
18+
19+
class Parameters implements ParameterInterface
20+
{
21+
22+
protected array $parameters;
23+
24+
public function __construct()
25+
{
26+
$this->parameters = [];
27+
}
28+
29+
/**
30+
* @inheritDoc
31+
*/
32+
public function set(string $key, mixed $value): self
33+
{
34+
$this->parameters[':' . ltrim(str_replace('.', '', $key), ':')] = $value;
35+
36+
return $this;
37+
}
38+
39+
/**
40+
* @inheritDoc
41+
*/
42+
public function add(RawQuery|string $key, mixed $value): string
43+
{
44+
if ($value === null) {
45+
return 'NULL';
46+
}
47+
if ($key instanceof RawQuery) {
48+
$key = md5((string)$key);
49+
}
50+
$originKey = ltrim(str_replace('.', '', $key), ':');
51+
$i = 0;
52+
do {
53+
$key = ':' . ($i === 0 ? $originKey : $originKey . '_' . $i);
54+
++$i;
55+
$hasParameter = isset($this->parameters[$key]);
56+
} while($hasParameter);
57+
58+
$this->parameters[$key] = $value;
59+
60+
return $key;
61+
}
62+
63+
/**
64+
* @inheritDoc
65+
*/
66+
public function merge(array|ParameterInterface ...$arrays): self
67+
{
68+
foreach ($arrays as $array) {
69+
if ($array instanceof ParameterInterface) {
70+
$array = $array->all();
71+
}
72+
foreach ($array as $key => $value) {
73+
$this->set($key, $value);
74+
}
75+
}
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* @inheritDoc
82+
*/
83+
public function get(?string $key = null, mixed $default = null): mixed
84+
{
85+
if ($key === null) {
86+
return $this->parameters;
87+
}
88+
89+
$key = ':' . ltrim($key, ':');
90+
if (isset($this->parameters[$key])) {
91+
return $this->parameters[$key];
92+
}
93+
94+
return ($default instanceof Closure) ? call_user_func_array($default, []) : $default;
95+
}
96+
97+
/**
98+
* @inheritDoc
99+
*/
100+
public function all(): array
101+
{
102+
return $this->parameters;
103+
}
104+
105+
/**
106+
* @inheritDoc
107+
*/
108+
public function reset(): self
109+
{
110+
$this->parameters = [];
111+
112+
return $this;
113+
}
114+
115+
}

0 commit comments

Comments
 (0)