Skip to content

Commit 2aa68ac

Browse files
author
ivan.babenko
committed
Refactoring and naming strategies draft
1 parent c8fc9cf commit 2aa68ac

File tree

7 files changed

+130
-36
lines changed

7 files changed

+130
-36
lines changed

src/Configurable.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace BabenkoIvan\FluentArray;
4+
5+
interface Configurable
6+
{
7+
/**
8+
* @param FluentArray|null $defaultConfig
9+
* @return mixed
10+
*/
11+
public static function defaultConfig(FluentArray $defaultConfig = null);
12+
13+
/**
14+
* @param FluentArray|null $config
15+
* @return mixed
16+
*/
17+
public function config(FluentArray $config = null);
18+
}

src/FluentArray.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,53 +2,21 @@
22

33
namespace BabenkoIvan\FluentArray;
44

5-
class FluentArray
5+
class FluentArray implements Configurable
66
{
7+
use HasConfiguration;
8+
79
/**
810
* @var array
911
*/
1012
private $storage = [];
1113

12-
/**
13-
* @var FluentArray
14-
*/
15-
private static $defaultConfig;
16-
17-
/**
18-
* @var FluentArray
19-
*/
20-
private $config;
21-
2214
/**
2315
* @param FluentArray|null $config
2416
*/
2517
public function __construct(FluentArray $config = null)
2618
{
27-
$this->config = $config;
28-
}
29-
30-
/**
31-
* @return FluentArray
32-
*/
33-
public static function defaultConfig()
34-
{
35-
if (!isset(static::$defaultConfig)) {
36-
static::$defaultConfig = new FluentArray();
37-
}
38-
39-
return static::$defaultConfig;
40-
}
41-
42-
/**
43-
* @return FluentArray
44-
*/
45-
public function config()
46-
{
47-
if (!isset($this->config)) {
48-
$this->config = clone static::defaultConfig();
49-
}
50-
51-
return $this->config;
19+
$this->config($config);
5220
}
5321

5422
/**

src/HasConfiguration.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace BabenkoIvan\FluentArray;
4+
5+
trait HasConfiguration
6+
{
7+
/**
8+
* @var FluentArray
9+
*/
10+
private static $defaultConfig;
11+
12+
/**
13+
* @var FluentArray
14+
*/
15+
private $config;
16+
17+
/**
18+
* @param FluentArray|null $defaultConfig
19+
* @return mixed
20+
*/
21+
public static function defaultConfig(FluentArray $defaultConfig = null)
22+
{
23+
if (isset($defaultConfig)) {
24+
static::$defaultConfig = $defaultConfig;
25+
} else {
26+
if (!isset(static::$defaultConfig)) {
27+
static::$defaultConfig = new FluentArray();
28+
}
29+
30+
return static::$defaultConfig;
31+
}
32+
}
33+
34+
/**
35+
* @param FluentArray|null $config
36+
* @return mixed
37+
*/
38+
public function config(FluentArray $config = null)
39+
{
40+
if (isset($config)) {
41+
$this->config = $config;
42+
return $this;
43+
} else {
44+
if (!isset($this->config)) {
45+
$this->config = clone static::defaultConfig();
46+
}
47+
48+
return $this->config;
49+
}
50+
}
51+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace BabenkoIvan\FluentArray\NamingStrategies;
4+
5+
class CamelCaseStrategy implements NamingStrategy
6+
{
7+
/**
8+
* @param string $key
9+
* @return string
10+
*/
11+
public function transform(string $key): string
12+
{
13+
// TODO: Implement transform() method.
14+
}
15+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace BabenkoIvan\FluentArray\NamingStrategies;
4+
5+
interface NamingStrategy
6+
{
7+
/**
8+
* @param string $key
9+
* @return string
10+
*/
11+
public function transform(string $key): string;
12+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace BabenkoIvan\FluentArray\NamingStrategies;
4+
5+
class NullStrategy implements NamingStrategy
6+
{
7+
/**
8+
* @param string $key
9+
* @return string
10+
*/
11+
public function transform(string $key): string
12+
{
13+
return $key;
14+
}
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace BabenkoIvan\FluentArray\NamingStrategies;
4+
5+
class UnderscoreStrategy implements NamingStrategy
6+
{
7+
/**
8+
* @param string $key
9+
* @return string
10+
*/
11+
public function transform(string $key): string
12+
{
13+
// TODO: Implement transform() method.
14+
}
15+
}

0 commit comments

Comments
 (0)