Skip to content

Commit 0a80290

Browse files
committed
Problem file
1 parent d9ae133 commit 0a80290

File tree

1 file changed

+77
-0
lines changed
  • exercises/a-safe-space-for-nulls/problem

1 file changed

+77
-0
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
Create a program that exports a `User` object instance to a CSV file using the null safe operator to access it's member properties.
2+
3+
You will have a variable named `$user` available in your PHP script. We will put it there, each time your program runs and it will be populated with random data each time it runs.
4+
5+
Sometimes, a number of the properties may not exist and will be null.
6+
7+
With the null safe operator it is possible to access variables like so:
8+
9+
```php
10+
$capitalPopulation = $country?->capital?->population;
11+
```
12+
13+
If the `capital` property is null, the variable `$capitalPopulation` will also contain null. Previously, without the null safe operator this would be achieved like so:
14+
15+
```php
16+
$capitalPopulation = null;
17+
if ($city->capital !== null) {
18+
$capitalPopulation = $country->capital->population;
19+
}
20+
```
21+
22+
The `User` class, for which the `$user` variable holds an instance of, has the following signature:
23+
24+
```php
25+
class User
26+
{
27+
public string $firstName;
28+
public string $lastName;
29+
public ?int $age = null;
30+
public ?Address $address = null;
31+
}
32+
33+
class Address
34+
{
35+
public int $number;
36+
public string $addressLine1;
37+
public ?string $addressLine2 = null;
38+
}
39+
```
40+
41+
Note also the `Address` class which the property `$user->address` may be an instance of, or it may be null.
42+
43+
Export the `$user` data to a CSV with the following columns:
44+
45+
`First Name`, `Last Name`, `Age`, `House num`, `Addr 1`, `Addr 2`
46+
47+
* The CSV should be comma delimited
48+
* The columns should read exactly as above, any mistake will trigger a failure
49+
* There should be one row for the column headers and one for the data
50+
* Any properties which are null on the user should be printed as empty fields in the CSV
51+
* The file should be named `users.csv` and exist next to your submission file (eg in the same directory)
52+
53+
And finally, the most important part, all properties which may be `NULL` should be accessed using the null safe operator!
54+
55+
### Advantages of the null safe operator
56+
57+
* Much less code for simple operations where null is a valid value
58+
* If the operator is part of a chain anything to the right of the null will not be executed, the statements will be short-circuited.
59+
* Can be used on methods where null coalescing cannot `$user->getCreatedAt()->format() ?? null` where `getCreatedAt()` could return null or a `\DateTime` instance
60+
61+
----------------------------------------------------------------------
62+
## HINTS
63+
64+
Remember your program will be passed no arguments. There will be a `User` object populated for you under the variable `$user`.
65+
It is available at the beginning of your script.
66+
67+
Documentation on the Null Safe Operator can be found by pointing your browser here:
68+
[https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.nullsafe]()
69+
70+
----------------------------------------------------------------------
71+
## EXTRA
72+
73+
We have not given any hints regarding writing to a CSV file, as we are not testing you on that. You can do it however you please.
74+
75+
Therefore, it is up to you to figure out how to write a CSV if you don't already know :)
76+
77+
Okay... just one hint: Check back over the exercise "Have the Last Say". You might find some pointers there!

0 commit comments

Comments
 (0)