Skip to content

Development: DataFromPathHelper Component

Sami Mokaddem edited this page Jan 13, 2021 · 2 revisions

Overview

The DataFromPathHelper.php provides function to replace strings (or array of strings) with data extracted from a cakephp's utility Hash path

Usage

  1. buildStringFromDataPath Allows the creation of a string using raw value, datapath or a function
$url = $this->DataFromPath->buildStringFromDataPath(
    'https://localhost/{{0}}/{{1}}.{{2}}',
    ['User' => ['id' => 42]],
    [
        ['raw' => 'users/view'],
        ['datapath' => 'User.id'],
        ['function' => function($data, $dataPath) { return 'json'; }]
    ]
);
// 'https://localhost/users/view/42.json'
  1. buildStringsInArray Applies buildStringFromDataPath for each elements in the provided array
$myArray = $this->DataFromPath->buildStringsInArray(
    [
        'string1' => __('Is this the {{0}} life?'),
        'string2' => __('Is this {{0}} {{1}}?'),
    ],
    ['Queen' => ['text1' => 'real']],
    [
        'string1' => ['Queen.text1'],
        'string2' => [
            ['raw' => 'just'],
            [
                'text' => 'fantasy',
                'function' => function($data, $dataPath) { return $dataPath['text']; }
            ]
        ],
    ]
);
// [
//     'string1' => 'Is this the real life?',
//     'string2' => 'Is this just fantasy?',
// ]
Clone this wiki locally