pragmatic-modules/jslayout-parser

一个轻量级的PHP库,旨在使在Magento 2中与`$jsLayout`交互更加简洁,更面向对象。

1.0.0 2022-02-03 21:02 UTC

This package is auto-updated.

Last update: 2024-09-10 00:22:02 UTC


README

一个轻量级的独立PHP库,旨在使在Magento 2中与$jsLayout交互更加简洁,更面向对象。

安装

composer require pragmatic-modules/jslayout-parser

结账时的使用

通过实现LayoutProcessorInterface添加新的布局处理器,并将其注入到layoutProcessors数组中。

文件: etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Checkout\Block\Onepage">
        <arguments>
            <argument name="layoutProcessors" xsi:type="array">
                <item name="example_processor" xsi:type="object">Pragmatic\Example\Block\Checkout\ExampleProcessor
                </item>
            </argument>
        </arguments>
    </type>
</config>

Pragmatic\JsLayoutParser\Model\JsLayoutParser注入到处理器中,并为所选根组件解析$jsLayout

以下是可以使用的组件方法列表。

<?php
declare(strict_types=1);

namespace Pragmatic\Example\Block\Checkout;

use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Pragmatic\JsLayoutParser\Api\ComponentInterface;
use Pragmatic\JsLayoutParser\Model\JsLayoutParser;

class ExampleProcessor implements LayoutProcessorInterface
{
    /** @var JsLayoutParser */
    private $jsLayoutParser;

    public function __construct(JsLayoutParser $jsLayoutParser)
    {
        $this->jsLayoutParser = $jsLayoutParser;
    }

    public function process($jsLayout) : array
    {
        /** @var ComponentInterface $component */
        $component = $this->jsLayoutParser->parse($jsLayout, 'checkout');

        if ($shippingAddress = $component->getNestedChild('steps.shipping-step.shippingAddress')) {
            $shippingAddress->setComponent('Vendor_Module/js/view/shipping');
            $shippingAddress->setIsVisible(false);
        }
      
        $jsLayout['components']['checkout'] = $component->asArray();

        return $jsLayout;
    }
}

JsLayoutParser 方法

parse( array $jsLayout, string $rootComponent )

$jsLayout解析为嵌套组件对象树。

$rootComponent$jsLayout['components']中组件的名称,将用作任何操作的基准路径。

返回根Component对象。

组件方法

asArray( )

递归地将组件对象及其所有嵌套子组件转换为普通数组,可以通过$jsLayout返回。

示例

$component = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if ($shippingAddress = $component->getNestedChild('steps.shipping-step.shippingAddress')) {
    $shippingAddress->setComponent('Vendor_Module/js/view/shipping');
    $shippingAddress->setIsVisible(false);
}

$jsLayout['components']['checkout'] = $component->asArray();

向上滚动️

getComponentName( )

在布局中获取组件名称。

返回字符串。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');
$componentName = $checkout->getComponentName(); // returns 'checkout'

jsLayout等效

在关联的$jsLayout数组中,组件以嵌套键值对的形式存储,其中值是包含嵌套子组件的数组。在子作用域内无法确定父键。相同的数组可能在jsLayout的多个位置中使用,因此递归搜索不是一种选择。

换句话说,在检索组件之前必须知道其名称

$componentName = 'checkout';
$checkout = $jsLayout['components'][$componentName];

一旦进入子作用域,就没有办法动态地告诉父键是什么

$steps = $jsLayout['components']['checkout']['steps'];
// $steps tells you nothing about parent

要接近解析器的行为,可以这样做

$checkout = [
    'componentName' => 'checkout', 
    'data' => $jsLayout['components']['checkout']
];
$componentName = $checkout['componentName'] // 'checkout'

向上滚动️

getParent( )

获取父组件。

如果存在父组件,则返回 ComponentInterface

如果不存在父组件,则返回 NULL

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

$parent = $checkout->getParent(); // returns null

if($steps = $checkout->getChild('steps')) {
    $parent = $steps->getParent(); // returns $checkout object
}

jsLayout等效

$checkout = $jsLayout['components']['checkout'];
$parent = null;

if(isset($checkout['steps'])) {
    $steps = $checkout['steps'];
    $parent = $checkout;
}

向上滚动️

remove( )

从组件树中删除具有所有子组件的组件。

