AbstractPart::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
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
 * Abstract base class for file parts
43
 *
44
 * @package     Apparat\Resource
45
 * @subpackage Apparat\Resource\Domain
46
 */
47
abstract class AbstractPart implements PartInterface
48
{
49
    /**
50
     * Associated hydrator
51
     *
52
     * @var HydratorInterface
53
     */
54
    protected $hydrator = null;
55
56
    /**
57
     * Abstract part constructor
58
     *
59
     * @param HydratorInterface $hydrator Associated hydrator
60
     */
61 79
    public function __construct(HydratorInterface $hydrator)
62
    {
63 79
        $this->hydrator = $hydrator;
64 79
    }
65
66
    /**
67
     * Validate a part identifier
68
     *
69
     * @param string $part Part identifier
70
     * @throws InvalidArgumentException If the part identifier is not valid
71
     */
72 100
    public static function validatePartIdentifier($part)
73
    {
74 100
        $part = strval($part);
75 100
        if (!preg_match("%^[a-z0-9\_\*]+$%i", $part)) {
76 3
            throw new InvalidArgumentException(
77 3
                sprintf('Invalid part path identifier "%s"', $part),
78 3
                InvalidArgumentException::INVALID_PART_IDENTIFIER
79
            );
80
        }
81 99
    }
82
83
    /**
84
     * Return the associated hydrator
85
     *
86
     * @return HydratorInterface Associated hydrator
87
     */
88 39
    public function getHydrator()
89
    {
90 39
        return $this->hydrator;
91
    }
92
93
    /**
94
     * Delegate a method call to a subpart
95
     *
96
     * @param string $method Method nae
97
     * @param array $subparts Subpart identifiers
98
     * @param array $arguments Method arguments
99
     * @return mixed Method result
100
     * @throws InvalidArgumentException If the method is unknown
101
     */
102 26
    public function delegate($method, array $subparts, array $arguments)
103
    {
104
        // If the method is unknown
105 26
        if (!is_callable(array($this, $method))) {
106 1
            throw new InvalidArgumentException(
107 1
                sprintf('Unknown part method "%s"', $method),
108 1
                InvalidArgumentException::UNKNOWN_PART_METHOD
109
            );
110
        }
111
112 25
        unset($subparts);
113
114
        // Call the method
115 25
        return call_user_func_array(array($this, $method), $arguments);
116
    }
117
}
118