PHP code example of jeyroik / extas-patterns

1. Go to this page and download the library: Download jeyroik/extas-patterns 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/ */

    

jeyroik / extas-patterns example snippets


$data = [
    'level1' => [
        'level2' => [
            'level3' => 'enough'
        ],
        'level2.1' => 'stop'
    ],
    'level1.1' => 'break'
];

namespace my\extas\interafces\extensions;

interface ILevelsPatternExtension
{
    public function getLevel1(): array;
    public function getLevel1_1(): string;
    public function getLevel2(): array;
    public function getLevel2_1(): string;
    public function getLevel3(): string;
}

namespace my\extas\components\extensions;

use extas\components\extensions\Extension;
use extas\interfaces\patterns\IPattern;

class LevelsPatternExtension extends Extension implements ILevelsPatternExtension
{
    public function getLevel1(IPattern $pattern = null)
    {
        $schema = $pattern->getImportedData();
        return $schema['level1'] ?? [];
    }
    
    public function getLevel1_1(IPattern $pattern)
    {
        $schema = $pattern->getImportedData();
        return $schema['level1.1'] ?? '';
    }
    
    public function getLevel2(IPattern $pattern = null)
    {
        $level1 = $this->getLevel1($pattern);
        return $level1['level2'] ?? [];
    }
    
    public function getLevel2_1(IPattern $pattern = null)
    {
        $level1 = $this->getLevel1($pattern);
        return $level1['level2.1'] ?? '';
    }
    
    public function getLevel3(IPattern $pattern = null)
    {
        $level2 = $this->getLevel2($pattern);
        return $level2['level3'] ?? '';
    }
}

use \extas\components\patterns\Pattern;

$data = [
    'level1' => [
        'level2' => [
            'level3' => 'enough'
        ],
        'level2.1' => 'stop'
    ],
    'level1.1' => 'break'
];

$pattern = new Pattern([Pattern::FIELD__NAME => 'levels', Pattern::FIELD__SCHEMA => $data]);
echo $pattern->getLevel3(); // 'enough'