DansMaculotte/laravel-prestashop-webservice

Laravel 5 用于 Prestashop Web 服务库的包装器

dev-master 2019-09-29 08:03 UTC

This package is auto-updated.

Last update: 2024-09-29 05:46:30 UTC


README

Latest Version Total Downloads Build Status Quality Score Code Coverage

Laravel 预设的 Prestashop Web 服务库包装器

安装

使用以下命令通过 composer 安装此包

composer require dansmaculotte/laravel-prestashop-webservice

最后,使用 artisan 命令发布配置文件

php artisan vendor:publish --provider="DansMaCulotte\PrestashopWebService\PrestashopWebServiceProvider"

配置

config/prestashop-webservice.php 打开已发布的配置文件

return [
    'url' => 'http://domain.com',
    'token' => '',
    'debug' => env('APP_DEBUG', false)
];

然后,将 url 字段填充为目标 Prestashop 安装的 根 URL,并将从 Prestashop 控制面板 Web 服务部分获取的 API 令牌填充到 token 字段。如果 debugtrue,则 Prestashop 在响应 API 请求时会返回调试信息。

用法

您可以使用两种方式使用 Prestashop Web 服务包装器

使用依赖关系或方法注入

...
use DansMaCulotte\PrestashopWebService\PrestashopWebService;

class FooController extends Controller
{
    private $prestashop;
    
    public function __construct(PrestashopWebService $prestashop)
    {
        $this->prestashop = $prestashop;
    }
    
    public function bar()
    {
        $opt['resource'] = 'customers';
        $xml = $this->prestashop->get($opt);
    }
}

使用外观

...
use Prestashop;

...

public function bar()
{   
    $opt['resource'] = 'customers';
    $xml = Prestashop::get($opt);
}

Prestashop 基础库使用

您可以在Prestashop 文档中找到有关 Prestashop Web 服务库的完整文档和教程。

辅助方法

我添加了一些辅助方法以减少开发时间

检索资源模式和填充数据

您可以通过调用 getSchema() 方法检索请求的资源模式。然后,您可以使用 fillSchema() 方法将关联数组的数据填充到模式中。

$xmlSchema = Prestashop::getSchema('categories'); //returns a SimpleXMLElement instance with the desired schema

$data = [
    'name' => 'Clothes',
    'link_rewrite' => 'clothes',
    'active' => true,
];

$postXml = Prestashop::fillSchema($xmlSchema, $data); 

//The xml is now ready for being sent back to the web service to create a new category

$response = Prestashop::add(['resource' => 'categories', 'postXml' => $postXml->asXml()]);

保留未填充节点以防止删除

fillSchema 方法的默认行为是删除未填充的节点。如果您想保留这些节点(典型的更新情况),请将第三个参数设置为 false

$putXml = Prestashop::fillSchema($xmlSchema, $data, false); 

删除特定节点

在保留未填充节点以防止删除时,您可以指定要删除的一些节点作为第四个参数(这在更新具有一些只读节点的资源时可能很有用,这会触发错误 400)

$putXml = Prestashop::fillSchema($xmlSchema, $data, false, ['manufacturer_name', 'quantity']);
//manufacturer_name and quantity only will be removed from the XML

处理语言值

如果节点有一个语言子节点,如果您的商店只安装了一种语言,您可以使用简单的字符串作为值。

/*
    xml node with one language child example
    ...
    <name>
    <language id="1"/>
    </name>
    ...
*/
$data = ['name' => 'Clothes'];

如果您的商店安装了多种语言,您可以将节点值作为数组传递,其中键是语言 ID。

/*
    xml node with n language children example 
    ...
    <name>
    <language id="1"/>
    <language id="2"/>
    </name>
    ... 
*/
$data = [
    'name' => [
        1 => 'Clothes',
        2 => 'Abbigliamento',
    ],
];

请注意,如果您不提供按语言 ID 键的值数组,则所有语言值都将具有相同的值。

处理具有多个兄弟的关联

如果您有一个具有多个关联的节点,如产品或类似产品的类别关联,如下所示的产品模式摘录

...
<associations>
<categories>
<category>
<id/>
</category>
</categories>
<product_features>
<product_feature>
<id/>
<id_feature_value/>
</product_feature>
</product_features>
...

您可以这样为 fillSchema 方法的数组数据映射做好准备

$data => [
    ...
    'associations' => [
        'categories' => [
            [ 'category' => ['id' => 4] ],
            [ 'category' => ['id' => 5] ],
            [ 'category' => ['id' => 11] ],
        ],
        'product_features' => [
            [
                'product_feature' => [
                    'id' => 5,
                    'id_feature_value' => 94,
                ],
            ],
            [
                'product_feature' => [
                    'id' => 1,
                    'id_feature_value' => 2,
                ],
            ],
        ],
    ],
];

结果将如预期的那样

...
<associations>
    <categories>
        <category>
            <id>4</id>
        </category>
        <category>
            <id>5</id>
        </category>
        <category>
            <id>11</id>
        </category>
    </categories>
    <product_features>
        <product_feature>
            <id>5</id>
            <id_feature_value>94</id_feature_value>
        </product_feature>
        <product_feature>
            <id>1</id>
            <id_feature_value>2</id_feature_value>
        </product_feature>
    </product_features>
...

测试

composer test

更改日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

许可

MIT 许可证(MIT)。有关更多信息,请参阅许可证文件