Completed
Pull Request — master (#153)
by
unknown
08:58
created

StringCollection::asArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Yandex\Common;
4
5
use Yandex\Common\Exception\InvalidArgumentException;
6
7
/**
8
 * String collection
9
 *
10
 * @category Yandex
11
 * @package  Common
12
 *
13
 * @author   Vasilii Zaluev
14
 * @created  17.03.16 22:31
15
 */
16
class StringCollection
17
{
18
   /**
19
    * This string collection will convert to one string in get request by join
20
    *
21
    * @var string
22
    */
23
   protected $collection = [];
24
25
   public function __construct($strings)
26
   {
27
      foreach($strings as $string){
28
         if(!self::isString($string)){
29
            throw new InvalidArgumentException('Argument must have only strings');
30
         }
31
32
         $this->collection[] = $string;
33
      }
34
   }
35
36
   /**
37
    * @param $strings
38
    * @return null|self
39
    * @throws InvalidArgumentException
40
    */
41
   public static function init($strings)
42
   {
43
      if (is_null($strings)) {
44
         return null;
45
      }
46
47
      if (self::isString($strings)) {
48
         return new static([$strings]);
49
      }
50
51
      if (is_array($strings)){
52
         if(empty($strings)){
53
            return null;
54
         }
55
56
         return new self($strings);
57
      }
58
59
      throw new InvalidArgumentException('Argument must be string or string list, ' . gettype($strings) . ' received');
60
   }
61
62
   public function getAll()
63
   {
64
      return implode(',', $this->collection);
65
   }
66
67
   private static function isString($string)
68
   {
69
      return is_string($string) || method_exists($string, '__toString');
70
   }
71
72
   public function asArray()
73
   {
74
      return $this->collection;
75
   }
76
}