Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammetsafak committed Dec 9, 2023
0 parents commit 7a3e592
Show file tree
Hide file tree
Showing 15 changed files with 955 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/.idea/
/.vs/
/.vscode/
/vendor/
/composer.lock
/.phpunit.result.cache
/nbproject/private/
/*.log
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) 2023 InitORM

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

Manage your database with or without abstraction. This library is built on the PHP PDO plugin and is mainly used to build and execute SQL queries.

[![Latest Stable Version](http://poser.pugx.org/initorm/orm/v)](https://packagist.org/packages/initorm/orm) [![Total Downloads](http://poser.pugx.org/initorm/orm/downloads)](https://packagist.org/packages/initorm/orm) [![Latest Unstable Version](http://poser.pugx.org/initorm/orm/v/unstable)](https://packagist.org/packages/initorm/orm) [![License](http://poser.pugx.org/initorm/orm/license)](https://packagist.org/packages/initorm/orm) [![PHP Version Require](http://poser.pugx.org/initorm/orm/require/php)](https://packagist.org/packages/initorm/orm)

## Requirements

- PHP 8.0 and later.
- PHP PDO extension.

## Supported Databases

This library should work correctly in almost any database that uses basic SQL syntax.
Databases supported by PDO and suitable drivers are available at [https://www.php.net/manual/en/pdo.drivers.php](https://www.php.net/manual/en/pdo.drivers.php).

## Installation

```
composer require initorm/orm
```

## Usage

### Model and Entity

Model and Entity; are two common concepts used in database abstraction. To explain these two concepts in the roughest way;

- **Model :** Each model is a class that represents a table in the database.
- **Entity :** Entity is a class that represents a single row of data.

The most basic example of a model class would look like this.

```php
namespace App\Model;

class Posts extends \InitORM\ORM\Model
{

/**
* If your model will use a connection other than your global connection, provide connection information.
* @var array|null <p>Default : NULL</p>
*/
protected array $credentials = [
'dsn' => '',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
];

/**
* If not specified, \InitORM\ORM\Entity::class is used by default.
*
* @var string<\InitORM\ORM\Entity>
*/
protected $entity = \App\Entities\PostEntity::class;

/**
* If not specified, the name of your model class is used.
*
* @var string
*/
protected string $schema = 'posts';

/**
* The name of the PRIMARY KEY column.
*
* @var string
*/
protected string $schemaId = 'id';

/**
* Specify FALSE if you want the data to be permanently deleted.
*
* @var bool
*/
protected bool $useSoftDeletes = true;

/**
* Column name to hold the creation time of the data.
*
* @var string|null
*/
protected ?string $createdField = 'created_at';

/**
* The column name to hold the last time the data was updated.
*
* @var string|null
*/
protected ?string $updatedField = 'updated_at';

/**
* Column name to keep deletion time if $useSoftDeletes is active.
*
* @var string|null
*/
protected ?string $deletedField = 'deleted_at';

protected bool $readable = true;

protected bool $writable = true;

protected bool $deletable = true;

protected bool $updatable = true;

}
```

The most basic example of a entity class would look like this.

```php
namespace App\Entities;

