|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* CSVelte: Slender, elegant CSV for PHP |
|
4
|
|
|
* |
|
5
|
|
|
* Inspired by Python's CSV module and Frictionless Data and the W3C's CSV |
|
6
|
|
|
* standardization efforts, CSVelte was written in an effort to take all the |
|
7
|
|
|
* suck out of working with CSV. |
|
8
|
|
|
* |
|
9
|
|
|
* @version v${CSVELTE_DEV_VERSION} |
|
10
|
|
|
* @copyright Copyright (c) 2016 Luke Visinoni <[email protected]> |
|
11
|
|
|
* @author Luke Visinoni <[email protected]> |
|
12
|
|
|
* @license https://github.com/deni-zen/csvelte/blob/master/LICENSE The MIT License (MIT) |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace CSVelte\Collection; |
|
15
|
|
|
|
|
16
|
|
|
class CharCollection extends AbstractCollection |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Convert input data to an array. |
|
20
|
|
|
* |
|
21
|
|
|
* Convert the input data to an array that can be worked with by a collection. |
|
22
|
|
|
* |
|
23
|
|
|
* @param mixed $data The input data |
|
24
|
|
|
* @return array |
|
25
|
|
|
*/ |
|
26
|
25 |
|
protected function prepareData($data) |
|
27
|
|
|
{ |
|
28
|
25 |
|
if (!is_string($data)) { |
|
29
|
1 |
|
$data = (string) $data; |
|
30
|
1 |
|
} |
|
31
|
25 |
|
return str_split($data); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Apply a callback to each item in collection. |
|
36
|
|
|
* |
|
37
|
|
|
* Applies a callback to each item in collection and returns a new collection |
|
38
|
|
|
* containing each iteration's return value. |
|
39
|
|
|
* |
|
40
|
|
|
* @param callable $callback The callback to apply |
|
41
|
|
|
* @return AbstractCollection A new collection with callback return values |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public function map(callable $callback) |
|
44
|
|
|
{ |
|
45
|
2 |
|
return new self(implode('', array_map($callback, $this->data))); |
|
|
|
|
|
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @inheritdoc |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function push(...$items) |
|
52
|
|
|
{ |
|
53
|
1 |
|
$result = parent::push(...$items); |
|
54
|
1 |
|
return new self(implode('', $result->toArray())); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @inheritdoc |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function unshift(...$items) |
|
61
|
|
|
{ |
|
62
|
1 |
|
$result = parent::unshift(...$items); |
|
63
|
1 |
|
return new self(implode('', $result->toArray())); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Is data consistent with this collection type? |
|
69
|
|
|
* |
|
70
|
|
|
* @param mixed $data The data to check |
|
71
|
|
|
* @return bool |
|
72
|
|
|
*/ |
|
73
|
25 |
|
protected function isConsistentDataStructure($data) |
|
74
|
|
|
{ |
|
75
|
25 |
|
return static::isCharacterSet($data); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
} |
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.