Skip to content

Latest commit

 

History

History
200 lines (148 loc) · 3.48 KB

01-usage.md

File metadata and controls

200 lines (148 loc) · 3.48 KB

Using Tools

Installation

olive-cms/tools is available on Packagist (olive-cms/tools) and as such installable via Composer.

composer require olive-cms/tools

If you do not use Composer, you can grab the code from GitHub, and use any PSR-0 compatible autoloader (e.g. the Symfony2 ClassLoader component) to load Monolog classes.

Basic Usage

// load autoload
require_once 'vendor/autoload.php';
use Olive\Tools;

Items

File

/**
 * file tools
 * last argument type stream file -> default: 'w'
 * @return bool
 */

Tools::file('/path/to/file', 'content');
Tools::file('/path/to/file', 'content', 'a');

Get Dir List

Get Directorys of path

// @return array

$list = Tools::getDirList('/path/to/directory/');

Get Dirs List

Get all Directorys of path (Recursive)

// @return array

$list = Tools::getDirsList('/path/to/directory/');

Get Dir Files List

Get Files Directorys of path

// @return array

$list = Tools::getDirFiles('/path/to/directory/');

Remove Directorys

Remove not empty Directory

// @return void

Tools::rmDir('/path/to/directory/');

Json Encode

Json encode with pretty

// @return string

$json = Tools::jsonEncode(['ali', 'mehdi']);

Get Json File

Json encode with pretty

// @return array | object

$array = Tools::getJsonFile('/path/to/file.json');
$object = Tools::getJsonFile('/path/to/file.json', false);

Array Merge

Merge arrays with compare

// @return array | object

$a=[
  'arr1'=>[
    'arr11'=>[1,2,3,4,5,6],
    'arr12'=>[7,8,9,10,11]
  ],
  'str1'=>'tools'
];
$b=[
  'arr1'=>[
    'arr11'=>[11,22,33],
    'arr12'=>[
      'op' => [111,222,333],
      'op2' => 'lurem'
    ]
  ],
  'str2'=>'merge'
];
$merged=Tools::arrayMerge($a, $b));

output:

Array
(
    [arr1] => Array
        (
            [arr11] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                    [3] => 4
                    [4] => 5
                    [5] => 6
                    [6] => 11
                    [7] => 22
                    [8] => 33
                )

            [arr12] => Array
                (
                    [op] => Array
                        (
                            [0] => 111
                            [1] => 222
                            [2] => 333
                        )

                    [op2] => lurem
                    [0] => 7
                    [1] => 8
                    [2] => 9
                    [3] => 10
                    [4] => 11
                )

        )

    [str2] => merge
    [str1] => tools
)

Run Callable Methods

Run Callable Methods (functions, oops, string)

// return mixed

class Hello
{
    public function say($name)
    {
        return 'hello ' . $name;
    }
}

$testob = new Hello;
echo Tools::runCaller([$testob, 'say'], ['world']); // Hello world