PropertyList::export()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 16
c 0
b 0
f 0
ccs 10
cts 10
cp 1
rs 9.7333
cc 2
nc 2
nop 0
crap 2
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 © 2018 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 © 2018 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 33
    public function __construct(AliasFactoryInterface $aliasFactory)
70
    {
71 33
        $this->aliasFactory = $aliasFactory;
72 33
    }
73
74
    /**
75
     * Set a particular property
76
     *
77
     * @param \stdClass|string $iri IRI
78
     * @param array $value          Property values
79
     */
80 29
    public function offsetSet($iri, $value)
81
    {
82 29
        $iri                    = IriFactory::create($iri);
83 29
        $iriStr                 = strval($iri);
84 29
        $cursor                 = array_key_exists($iriStr,
85 29
            $this->nameToCursor) ? $this->nameToCursor[$iriStr] : count($this->values);
86 29
        $this->aliases[$iriStr] = [];
87
88
        // Run through all name aliases
89 29
        foreach ($this->aliasFactory->createAliases($iri->name) as $alias) {
90 29
            $this->aliases[$iriStr][]                 = $alias;
91 29
            $this->nameToCursor[$iri->profile.$alias] = $cursor;
92
        }
93
94 29
        $this->names[$cursor]  = $iri;
95 29
        $this->values[$cursor] = $value;
96 29
    }
97
98
    /**
99
     * Export the object
100
     *
101
     * @return mixed
102
     */
103 5
    public function export()
104
    {
105 5
        $propertyList = [];
106 5
        foreach ($this->names as $iri) {
107 3
            $profiledName                = strval($iri);
108 3
            $cursor                      = $this->nameToCursor[$profiledName];
109 3
            $propertyList[$profiledName] = array_map(
110
                function (ExportableInterface $value) {
111 3
                    return $value->export();
112 3
                },
113 3
                $this->values[$cursor]
114
            );
115
        }
116
117 5
        return $propertyList;
118
    }
119
120
    /**
121
     * Get a particular property cursor by its name
122
     *
123
     * @param string $name Property name
124
     *
125
     * @return int Property cursor
126
     */
127 8
    protected function getPropertyCursor($name)
128
    {
129
        // Run through all property names
130 8
        foreach ($this->names as $cursor => $iri) {
131 8
            foreach ($this->aliases[strval($iri)] as $alias) {
132 8
                if ($name === $alias) {
133 8
                    return $cursor;
134
                }
135
            }
136
        }
137
138 4
        return $this->handleUnknownName($name);
139
    }
140
}
141