File size: 5,014 Bytes
93d826e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php

declare(strict_types=1);

namespace Example;

use DateTime;
use Exception;
use InvalidArgumentException;
use JsonSerializable;
use Psr\Log\LoggerInterface;

// Interface definition
interface DataProcessor
{
    public function process(mixed $data): mixed;
    public function validate(mixed $data): bool;
}

// Trait definition
trait Loggable
{
    protected ?LoggerInterface $logger = null;

    public function setLogger(LoggerInterface $logger): void
    {
        $this->logger = $logger;
    }

    protected function log(string $message, array $context = []): void
    {
        $this->logger?->info($message, $context);
    }
}

// Abstract class
abstract class Entity implements JsonSerializable
{
    protected DateTime $createdAt;
    protected ?DateTime $updatedAt = null;

    public function __construct()
    {
        $this->createdAt = new DateTime();
    }

    abstract public function validate(): bool;

    public function jsonSerialize(): mixed
    {
        return [
            'createdAt' => $this->createdAt->format('c'),
            'updatedAt' => $this->updatedAt?->format('c'),
        ];
    }
}

// Enum definition (PHP 8.1+)
enum Status: string
{
    case PENDING = 'pending';
    case ACTIVE = 'active';
    case COMPLETED = 'completed';
    case FAILED = 'failed';

    public function label(): string
    {
        return match($this) {
            self::PENDING => 'Pending',
            self::ACTIVE => 'Active',
            self::COMPLETED => 'Completed',
            self::FAILED => 'Failed',
        };
    }
}

// Class implementing interface and using trait
class User extends Entity implements DataProcessor
{
    use Loggable;

    private static int $instanceCount = 0;

    public function __construct(
        private string $name,
        private string $email,
        private Status $status = Status::PENDING,
        private array $metadata = []
    ) {
        parent::__construct();
        self::$instanceCount++;
    }

    public static function getInstanceCount(): int
    {
        return self::$instanceCount;
    }

    // Property getter with validation
    public function getEmail(): string
    {
        return $this->email;
    }

    // Property setter with validation
    public function setEmail(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException('Invalid email format');
        }
        $this->email = $email;
        $this->updatedAt = new DateTime();
    }

    // Magic method implementation
    public function __get(string $name)
    {
        return $this->metadata[$name] ?? null;
    }

    public function __set(string $name, mixed $value): void
    {
        $this->metadata[$name] = $value;
    }

    // Interface method implementations
    public function process(mixed $data): mixed
    {
        if (!is_array($data)) {
            throw new InvalidArgumentException('Data must be an array');
        }

        $this->log('Processing user data', ['user' => $this->name]);

        foreach ($data as $key => $value) {
            $this->metadata[$key] = $value;
        }

        $this->status = Status::COMPLETED;
        return $this;
    }

    public function validate(mixed $data): bool
    {
        return is_array($data) && !empty($data);
    }

    // Abstract method implementation
    public function validate(): bool
    {
        return !empty($this->name) && !empty($this->email);
    }

    // Method using arrow functions (PHP 7.4+)
    public function getMetadataValues(): array
    {
        return array_map(
            fn($value) => is_array($value) ? json_encode($value) : (string)$value,
            $this->metadata
        );
    }

    // Implementation of JsonSerializable
    public function jsonSerialize(): mixed
    {
        return [
            ...parent::jsonSerialize(),
            'name' => $this->name,
            'email' => $this->email,
            'status' => $this->status->value,
            'metadata' => $this->metadata,
        ];
    }
}

// Custom exception
class ProcessingException extends Exception
{
    public function __construct(
        string $message = "",
        private ?string $errorCode = null,
        int $code = 0,
        ?Throwable $previous = null
    ) {
        parent::__construct($message, $code, $previous);
    }

    public function getErrorCode(): ?string
    {
        return $this->errorCode;
    }
}

// Anonymous class usage
$validator = new class {
    public function validateUser(User $user): bool
    {
        return $user->validate();
    }
};

// Example usage
try {
    $user = new User("John Doe", "john@example.com");
    $user->process(['role' => 'admin', 'preferences' => ['theme' => 'dark']]);
    
    // Using magic methods
    $user->customField = 'custom value';
    echo $user->customField;
    
    // JSON serialization
    echo json_encode($user, JSON_PRETTY_PRINT);
    
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}