1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Copyright (c) 2014-2016 Ryan Parman. |
4
|
|
|
* |
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
7
|
|
|
* in the Software without restriction, including without limitation the rights |
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
10
|
|
|
* furnished to do so, subject to the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
13
|
|
|
* all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21
|
|
|
* THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
* http://opensource.org/licenses/MIT |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace Skyzyx\StrongTypes; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Base shape that all other shapes extend from. |
30
|
|
|
*/ |
31
|
|
|
abstract class AbstractShape |
32
|
|
|
{ |
33
|
|
|
const TYPE_EXCEPTION_MESSAGE = 'The %s class expects a value of type %s. Received a value of type %s instead.'; |
34
|
|
|
|
35
|
|
|
/** @var mixed */ |
36
|
|
|
protected $value; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* A map of Strong Types => native types. |
40
|
|
|
* |
41
|
|
|
* @var array<string> |
42
|
|
|
*/ |
43
|
|
|
protected $typemap = [ |
44
|
|
|
'Skyzyx\\StrongTypes\\BooleanType' => 'boolean', |
45
|
|
|
'Skyzyx\\StrongTypes\\Collection' => 'array', |
46
|
|
|
'Skyzyx\\StrongTypes\\IntegerType' => 'integer', |
47
|
|
|
'Skyzyx\\StrongTypes\\FloatType' => 'double', |
48
|
|
|
'Skyzyx\\StrongTypes\\StringType' => 'string', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Constructs a new instance of this object. |
53
|
|
|
* |
54
|
|
|
* @param mixed $value The value to assign to the instance. |
55
|
|
|
*/ |
56
|
|
|
public function __construct($value = null) |
57
|
|
|
{ |
58
|
|
|
$this->value = $value; |
59
|
|
|
$this->validate(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getValue() |
66
|
|
|
{ |
67
|
|
|
return $this->value; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the typemap. |
72
|
|
|
* |
73
|
|
|
* @return array<string> The typemap. |
74
|
|
|
*/ |
75
|
|
|
public function getTypeMap() |
76
|
|
|
{ |
77
|
|
|
return $this->typemap; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Gets the name of the native type that pairs with the Strong Type. |
82
|
|
|
* |
83
|
|
|
* @param ShapeInterface $value A strongly-typed object. |
84
|
|
|
* @return string The name of the native type. |
|
|
|
|
85
|
|
|
*/ |
86
|
|
|
public function getNativeType(ShapeInterface $value) |
87
|
|
|
{ |
88
|
|
|
foreach ($this->typemap as $type => $native) { |
89
|
|
|
if (is_a($value, $type)) { |
90
|
|
|
return $native; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* {@inheritdoc} |
99
|
|
|
*/ |
100
|
|
|
public function __toString() |
101
|
|
|
{ |
102
|
|
|
return $this->value; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.