1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
* |
13
|
|
|
* This software consists of voluntary contributions made by many individuals |
14
|
|
|
* and is licensed under the MIT license. |
15
|
|
|
* |
16
|
|
|
* Copyright (c) 2018 Yuuki Takezawa |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Ytake\Lom\Factory; |
20
|
|
|
|
21
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
22
|
|
|
use Ytake\Lom\Exception\TraitMethodCallException; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Class ToStringTrait. |
26
|
|
|
* |
27
|
|
|
* @author yuuki.takezawa<[email protected]> |
28
|
|
|
*/ |
29
|
|
|
trait ToStringTrait |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @param \string[] $getters |
33
|
|
|
* |
34
|
|
|
* @return ClassMethod |
35
|
|
|
*/ |
36
|
|
|
protected function createToString(array $getters): ClassMethod |
37
|
|
|
{ |
38
|
|
|
if (!$this instanceof AbstractDriver) { |
39
|
|
|
throw new TraitMethodCallException("should extend " . AbstractDriver::class); |
40
|
|
|
} |
41
|
|
|
/** @var \ReflectionClass $reflector */ |
42
|
|
|
$reflector = $this->reflector; |
|
|
|
|
43
|
|
|
$class = $reflector->getName(); |
|
|
|
|
44
|
|
|
$classMethods = []; |
45
|
|
|
foreach ($getters as $getter) { |
46
|
|
|
$classMethods[] = "\$this->{$getter['method']}()"; |
47
|
|
|
} |
48
|
|
|
$stringBuilder = implode(" . ', ' . ", $classMethods); |
49
|
|
|
$build = "return '{$class}(' . $stringBuilder . ')';"; |
50
|
|
|
/** @var \PhpParser\BuilderFactory $builder */ |
51
|
|
|
$builder = $this->builder; |
|
|
|
|
52
|
|
|
|
53
|
|
|
return $builder->method('__toString') |
54
|
|
|
->setDocComment('') |
55
|
|
|
->addStmt( |
56
|
|
|
new \PhpParser\Node\Stmt\Class_($build) |
57
|
|
|
)->makePublic()->getNode(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
This check looks for access to properties that are not accessible from the current context.
If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.