Completed
Branch master (6c7c3c)
by Joschi
02:33
created

PropertyList::offsetSet()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
cc 3
eloc 10
nc 4
nop 2
ccs 11
cts 11
cp 1
crap 3
rs 9.4285
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Application\Item
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 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 © 2017 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 Jkphl\Micrometa\Application\Item;
38
39
use Jkphl\Micrometa\Application\Contract\ExportableInterface;
40
use Jkphl\Micrometa\Application\Factory\AliasFactoryInterface;
41
use Jkphl\Micrometa\Domain\Factory\IriFactory;
42
43
/**
44
 * Property list
45
 *
46
 * @package Jkphl\Micrometa
47
 * @subpackage Jkphl\Micrometa\Application
48
 */
49
class PropertyList extends \Jkphl\Micrometa\Domain\Item\PropertyList implements PropertyListInterface
50
{
51
    /**
52
     * Property name aliases
53
     *
54
     * @var array[]
55
     */
56
    protected $aliases = [];
57
    /**
58
     * Alias factory
59
     *
60
     * @var AliasFactoryInterface
61
     */
62
    protected $aliasFactory;
63
64
    /**
65
     * Property list constructor
66
     *
67
     * @param AliasFactoryInterface $aliasFactory Alias factory
68
     */
69 31
    public function __construct(AliasFactoryInterface $aliasFactory)
70
    {
71 31
        $this->aliasFactory = $aliasFactory;
72 31
    }
73
74
    /**
75
     * Set a particular property
76
     *
77
     * @param \stdClass|string $iri IRI
78
     * @param array $value Property values
79
     */
80 28
    public function offsetSet($iri, $value)
81
    {
82 28
        $iri = IriFactory::create($iri);
83 28
        $iriStr = strval($iri);
84 28
        $cursor = array_key_exists($iriStr, $this->nameToCursor) ? $this->nameToCursor[$iriStr] : count($this->values);
85 28
        $this->aliases[$iriStr] = [];
86
87
        // Run through all name aliases
88 28
        foreach ($this->aliasFactory->createAliases($iri->name) as $alias) {
89 28
            $this->aliases[$iriStr][] = $alias;
90 28
            $this->nameToCursor[$iri->profile.$alias] = $cursor;
91
        }
92
93 28
        $this->names[$cursor] = $iri;
94 28
        $this->values[$cursor] = $value;
95 28
    }
96
97
    /**
98
     * Export the object
99
     *
100
     * @return mixed
101
     */
102 5
    public function export()
103
    {
104 5
        $propertyList = [];
105 5
        foreach ($this->names as $iri) {
106 3
            $profiledName = strval($iri);
107 3
            $cursor = $this->nameToCursor[$profiledName];
108 3
            $propertyList[$profiledName] = array_map(
109 3
                function (ExportableInterface $value) {
110 3
                    return $value->export();
111 3
                }, $this->values[$cursor]
112
            );
113
        }
114 5
        return $propertyList;
115
    }
116
117
    /**
118
     * Get a particular property cursor by its name
119
     *
120
     * @param string $name Property name
121
     * @return int Property cursor
122
     */
123 7
    protected function getPropertyCursor($name)
124
    {
125
        // Run through all property names
126 7
        foreach ($this->names as $cursor => $iri) {
127 7
            foreach ($this->aliases[strval($iri)] as $alias) {
128 7
                if ($name === $alias) {
129 7
                    return $cursor;
130
                }
131
            }
132
        }
133
134 4
        return $this->handleUnknownName($name);
135
    }
136
}
137