-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from miamibc/database-orm
Database orm
- Loading branch information
Showing
47 changed files
with
1,100 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
/** | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Joker\Database; | ||
|
||
use RedisClient\RedisClient; | ||
|
||
trait Redis | ||
{ | ||
|
||
private $redis_cache; | ||
|
||
private function getRedisKey() | ||
{ | ||
$class = explode('\\', get_class( $this ) ); | ||
$class = strtolower(end($class)); | ||
return "joker/$class/". $this->id(); | ||
} | ||
|
||
public function getRedis() | ||
{ | ||
// is already loaded, return it | ||
if (!is_null($this->redis_cache)) | ||
return $this->redis_cache; | ||
|
||
$class = explode('\\', get_class( $this ) ); | ||
$class = strtolower(end($class)); | ||
|
||
$client = new RedisClient(); | ||
$json = $client->get("joker/$class/". $this->id()); | ||
$object = is_null($json) ? new \stdClass() : json_decode( $json ); | ||
return $this->redis_cache = $object; | ||
} | ||
|
||
public function saveRedis() | ||
{ | ||
if (!is_null($this->redis_cache)) | ||
{ | ||
$class = explode('\\', get_class( $this ) ); | ||
$class = strtolower(end($class)); | ||
|
||
$client = new RedisClient(); | ||
$client->set( $this->getRedisKey() , json_encode($this->redis_cache)); | ||
} | ||
return $this; | ||
} | ||
|
||
public function cleanRedis() | ||
{ | ||
$client = new RedisClient(); | ||
$client->del( $this->getRedisKey() ); | ||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
/** | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Joker\Database; | ||
|
||
use RedBeanPHP\R; | ||
|
||
trait Sqlite | ||
{ | ||
|
||
private $sqlite_cache; | ||
|
||
public function getCustom() | ||
{ | ||
// is already loaded, return it | ||
if (!is_null($this->sqlite_cache)) | ||
return $this->sqlite_cache; | ||
|
||
// try to read from database | ||
// table name is last word in class signature | ||
$class = explode('\\', get_class( $this ) ); | ||
$class = strtolower(end($class)); | ||
|
||
// try to find by uuid | ||
if (!$item = R::findOne( $class, 'uuid =?', [ $this->id() ])) | ||
{ | ||
// if not found, create | ||
$item = R::dispense( $class ); | ||
$item->uuid = $this->id(); | ||
} | ||
|
||
// store in cache and return | ||
return $this->sqlite_cache = $item; | ||
} | ||
|
||
public function saveCustom() | ||
{ | ||
if (!is_null($this->sqlite_cache)) | ||
R::store($this->sqlite_cache); | ||
|
||
return $this; | ||
} | ||
|
||
public function cleanCustom() | ||
{ | ||
if (!is_null($this->sqlite_cache)) | ||
R::trash($this->sqlite_cache); | ||
|
||
return $this; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Telegram Bot API parser for Joker | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Joker\Parser; | ||
|
||
/** | ||
* This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound). | ||
* @see https://core.telegram.org/bots/api#animation | ||
* | ||
* @method string file_id() Identifier for this file, which can be used to download or reuse the file | ||
* @method string file_unique_id() Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file. | ||
* @method integer width() Video width as defined by sender | ||
* @method integer height() Video height as defined by sender | ||
* @method integer duration() Duration of the video in seconds as defined by sender | ||
* @method PhotoSize thumb() Optional. Animation thumbnail as defined by sender | ||
* @method string file_name() Optional. Original animation filename as defined by sender | ||
* @method string mime_type() Optional. MIME type of the file as defined by sender | ||
* @method integer file_size() Optional. File size | ||
*/ | ||
class Animation extends Base | ||
{ | ||
|
||
protected $wrapper = [ | ||
'thumb' => PhotoSize::class, | ||
]; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
/** | ||
* Telegram Bot API parser for Joker | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Joker\Parser; | ||
|
||
/** | ||
* This object represents an animation file (GIF or H.264/MPEG-4 AVC video without sound). | ||
* @see https://core.telegram.org/bots/api#audio | ||
* | ||
* @method string file_id() Identifier for this file, which can be used to download or reuse the file | ||
* @method string file_unique_id() Unique identifier for this file, which is supposed to be the same over time and for different bots. Can't be used to download or reuse the file. | ||
* @method integer duration() Duration of the audio in seconds as defined by sender | ||
* @method string performer() Optional. Performer of the audio as defined by sender or by audio tags | ||
* @method string title() Optional. Title of the audio as defined by sender or by audio tags | ||
* @method string file_name() Optional. Original filename as defined by sender | ||
* @method string mime_type() Optional. MIME type of the file as defined by sender | ||
* @method integer file_size() Optional. File size | ||
* @method PhotoSize thumb() Optional. Thumbnail of the album cover to which the music file belongs | ||
*/ | ||
class Audio extends Base | ||
{ | ||
|
||
protected $wrapper = [ | ||
'thumb' => PhotoSize::class, | ||
]; | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
/** | ||
* Telegram Bot API parser for Joker | ||
* | ||
* @package joker-telegram-bot | ||
* @author Sergei Miami <[email protected]> | ||
*/ | ||
|
||
namespace Joker\Parser; | ||
|
||
|
||
class Base | ||
{ | ||
|
||
protected $data = []; // incoming data array | ||
protected $cache = []; // wrapped objects cache | ||
protected $wrapper = []; // array or wrappers | ||
|
||
public function __construct( $data ) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
/** | ||
* Get data, wrapped if possible | ||
* @param $key | ||
* | ||
* @return false|mixed | ||
*/ | ||
public function __call($key, $arguments) | ||
{ | ||
// no data with this key, return false | ||
if (!isset($this->data[$key])) return false; | ||
|
||
// no wrapper for this key, return data no need to cache | ||
if (!isset($this->wrapper[$key])) return $this->data[$key]; | ||
|
||
// cache exists, return it | ||
if (isset($this->cache[$key])) return $this->cache[$key]; | ||
|
||
// wrap and save to cache | ||
$wrapper = $this->wrapper[$key]; | ||
$data = $this->data[$key]; | ||
|
||
// data is sequental array, result will be array of wrapped elements | ||
if (is_array( $data ) && array_keys($data) === range(0, count($data) - 1)) | ||
{ | ||
$result = array_map(function ( $item ) use ($wrapper){ | ||
return new $wrapper( $item ); | ||
}, $data); | ||
} | ||
// all other types of data, just wrap it | ||
else | ||
{ | ||
$result = new $wrapper($data); | ||
} | ||
return $this->cache[$key] = $result; | ||
} | ||
|
||
public function getData() | ||
{ | ||
return $this->data; | ||
} | ||
|
||
} |
Oops, something went wrong.