RemoveProperty::__invoke()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
3
/**
4
 * Copyright (c) 2015-present Ganbaro Digital Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   DataContainers/Editors
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @copyright 2015-present Ganbaro Digital Ltd www.ganbarodigital.com
41
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
42
 * @link      http://code.ganbarodigital.com/php-data-containers
43
 */
44
45
namespace GanbaroDigital\DataContainers\Editors;
46
47
use ArrayObject;
48
use GanbaroDigital\DataContainers\Exceptions\E4xx_UnsupportedType;
49
use GanbaroDigital\Reflection\Checks\IsAssignable;
50
use GanbaroDigital\Reflection\Checks\IsIndexable;
51
use GanbaroDigital\Reflection\Requirements\RequireAssignable;
52
use GanbaroDigital\Reflection\Requirements\RequireIndexable;
53
use GanbaroDigital\Reflection\ValueBuilders\LookupMethodByType;
54
use GanbaroDigital\Reflection\ValueBuilders\SimpleType;
55
56
class RemoveProperty
57
{
58
    use LookupMethodByType;
59
60
    /**
61
     * remove data from a container
62
     *
63
     * @param  mixed $container
64
     *         the container that we want to remove data from
65
     * @param  string $property
66
     *         the data that we want to remove
67
     * @return void
68
     */
69
    public function __invoke(&$container, $property)
70
    {
71
        return self::from($container, $property);
72
    }
73
74
    /**
75
     * remove data from a container
76
     *
77
     * @param  mixed $container
78
     *         the container that we want to remove data from
79
     * @param  string $property
80
     *         the data that we want to remove
81
     * @return void
82
     */
83
    public static function from(&$container, $property)
84
    {
85
        $method = self::lookupMethodFor($container, self::$dispatchTable);
86
        self::$method($container, $property);
87
    }
88
89
    /**
90
     * called when we're given a container that we cannot do anything with
91
     *
92
     * @return void
93
     */
94
    public static function nothingMatchesTheInputType($container)
95
    {
96
        throw new E4xx_UnsupportedType(SimpleType::from($container));
97
    }
98
99
    /**
100
     * remove data from a container
101
     *
102
     * @param  array|ArrayObject $container
103
     *         the container that we want to remove data from
104
     * @param  string $property
105
     *         the data that we want to remove
106
     * @return void
107
     */
108
    private static function fromArray(&$container, $property)
109
    {
110
        if (isset($container[$property])) {
111
            unset($container[$property]);
112
        }
113
    }
114
115
    /**
116
     * remove data from a container
117
     *
118
     * @param  object $container
119
     *         the container that we want to remove data from
120
     * @param  string $property
121
     *         the data that we want to remove
122
     * @return void
123
     */
124
    private static function fromObject($container, $property)
125
    {
126
        if (isset($container->{$property})) {
127
            unset($container->{$property});
128
        }
129
    }
130
131
    private static $dispatchTable = [
132
        'Array' => 'fromArray',
133
        'Assignable' => 'fromObject',
134
        'Indexable' => 'fromArray',
135
        'Object' => 'fromObject',
136
    ];
137
}