PHP code example of arefshojaei / kitdash
1. Go to this page and download the library: Download arefshojaei/kitdash library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
arefshojaei / kitdash example snippets
use Kit\Support\Arr;
Arr::add(["a" => 1], "b", 2); // ['a' => 1, 'b' => 2]
Arr::get(["a" => 1], "a"); // 1
Arr::take([1, 2, 3, 4, 5], 3); // [1, 2, 3]
Arr::nth([10, 20, 30], 1); // 20
Arr::drop([1, 2, 3], 1); // [1, 3] or shifted
Arr::compact([0, "", false, "hello"]); // ['hello']
Arr::except(["a" => 1, "b" => 2], "a"); // ['b' => 2]
Arr::first([1, 2, 3]); // 1
Arr::last([1, 2, 3]); // 3
Arr::only(["a" => 1, "b" => 2], ["a"]); // ['a' => 1]
Arr::fill([1, 2, 3], "x"); // ['x', 'x', 'x']
Arr::sort([3, 1, 4]); // [1, 3, 4]
Arr::unique([1, 2, 2, 3]); // [1, 2, 3]
Arr::concat([1, 2], [3, 4]); // merged array
Arr::random([1, 2, 3, 4]); // random element
Arr::chunk([1, 2, 3, 4], 2); // [[1,2],[3,4]]
Arr::join(["A", "B"], " "); // "A B"
Arr::difference([1, 2, 3], [3, 4]); // [1, 2]
use Kit\Support\Str;
Str::upper("hello"); // HELLO
Str::lower("HELLO"); // hello
Str::title("hello world"); // Hello World
Str::snake("fooBar"); // foo_bar
Str::kebab("fooBar"); // foo-bar
Str::camel("foo_bar"); // fooBar
Str::limit("hello world", 5); // hello...
Str::slug("My Awesome Post"); // My-Awesome-Post
Str::contains("hello world", "world"); // true
Str::startsWith("hello", "he"); // true
Str::endsWith("hello", "lo"); // true
Str::toBase64("hello"); // aGVsbG8=
Str::e("<script>"); // <script>
Str::split("a,b,c", ","); // ['a', 'b', 'c']
Str::repeat("ha", 3); // hahaha
Str::reverse("hello"); // olleh
use Kit\Support\Binary;
$binary = Binary::create("secret-key");
$encoded = $binary->encode("Hello");
$decoded = $binary->decode($encoded); // Hello
use Kit\Structure\Stack;
$stack = new Stack();
$stack->push("first");
$stack->push("second");
$stack->pop(); // 'second'
$stack->isEmpty(); // false
$stack->toArray(); // ['first']
use Kit\Structure\Queue;
$queue = new Queue();
$queue->enqueue("first");
$queue->enqueue("second");
$queue->dequeue(); // 'first'
$queue->toArray(); // ['second']
use Kit\Structure\HashTable;
$table = new HashTable();
$table->set("user_1", "John");
$table->get("user_1"); // John
$table->has("user_1"); // true
use Kit\Structure\Tree\Tree;
$tree = new Tree("Root");
$tree->add("Child 1");
$tree->add("Child 2");
$tree->toArray();
use Kit\Structure\Graph\Graph;
$graph = new Graph();
$node1 = $graph->addNode("A");
$node2 = $graph->addNode("B");
$graph->addEdge($node1, $node2);
$graph->getNode("A");
use Kit\Fs\File;
use Kit\Fs\Directory;
use Kit\Fs\Archive;
// File
File::save("file.txt", "content");
File::get("file.txt");
File::has("file.txt");
File::append("file.txt", " more");
File::delete("file.txt");
File::copy("a.txt", "b.txt");
File::size("file.txt");
File::extension("file.txt"); // txt
File::hash("file.txt"); // sha256
// Directory
Directory::create("path/to/dir");
Directory::files("path");
Directory::directories("path");
Directory::clean("path");
Directory::delete("path");
Directory::size("path");
Directory::isEmpty("path");
// Archive (Zip)
$archive = new Archive("backup.zip");
$archive->addFile("file.txt");
$archive->addFromString("readme.md", "# Hello");
$archive->addDirectory("src");
$archive->comment("Backup");
$archive->extract("output");
$archive->close();
use Kit\Net\Url;
use Kit\Net\Request;
// URL Parser
$url = Url::create("https://example.com/path?name=Aref");
$url->host(); // example.com
$url->protocol(); // https
$url->path(); // /path?name=Aref
$url->query(); // ['name' => 'Aref']
$url->origin(); // https://example.com
// HTTP Request
$posts = Request::get("https://jsonplaceholder.typicode.com/posts");
$post = Request::post("https://jsonplaceholder.typicode.com/posts", [
"title" => "New Post",
"body" => "Content",
]);
use Kit\Json\Json;
$json = Json::encode(["name" => "Aref", "age" => 25]);
$data = Json::decode($json); // object
$data = Json::decode($json, true); // array
use Kit\Support\{Arr, Str};
$input = [
"name" => " John Doe ",
"email" => "[email protected] ",
"description" => "",
"website" => null,
];
// Clean input
$input["name"] = Str::trim($input["name"]);
$input["email"] = Str::lower($input["email"]);
// Remove empty values
$clean = Arr::compact($input);
// Result: ['name' => 'John Doe', 'email' => '[email protected] ']
use Kit\Support\Str;
$title = "How to Build a PHP Application";
// Create URL-friendly slug
$slug = Str::slug($title); // 'how-to-build-a-php-application'
// Create filename
$filename = $slug . ".md"; // 'how-to-build-a-php-application.md'
// Check URL patterns
$isValid = Str::startsWith("https://example.com/api", "https://");
use Kit\Support\Arr;
$config = [
"app" => ["name" => "MyApp", "version" => "1.0"],
"db" => ["host" => "localhost", "port" => 3306],
"cache" => ["driver" => "redis"],
];
// Get specific values
$appName = Arr::get($config, "app"); // Using array keys
$dbHost = Arr::get($config, "db");
// Add new config
$newConfig = Arr::add($config, "queue", ["driver" => "redis"]);
// Get only app config
$appConfig = Arr::only($config["app"], ["name", "version"]);
use Kit\Fs\File;
// Save configuration
$config = ["app" => "MyApp", "debug" => true];
File::save("config.json", json_encode($config));
// Read configuration
if (File::has("config.json")) {
$content = File::get("config.json");
$config = json_decode($content, true);
}
// Create backup
$original = File::get("data.txt");
File::save("data.backup.txt", $original);
use Kit\Structure\{Stack, HashTable};
// Undo/Redo functionality with Stack
$stack = new Stack();
$stack->push("action1");
$stack->push("action2");
$stack->push("action3");
$lastAction = $stack->pop(); // 'action3'
// Cache with HashTable
$cache = new HashTable();
$cache->set("user:1", ["name" => "John", "email" => "[email protected] "]);
$cache->set("user:2", ["name" => "Jane", "email" => "[email protected] "]);
$user = $cache->get("user:1");
$hasUser = $cache->has("user:1"); // true
bash
vendor/bin/phpunit tests/Unit/Support/ArrTest.php