Completed
Push — master ( 96b2f8...da2c9c )
by Kirill
05:58
created

Serializable::__sleep()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Reflection\Common;
11
12
/**
13
 * Trait Serializable
14
 */
15
trait Serializable
16
{
17
    /**
18
     * @return array
19
     */
20
    public function __sleep(): array
21
    {
22
        return \array_keys(\iterator_to_array($this->getObjectFields()));
23
    }
24
25
    /**
26
     * @return array
27
     */
28
    public function __debugInfo(): array
29
    {
30
        return \iterator_to_array($this->getObjectFields());
31
    }
32
33
    /**
34
     * @return iterable|\Generator
35
     */
36
    protected function getObjectFields(): iterable
37
    {
38
        $reflection = new \ReflectionObject($this);
39
40
        $flags = \ReflectionProperty::IS_PROTECTED | \ReflectionProperty::IS_PUBLIC;
41
42
        foreach ($reflection->getProperties($flags) as $field) {
43
            $field->setAccessible(true);
44
45
            yield $field->getName() => $field->getValue($this);
46
        }
47
    }
48
}
49