class PostEntity extends \InitORM\ORM\Entity
{
/**
* An example of a getter method for the "post_title" column.
*
* Usage :
* echo $entity->post_title;
*/
public function getPostTitleAttribute($title)
{
return strtoupper($title);
}

/**
* An example of a setter method for the "post_title" column.
*
* Usage :
* $entity->post_title = 'New Post Title';
*/
public function setPostTitleAttribute($title)
{
$this->post_title = strtolower($title);
}

}
```

## Getting Help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

## Getting Involved

> All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest.
There are two primary ways to help:

- Using the issue tracker, and
- Changing the code-base.

### Using the issue tracker

Use the issue tracker to suggest feature requests, report bugs, and ask questions. This is also a great way to connect with the developers of the project as well as others who are interested in this solution.

Use the issue tracker to find ways to contribute. Find a bug or a feature, mention in the issue that you will take on that effort, then follow the Changing the code-base guidance below.

### Changing the code-base

Generally speaking, you should fork this repository, make changes in your own fork, and then submit a pull request. All new code should have associated unit tests that validate implemented features and the presence or lack of defects. Additionally, the code should follow any stylistic and architectural guidelines prescribed by the project. In the absence of such guidelines, mimic the styles and patterns in the existing code-base.

## Credits

- [Muhammet ŞAFAK](https://www.muhammetsafak.com.tr) <<[email protected]>>

## License

Copyright &copy; 2023 [MIT License](./LICENSE)
24 changes: 24 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "initorm/orm",
"description": "InitORM ORM",
"keywords": ["php", "php8", "pdo", "database", "orm"],
"type": "library",
"license": "MIT",
"autoload": {
"psr-4": {
"InitORM\\ORM\\": "src/"
}
},
"authors": [
{
"name": "Muhammet ŞAFAK",
"email": "[email protected]"
}
],
"minimum-stability": "stable",
"require": {
"php": ">=8.0",
"ext-pdo": "*",
"initorm/database": "^1.0"
}
}
140 changes: 140 additions & 0 deletions src/Entity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php
/**
* InitORM ORM
*
* This file is part of InitORM ORM.
*
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2023 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);
namespace InitORM\ORM;

use InitORM\ORM\Exceptions\EntityException;
use InitORM\ORM\Interfaces\EntityInterface;
use InitORM\ORM\Utils\Helper;

class Entity implements EntityInterface
{

protected array $__attributes = [];

protected array $__attributesOriginal = [];

public function __construct(?array $data = [])
{
$this->setUp($data);
}

/**
* @param $name
* @param $arguments
* @return mixed
* @throws EntityException
*/
public function __call($name, $arguments)
{
if (str_ends_with($name, 'Attribute') === false) {
throw new EntityException($name);
}

$attr = Helper::camelCaseToSnakeCase(substr($name, 3, -9));

return match (substr($name, 0, 3)) {
'get' => $this->__attributes[$attr] ?? null,
'set' => $this->__attributes[$attr] = $arguments[0],
default => throw new EntityException($name)
};
}

public function __set($name, $value)
{
$methodName = 'set' . Helper::snakeCaseToPascalCase($name) . 'Attribute';
if(method_exists($this, $methodName)){
$this->{$methodName}($value);
return $value;
}
return $this->__attributes[$name] = $value;
}

public function __get($name)
{
$methodName = 'get' . Helper::snakeCaseToPascalCase($name) . 'Attribute';
if(method_exists($this, $methodName)){
return $this->{$methodName}();
}
return $this->__attributes[$name] ?? null;
}

public function __isset($name)
{
return isset($this->__attributes[$name]);
}

public function __unset($name)
{
if(isset($this->__attributes[$name])){
unset($this->__attributes[$name]);
}
}

public function __debugInfo()
{
return $this->__attributes;
}

/**
* @inheritDoc
*/
public function toArray(): array
{
return $this->__attributes;
}

/**
* @inheritDoc
*/
public function getAttributes(): array
{
return $this->toArray();
}

/**
* @param array|null $data
* @return $this
*/
protected function setUp(?array $data = null): self
{
$this->syncOriginal()
->fill($data);
return $this;
}

/**
* @param array|null $data
* @return $this
*/
protected function fill(?array $data = null): self
{
if($data !== null){
foreach ($data as $key => $value) {
$this->__set($key, $value);
}
}
return $this;
}

/**
* @return $this
*/
protected function syncOriginal(): self
{
$this->__attributesOriginal = $this->__attributes;
return $this;
}

}
19 changes: 19 additions & 0 deletions src/Exceptions/DeletableException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
/**
* InitORM ORM
*
* This file is part of InitORM ORM.
*
* @author Muhammet ŞAFAK <[email protected]>
* @copyright Copyright © 2023 Muhammet ŞAFAK
* @license ./LICENSE MIT
* @version 1.0
* @link https://www.muhammetsafak.com.tr
*/

declare(strict_types=1);
namespace InitORM\ORM\Exceptions;

class DeletableException extends ModelException
{
}
Loading

0 comments on commit 7a3e592

Please sign in to comment.