PHP code example of mazer / rewrite-dagger

1. Go to this page and download the library: Download mazer/rewrite-dagger 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/ */

    

mazer / rewrite-dagger example snippets




    function apiErrorResponse(string $errorMsg): void
    {
        header('Content-type: application/json; charset=utf-8');
        echo(json_encode([
            'data' => [
                'error' => true,
                'message' => $errorMsg
            ]
        ]));
        exit();
    }

 declare(strict_types=1);

    use PHPUnit\Framework\TestCase;
    use RewriteDagger\DaggerFactory;

    // Mock class that can sense and save function result
    class Mock
    {
        static $exitHasCalled = false;
        static $header = '';

        static public function exit(): void
        {
            self::$exitHasCalled = true;
        }

        static public function header(string $header): void
        {
            self::$header = $header;
        }
    }

    final class ApiErrorResponseTest extends TestCase
    {
        public function testApiErrorResponse(): void
        {
            $dagger = (new DaggerFactory())->getDagger();
            // add rewrite rule
            $dagger->addReplaceRule('exit', 'Mock::exit');
            $dagger->addReplaceRule('header', 'Mock::header');
            // 

$dagger->addDeleteRule('is a number.');

$dagger->addRegexDeleteRule('/\d+/');

$dagger->addReplaceRule('is a number', ': Answer to the Ultimate Question of Everything');

$dagger->addRegexReplaceRule('/\d+/', 'Number');

$dagger->addInsertBeforeRule('number', 'answer and ');

$dagger->addRegexInsertBeforeRule('/\d+/', '(Number) ');

$dagger->addInsertAfterRule('number', ' and answer');

$dagger->addRegexInsertAfterRule('/\d+/', ' (Number)');

$dagger->addRegexReplaceCallbackRule('/^(\d+).*(number)\.$/', function ($match) {
    return "[{$match[1]}] is a ({$match[2]}).";
});


    // default is use IncludeFileCodeRepository
    $dagger = (new DaggerFactory())->getDagger();

    // explicit use IncludeFileCodeRepository
    $dagger = (new DaggerFactory())->getDagger([
        'codeRepositoryType' => '


    class CustomDaggerFactory extends DaggerFactory
    {
        protected function initDagger(Dagger $dagger): Dagger
        {
            $dagger->addDeleteRule('exit()');
            return $dagger;
        }
    }

    // all dagger create by CustomDaggerFactory has a delete exit() rule by default
    $dagger = (new CustomDaggerFactory())->getDagger();

$ vendor/bin/phpunit ApiErrorResponseTest.php
PHPUnit 9.4.0 by Sebastian Bergmann and contributors.

Api Error Response
 ✔ Api error response

Time: 00:00.015, Memory: 6.00 MB

OK (1 test, 3 assertions)