此方法没有返回值。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($steps = $checkout->getChild('steps')) {
    $steps->remove();
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['steps'])) {
    unset($jsLayout['components']['checkout']['steps']);
}

向上滚动️

hasChild ( string $componentName )

检查组件是否具有具有给定名称的子组件。

返回 bool。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($checkout->hasChild('steps')) {
    // do something
}

if($checkout->hasChild('non-existing-child')) {
    // this won't execute
}

jsLayout等效

if(isset($jsLayout['component']['checkout']['children']['steps'])) {
    // do something
}

if(isset($jsLayout['component']['checkout']['children']['non-existing-child'])) {
    // this won't execute
}

向上滚动️

getChild( string $componentName )

获取组件子组件。

如果子组件存在,则返回 ComponentInterface

如果子组件不存在,则返回 NULL

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($child = $checkout->getChild('steps')) {
    // do something with child
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']['steps'])) {
    $child = $jsLayout['components']['checkout']['children']['steps'];
    // do something with child
}

向上滚动️

addChild( ComponentInterface $component )

将新组件作为当前组件对象的子组件添加。

如果具有相同名称的组件已经是当前组件的子组件,则此方法抛出 Exception

成功时返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

/** @var \Pragmatic\JsLayoutParser\Model\ComponentFactory */
$child = $this->componentFactory->create([
    'componentName' => 'example',
    'data' => [
        'component' => 'Magento_Ui/js/form/element/abstract',
        'label' => 'Example component',
        'provider' => 'checkoutProvider'
    ]   
]);

if(!$checkout->hasChild('example')) {
    $checkout->addChild($child);
}

jsLayout等效

if(!isset($jsLayout['components']['checkout']['children']['example'])) {
    $jsLayout['components']['checkout']['children']['example'] = [
        'component' => 'Magento_Ui/js/form/element/abstract',
        'label' => 'Example component',
        'provider' => 'checkoutProvider'
    ];
}

向上滚动️

removeChild ( string $componentName )

从组件中删除子组件。

如果子组件在组件中不存在,则此方法抛出 Exception

此方法没有返回值。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($checkout->hasChild('steps')) {
    $checkout->removeChild('steps');
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']['steps'])) {
    unset($jsLayout['components']['checkout']['children']['steps']);
}

向上滚动️

hasNestedChild ( string $path, string $childSeparator = '.' )

检查组件是否具有具有给定路径的嵌套子组件。

默认情况下,子组件之间用点分隔。可以通过传递自定义分隔符作为第二个参数来调整此行为。

返回 bool。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($checkout->hasNestedChild('steps.shipping-step.shippingAddress')) {
    // do something
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress'])
) {
    // do something
}

向上滚动️

getNestedChild ( string $componentName, string $childSeparator = '.' )

获取组件嵌套子组件。

默认情况下,子组件之间用点分隔。可以通过传递自定义分隔符作为第二个参数来调整此行为。

如果嵌套子组件存在,则返回 ComponentInterface

如果嵌套子组件不存在,则返回 NULL

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($shippingAddress = $checkout->getNestedChild('steps.shipping-step.shippingAddress')) {
    // do something with $shippingAddress
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
)) {
    $shippingAddress = $jsLayout['components']['checkout']['children']
        ['steps']['children']
        ['shipping-step']['children']
        ['shippingAddress'];
    // do something with $shippingAddress
}

向上滚动️

moveNestedChild ( string $sourcePath, string $destinationPath, string $childSeparator = '.' )

将嵌套子组件从源移动到目标。

默认情况下,子组件之间用点分隔。可以通过传递自定义分隔符作为第三个参数来调整此行为。

如果源或目标不存在,则此方法抛出 Exception

此方法没有返回值。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($checkout->hasNestedChild('steps.shipping-step.shippingAddress') && 
   $checkout->hasChild('steps')
) {
    $checkout->moveNestedChild('steps.shipping-step.shippingAddress', 'steps');
}

$checkout->hasNestedChild('steps.shipping-step.shippingAddress'); // false
$checkout->hasNestedChild('steps.shippingAddress'); // true

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
) && isset($jsLayout['components']['checkout']['children']['steps'])
) {
    $steps = &$jsLayout['components']['checkout']['children']
    ['steps']['children'];
    $shippingAddress = $steps['shipping-step']['children']['shippingAddress'];
    unset($steps['shipping-step']['children']['shippingAddress']);
    $steps['shippingAddress'] = $shippingAddress;
}

isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
); // false

isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shippingAddress']
); // true

向上滚动️

removeNestedChild ( string $path, string string $childSeparator = '.' )

通过路径删除嵌套子组件。

默认情况下,子组件之间用点分隔。可以通过传递自定义分隔符作为第三个参数来调整此行为。

如果源或目标不存在,则此方法抛出 Exception

此方法没有返回值。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($checkout->hasNestedChild('steps.shipping-step.shippingAddress')) {
    $checkout->removeNestedChild('steps.shipping-step.shippingAddress');
}
$checkout->hasNestedChild('steps.shipping-step.shippingAddress'); // false

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
)) {
    unset($steps['shipping-step']['children']['shippingAddress']);
}

isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
); // false

向上滚动️

hasChildren ( )

检查组件是否有子组件。

如果至少存在一个子组件,则返回 true,否则返回 false。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

$checkout->hasChildren() // returns true

jsLayout等效

(isset($jsLayout['components']['checkout']['children']) && 
count($jsLayout['components']['checkout']['children']) > 0); // returns true

向上滚动️

getChildren ( )

获取组件子组件。

返回组件数组。

如果组件没有子组件,则返回空数组。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

$checkout->getChildren(); // returns array with 'steps' component

jsLayout等效

$jsLayout['components']['checkout']['children'] ?? [] // returns array with 'steps' component

向上滚动️

isChildOf ( ComponentInterface $component )

检查组件是否是给定组件的子组件。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

$steps = $checkout->getChild('steps');
$steps->isChildOf($checkout); // returns true

jsLayout等效

在关联的 $jsLayout 数组中,组件作为嵌套键值对存储,其中值是包含嵌套子数组的数组。在子组件范围内无法确定父组件。

向上滚动️

getComponent ( )

获取给定组件对象的 UI 组件。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($shippingAddress = $checkout->getNestedChild('steps.shipping-step.shippingAddress')) {
    $component = $shippingAddress->getComponent(); // returns 'Magento_Checkout/js/view/shipping'
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
)) {
    $shippingAddress = $jsLayout['components']['checkout']['children']
        ['steps']['children']
        ['shipping-step']['children']
        ['shippingAddress'];
    $component = $shippingAddress['component'] // 'Magento_Checkout/js/view/shipping'
}

向上滚动️

setComponent ( string $component )

设置给定组件对象的 UI 组件。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($shippingAddress = $checkout->getNestedChild('steps.shipping-step.shippingAddress')) {
    $shippingAddress->setComponent('Vendor_Module/js/view/shipping')
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']
)) {
    $shippingAddress = &$jsLayout['components']['checkout']['children']
        ['steps']['children']
        ['shipping-step']['children']
        ['shippingAddress'];
    $shippingAddress['component'] = 'Vendor_Module/js/view/shipping';
}

向上滚动️

getConfig ( )

获取组件配置。

返回数组。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
    $config = $regionId->getConfig();
    /** $config is an array:
        [
            'customScope' => 'shippingAddress',
            'template' => 'ui/form/field',
            'elementTmpl' => 'ui/form/element/select',
            'customEntry' => 'shippingAddress.region',
        ]
    */
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']
)) {
    $regionId = &$jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id'];

    $config = $regionId['config'];
}

向上滚动️

setConfig ( array $config, bool $replace = false )

设置组件配置。

默认情况下,配置会合并在一起。之前的值保持不变,除非它们与 $config 数组中的键相同。

$config 的值具有更高的优先级,可以用来替换现有配置的部分。

使用 $replace = true 可以通过 $config 覆盖整个组件配置。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
    $config = $shippingAddress->setConfig([
        'template' => 'Vendor_Module/form/field'
    ]);
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']
)) {
    $regionId = &$jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id'];

    $regionId['config']['template'] = 'Vendor_Module/form/field';
}

向上滚动️

getDataScope ( )

获取组件数据作用域。

返回字符串或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
    $dataScope = $regionId->getDataScope(); // returns 'shippingAddress.region_id'
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']
)) {
    $dataScope = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']['dataScope'] ?? null; // 'shippingAddress.region_id'
}

向上滚动️

setDataScope ( string $dataScope )

