Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace Faker\Container;
6
 
7
use Faker\Core;
8
use Faker\Extension\BarcodeExtension;
9
use Faker\Extension\BloodExtension;
10
use Faker\Extension\ColorExtension;
11
use Faker\Extension\DateTimeExtension;
12
use Faker\Extension\FileExtension;
13
use Faker\Extension\NumberExtension;
14
use Faker\Extension\UuidExtension;
15
use Faker\Extension\VersionExtension;
16
 
17
/**
18
 * @experimental This class is experimental and does not fall under our BC promise
19
 */
20
final class ContainerBuilder
21
{
22
    /**
23
     * @var array<string, callable|object|string>
24
     */
25
    private $definitions = [];
26
 
27
    /**
28
     * @param callable|object|string $value
29
     *
30
     * @throws \InvalidArgumentException
31
     */
32
    public function add($value, string $name = null): self
33
    {
34
        if (!is_string($value) && !is_callable($value) && !is_object($value)) {
35
            throw new \InvalidArgumentException(sprintf(
36
                'First argument to "%s::add()" must be a string, callable or object.',
37
                self::class,
38
            ));
39
        }
40
 
41
        if ($name === null) {
42
            if (is_string($value)) {
43
                $name = $value;
44
            } elseif (is_object($value)) {
45
                $name = get_class($value);
46
            } else {
47
                throw new \InvalidArgumentException(sprintf(
48
                    'Second argument to "%s::add()" is required not passing a string or object as first argument',
49
                    self::class,
50
                ));
51
            }
52
        }
53
 
54
        $this->definitions[$name] = $value;
55
 
56
        return $this;
57
    }
58
 
59
    public function build(): ContainerInterface
60
    {
61
        return new Container($this->definitions);
62
    }
63
 
64
    /**
65
     * Get an array with extension that represent the default English
66
     * functionality.
67
     */
68
    public static function defaultExtensions(): array
69
    {
70
        return [
71
            BarcodeExtension::class => Core\Barcode::class,
72
            BloodExtension::class => Core\Blood::class,
73
            ColorExtension::class => Core\Color::class,
74
            DateTimeExtension::class => Core\DateTime::class,
75
            FileExtension::class => Core\File::class,
76
            NumberExtension::class => Core\Number::class,
77
            VersionExtension::class => Core\Version::class,
78
            UuidExtension::class => Core\Uuid::class,
79
        ];
80
    }
81
 
82
    public static function getDefault(): ContainerInterface
83
    {
84
        $instance = new self();
85
 
86
        foreach (self::defaultExtensions() as $id => $definition) {
87
            $instance->add($definition, $id);
88
        }
89
 
90
        return $instance->build();
91
    }
92
}