PartChoice::assign()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage Apparat\Resource\Domain
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Domain\Model\Part;
38
39
use Apparat\Resource\Domain\Model\Hydrator\HydratorInterface;
40
41
/**
42
 * Part choice
43
 *
44
 * @package     Apparat\Resource
45
 * @subpackage Apparat\Resource\Domain
46
 */
47
class PartChoice extends AbstractPartAggregate
48
{
49
    /**
50
     * Part name wildcard
51
     *
52
     * @var string
53
     */
54
    const WILDCARD = '*';
55
56
    /**
57
     * Assign data to a particular part
58
     *
59
     * @param string $part Part identifier
60
     * @param string $data Part data
61
     * @param null|int $occurrence Occurrence to assign the part data to
62
     */
63 23
    public function assign($part, $data, $occurrence = null)
64
    {
65 23
        $occurrence = $this->prepareAssignment($part, $occurrence);
66
67
        /** @var HydratorInterface $hydrator */
68 23
        $hydrator =& $this->template[$part];
69 23
        $this->occurrences[$occurrence] = [$part => $hydrator->hydrate($data)];
70 23
    }
71
72
    /**
73
     * Add an occurrence
74
     *
75
     * @return void
76
     */
77 23
    protected function addOccurrence()
78
    {
79 23
        $this->occurrences[] = null;
80 23
    }
81
82
    /**
83
     * Test if a particular part identifier is known for a particular occurrence
84
     *
85
     * @param int $occurrence Occurrence index
86
     * @param string $part Part identifier
87
     * @return bool Is known part identifier
88
     */
89 6
    protected function isKnownPartIdentifier($occurrence, $part)
90
    {
91 6
        return parent::isKnownPartIdentifier(
92
            $occurrence,
93
            $part
94 6
        ) || (($part == self::WILDCARD) && count($this->occurrences[$occurrence]));
95
    }
96
97
    /**
98
     * Return a particular part of a particular occurrence
99
     *
100
     * @param int $occurrence Occurrence index
101
     * @param string $part Part identifier
102
     * @return PartInterface Part instance
103
     */
104 6
    protected function getOccurrencePart(&$occurrence, &$part)
105
    {
106 6
        reset($this->occurrences[$occurrence]);
107
108 6
        if ($part == self::WILDCARD) {
109 3
            $part = key($this->occurrences[$occurrence]);
110
        }
111
112 6
        return parent::getOccurrencePart($occurrence, $part);
113
    }
114
}
115