如何将faker数据结果传递给自定义函数

时间:2014-09-29 16:44:01

标签: php symfony fixtures faker

我使用PhoneNumberBundle验证我的应用中的电话号码。我还使用了NelmioAliceBundleAliceFixtureBundle。以此作为起点我正在为具有PhoneNumberBundle断言的实体编写固定装置以验证电话号码。以下是该文件的摘录:

/**
 * @AssertPhoneNumber(defaultRegion="VE")
 * @ORM\Column(name="phone", type="phone_number", length=11)
 */
protected $phone;

我不知道如何在灯具本身上使用外部库,所以我看到的唯一解决方案是编写自己的faker并返回格式良好的号码电话并传回夹具。然后我这样做了:

TananeFakerProvider.php

class TananeFakerProvider {

    public function formatPhoneNumber($fakePhoneNumber)
    {
        return $this->container->get('libphonenumber.phone_number_util')->parse($fakePhoneNumber);
    }

}

services.yml

services:
    tanane.faker.provider:
        class: CommonBundle\Tools\TananeFakerProvider
        arguments: ["@service_container"]
        tags:
            -  { name: h4cc_alice_fixtures.provider }

最后 Orders.yml (灯具):

FrontendBundle\Entity\Orders:
    Orders{1..50}:
        nickname: <text(15)>
        # trying to pass the fake number back to the custom faker
        phone: <formatPhoneNumber(phoneNumber())>
        email: <companyEmail()>
        fiscal_address: <address()>
        shipping_address: <address()>
        shipping_from: <randomElement(array('MRW','DOMESA', 'ZOOM'))>
        payment_type: @PaymentType*
        order_amount: <randomFloat(2)>
        bank: @Bank*
        transaction: <randomNumber()>
        comments: <sentence(15)>
        secure: <boolean(35)>
        person: <randomElement(array(@Natural*, @Legal*))> 
        status: @OrderStatus*

但我收到了这个错误:

  

[Symfony \ Component \ Debug \ Exception \ ContextErrorException]注意:   使用未定义的常量phoneNumber - 假设&#39; phoneNumber&#39;在   /var/www/html/vendor/nelmio/alice/src/Nelmio/Alice/Loader/Base.php(630)   :eval()&#39; d代码行1

所以我以错误的方式传递价值,能不能给我一些帮助呢?或者也许给我一个如何实现这个目标的想法?

2 个答案:

答案 0 :(得分:4)

(在github上定居,但这里的后代是副本)

您可以在Alice 1.x中使用以下内容:

FrontendBundle\Entity\Orders:
    Orders{1..50}:
        nickname: <text(15)>
        # trying to pass the fake number back to the custom faker
        phone: <formatPhoneNumber($this->fake('phoneNumber'))>

这是2.x行:

FrontendBundle\Entity\Orders:
    Orders{1..50}:
        nickname: <text(15)>
        # trying to pass the fake number back to the custom faker
        phone: <formatPhoneNumber($fake('phoneNumber'))>

答案 1 :(得分:3)

作为替代方案,您可以在实例化Nelmio / Alice / Fixtures / Loader类时将Faker Provider对象作为第二个参数传递。

1.-创建提供者类

class MyCustomFakeProvider
{
    public function tell_me_something()
    {
        return 'something';
    }
}

2.-

use Nelmio\Alice\Fixtures\Loader;

use Doctrine\Common\DataFixtures\AbstractFixture;
use Doctrine\Common\DataFixtures\OrderedFixtureInterface;

class ExampleDataLoader extends AbstractFixture implements OrderedFixtureInterface
{
    public function loadMyDataFixtures() {       

        $myCustomFakeProvider = new MyCustomFakeProvider();
        $loader = new Loader('es_ES', $myCustomFakeProvider);
        $objects = $loader->load(__DIR__ . '/example.yml');

        $persister = new \Nelmio\Alice\Persister\Doctrine($manager);
        $persister->persist($objects);

3.-最后创建一个example.yml文件(在本例中与Loader类相同的级别),包含一些数据和对tell_me_something()的自定义调用

AppBundle\Entity\Example:
    examples_1{1..101}:
    title: <tell_me_something()> # MY CUSTOM METHOD
    description: <sentence()>