Skip to content

Instantly share code, notes, and snippets.

@yidemir
yidemir / simple_validator.php
Last active November 25, 2020 20:20
Simple array validator
<?php
function validate(array $data, string $key, $rules)
{
if (is_string($rules)) {
$rules = explode('|', $rules);
}
$normalizeRule = function (string $rule) {
$normalizedRule = [];
@yidemir
yidemir / helpers.php
Last active November 8, 2018 16:10
PHP array dot notation setter and getter
<?php
/**
* @param mixed $default
* @return mixed
*/
function array_get(array $config, string $dotPath, $default = null)
{
if (key_exists($dotPath, $config)) {
return $config[$dotPath];
@yidemir
yidemir / app.php
Last active February 16, 2018 15:46
<?php
class app
{
/**
* @var array
*/
protected $app = [
'routes' => [],
'groups' => [],
@yidemir
yidemir / lang.php
Last active October 3, 2017 10:17
Basit çoklu dil fonksiyonu/örneği
<?php
function lang($phrase, $lang = 'tr')
{
$phrase = explode('.', $phrase);
$file = array_shift($phrase);
$phrase = implode('.', $phrase);
$path = __DIR__ . "/languages/$lang/$file.php";
if (is_file($path)) {
@yidemir
yidemir / qs.php
Last active August 24, 2017 08:31
QueryString
<?php
// istek foo.com/?/post/view/5
$qs = $_SERVER['QUERY_STRING']; // /post/view/5
$qs = explode('/', $qs); // Array ( [0] => [1] => post [2] => view [3] => 5 )
array_shift($qs); // boş olan ilk öğeyi dizeden alıyoruz/siliyoruz
print_r($qs); // Array ( [0] => post [1] => view [2] => 5 )
<?php
function generateNewSlug($slug)
{
$newSlug = explode('-', $slug);
if (is_numeric(end($newSlug))) {
$last = end($newSlug);
array_pop($newSlug);
array_push($newSlug, ++$last);
@yidemir
yidemir / PostModel.php
Last active September 1, 2015 12:04
PostModel.php
<?php
class Post extends Model
{
public function nextPost($id)
{
$sql = 'SELECT * FROM posts WHERE id > ? ORDER BY id ASC LIMIT 1';
$sth = $this->db->prepare($sql);
$sth->execute(array($id));
return $sth->fetch();
<?php
/**
* Models/PostModel.php
* Örnek bir model katmanı
*/
class PostModel
{
public $pdo;
<?php
/**
* Controllers/Home.php
* Home Controller'ı
*/
class Home
{
public function index()
{
<?php
require 'Controller.php';
$controller = new Controller;