PHP code example of apdev / lawn-mower

1. Go to this page and download the library: Download apdev/lawn-mower 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/ */

    

apdev / lawn-mower example snippets




use LawnMower\Request;


$request = new Request();

if($request->hasData()){ //or hasPost(), hasGet(), hasFiles()

    $valid_data = $request->validate([
        'field_name' => 'rule_name|other_rule_name:with_param|rule_name',
        'other_field' => [
            'rule_name',
            'some_other_rule',
            $rule_instance, //instance of LawnMower\Rule::class
            ...
        ],
        ...
    ]);


    if($request->isValid()){
        //do stuff
    }else{
        $errors = $request->errors();
        $input = $request->all();
        $specific_fields = $request->only([ 'field_name', 'some_other_field', ... ]);
    }
}



$valid_data = $request->validate([
    'file_field' => '


$valid_data = $request->validate([
    'file_field' => [ '


$valid_data = $request->validate([
    'file_field' => [
        '),
    ],
]);




use LawnMower\Rule;

class MyRule extends Rule {

    protected $error_message = "###FIELD### has my custom error message, with ###PARAMS### too.";

    public function isValid():bool {

        echo $this->value; //value to be checked
        echo $this->params; //array of params passed by constructor

        //my stuff goes here

        return true; // or false
    }
}



...

//get instance of LawnMower\FileUpload::class
$upload = $valid_data['file_upload'];

$upload->isEmpty(); //checks if file is empty
$upload->getPath(); //returns full file path
$upload->getFilename(); //returns filename.ext without path
$stored_file = $upload->store("path/to/file/destination"); //stores file and returns a LawnMower\File::class instance;