Completed
Push — master ( e81e8a...e81e8a )
by Claude
01:07
created

Choices::initialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 2
1
<?php
2
3
/*
4
 * A php library for using the Emarsys API.
5
 *
6
 * @link      https://github.com/quitoque/emarsys-php-client
7
 * @package   emarsys-php-client
8
 * @license   MIT
9
 * @copyright Copyright (c) 2017 Quitoque <[email protected]>
10
 */
11
12
namespace Emarsys\Mapping;
13
14
use Emarsys\Exception\Mapping\NotFoundFieldException;
15
16
/**
17
 * Class Choices.
18
 *
19
 * @author Claude Khedhiri <[email protected]>
20
 */
21
class Choices
22
{
23
    /**
24
     * @var array
25
     */
26
    private $mapping = array();
27
28
    /**
29
     * @var array
30
     */
31
    private $systemFields = array();
32
33
    /**
34
     * @var Fields
35
     */
36
    private $fields;
37
38
    public function initialize(Fields $fields)
39
    {
40
        $this->fields = $fields;
41
        $this->systemFields = require __DIR__.'/../../config/choices.php';
42
    }
43
44
    /**
45
     * @param array $mapping
46
     */
47
    public function addMapping(array $mapping)
48
    {
49
        $this->mapping = array_merge($this->mapping, $mapping);
50
    }
51
52
    /**
53
     * @param $name
54
     *
55
     * @return array
56
     */
57
    public function find($name)
58
    {
59
        if (is_string($name)) {
60
            return $this->findByName($name);
61
        }
62
    }
63
64
    /**
65
     * @param $name
66
     *
67
     * @return array
68
     */
69
    public function findByName($name)
70
    {
71
        if (array_key_exists($name, $this->systemFields)) {
72
            return $this->systemFields[$name];
73
        }
74
75
        if (array_key_exists($name, $this->mapping)) {
76
            return $this->mapping[$name];
77
        }
78
79
        throw new NotFoundFieldException($name);
80
    }
81
82
    /**
83
     * @param $name
84
     * @param mixed $field
85
     *
86
     * @return array
87
     */
88
    public function findByField($field)
89
    {
90
        $this->fields->find($field);
91
    }
92
}
93