设置组件数据作用域。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
    $regionId->setDataScope('shippingAddress.some_region');
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']['dataScope'] = 'shippingAddress.some_region';
}

向上滚动️

getDisplayArea ( )

获取组件显示区域。

返回字符串或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($shippingFieldset = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset')) {
    $displayArea = $shippingFieldset->getDisplayArea(); // returns 'additional-fieldsets'
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']
)) {
    $displayArea = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['displayArea'] ?? null; // 'additional-fieldsets'
}

向上滚动️

setDisplayArea ( string $displayArea )

设置组件显示区域。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($shippingFieldset = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset')) {
    $shippingFieldset->setDisplayArea('summary');
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['displayArea'] = 'summary';
}

向上滚动️

getLabel ( )

获取组件标签。

返回字符串、Magento\Framework\Phrase 或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $label = $postcode->getLabel(); // returns 'Zip/Postal Code'
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $label = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['label'] ?? null;  // 'Zip/Postal Code'
}

向上滚动️

setLabel ( $label )

设置组件标签。

$label 应该是字符串、Magento\Framework\Phrase 或 null。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $label = $postcode->setLabel(__('ZIP'));
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['label'] = __('ZIP');
}

向上滚动️

getProvider ( )

获取组件提供者。

返回字符串或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $provider = $postcode->getProvider(); // returns 'checkoutProvider'
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $provider = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['provider'] ?? null; // 'checkoutProvider'
}

向上滚动️

setProvider ( string $provider )

设置组件提供者。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $postcode->setProvider('vendorProvider');
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['provider'] = 'vendorProvider';
}

向上滚动️

getSortOrder ( )

获取组件排序顺序。

返回字符串,或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $sortOrder = $postcode->getSortOrder(); // returns '110'
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $sortOrder = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['sortOrder'] ?? null; // '110'
}

向上滚动️

setSortOrder ( string $sortOrder )

设置组件排序顺序。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $postcode->setSortOrder('150');
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['sortOrder'] = '150';
}

向上滚动️

getValidation ( )

获取组件验证规则。

返回数组或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $validation = $postcode->getValidation(); // returns ['required-entry' => true]
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $validation = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['validation'] ?? null;  // ['required-entry' => true]
}

向上滚动️

setValidation ( array $validation )

设置组件验证规则。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $postcode->setValidation([
        'required-entry' => true,
        'max_text_length' => 6
    ]);
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $validation = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['validation'] = [
        'required-entry' => true,
        'max_text_length' => 6
    ];
}

向上滚动️

getFilterBy ( )

通过配置获取组件过滤器。

返回数组或 null。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
    $filterBy = $regionId->getFilterBy(); 
// returns 
//    [
//        'target' => '${ $.provider }:${ $.parentScope }.country_id',
//        'field' => 'country_id',
//    ]
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']
)) {
    $filterBy = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']['filterBy'] ?? null;
}

向上滚动️

setFilterBy ( ?array $filterBy )

通过配置设置组件过滤器。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($regionId = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.region_id')) {
    $regionId->setFilterBy(null); 
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['region_id']['filterBy'] = null;
}

向上滚动️

isVisible ( )

获取组件可见性。

返回 bool。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $isVisible = $postcode->isVisible(); // returns true
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $isVisible = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['visible'] ?? false; // true
}

向上滚动️

setIsVisible ( bool $visible )

设置组件可见性。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($postcode = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.postcode')) {
    $postcode->setIsVisible(false);
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['postcode']['visible'] = false;
}

向上滚动️

isRequired ( )

获取组件 required 标志。

返回 bool。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($street = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.street')) {
    $isRequired = $street->isRequired(); // returns true
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['street']
)) {
    $isRequired = $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['street']['required'] ?? false; // true
}

向上滚动️

setIsRequired ( bool $required )

设置组件 required 标志。

返回 self。

示例

$checkout = $this->jsLayoutParser->parse($jsLayout, 'checkout');

if($street = $checkout->getNestedChild('steps.shipping-step.shippingAddress.shipping-address-fieldset.street')) {
    $street->setIsRequired(false);
}

jsLayout等效

if(isset($jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['street']
)) {
    $jsLayout['components']['checkout']['children']
    ['steps']['children']
    ['shipping-step']['children']
    ['shippingAddress']['children']
    ['shipping-address-fieldset']['children']
    ['street']['required'] = false;
}

向上滚动️