Skip to content

Commit e25440f

Browse files
author
Matouš Němec
committed
initial commit
0 parents  commit e25440f

File tree

13 files changed

+1002
-0
lines changed

13 files changed

+1002
-0
lines changed

ArrayManager/ArrayManager.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* Mesour Nette ArrayManager
4+
*
5+
* Documentation here: https://github.com/mesour/ArrayManager
6+
*
7+
* @license LGPL-3.0 and MIT License
8+
* @copyright (c) 2014 Matous Nemec <[email protected]>
9+
*/
10+
11+
namespace Mesour;
12+
13+
/**
14+
* @author mesour <[email protected]>
15+
* @package Mesour ArrayManager
16+
*/
17+
class ArrayManager {
18+
19+
private $data_array;
20+
21+
public function __construct(array & $data_array) {
22+
ArrayManage\Validator::validateArray($data_array);
23+
$this->data_array = & $data_array;
24+
}
25+
26+
public function select() {
27+
return new ArrayManage\Searcher\Select($this->data_array);
28+
}
29+
30+
public function update($update_array) {
31+
return new ArrayManage\Changer\Update($this->data_array, $update_array);
32+
}
33+
34+
public function delete() {
35+
return new ArrayManage\Changer\Delete($this->data_array);
36+
}
37+
38+
public function insert($values_array) {
39+
return new ArrayManage\Changer\Insert($this->data_array, $values_array);
40+
}
41+
42+
}
43+
44+
class ManagerException extends \Exception{
45+
46+
}

ArrayManager/Changers/Delete.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace Mesour\ArrayManage\Changer;
4+
5+
use Mesour\ArrayManage\Searcher\Search,
6+
Mesour\ArrayManage\Translator;
7+
8+
/**
9+
* @author mesour <[email protected]>
10+
* @package Mesour ArrayManager
11+
*/
12+
class Delete extends Search {
13+
14+
/**
15+
* @var array
16+
*/
17+
private $data_array = array();
18+
19+
public function __construct(array & $data_array) {
20+
parent::__construct($data_array);
21+
$this->data_array = & $data_array;
22+
}
23+
24+
public function execute() {
25+
foreach($this->getResults() as $key => $val) {
26+
unset($this->data_array[$key]);
27+
}
28+
}
29+
30+
public function test() {
31+
echo '<pre>';
32+
echo (new Translator(Translator::DELETE))->translate() . "\n" . $this->translate();
33+
echo '</pre>';
34+
}
35+
36+
}

ArrayManager/Changers/Insert.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
namespace Mesour\ArrayManage\Changer;
4+
5+
use Mesour\ArrayManage\Searcher\Search,
6+
Mesour\ArrayManage\Translator,
7+
Mesour\ArrayManage\Validator;
8+
9+
/**
10+
* @author mesour <[email protected]>
11+
* @package Mesour ArrayManager
12+
*/
13+
class Insert extends Search {
14+
15+
private $values_array = array();
16+
17+
/**
18+
* @var array
19+
*/
20+
private $data_array = array();
21+
22+
public function __construct(array & $data_array, $values_array) {
23+
parent::__construct($data_array);
24+
Validator::validateUnknownColumns($data_array, $values_array);
25+
$this->data_array = &$data_array;
26+
$this->values_array = $values_array;
27+
}
28+
29+
public function execute() {
30+
$this->data_array[] = $this->addEmpty($this->values_array);
31+
}
32+
33+
private function addEmpty($insert_arr) {
34+
$count = count(reset($this->data_array));
35+
if (count($insert_arr) !== $count) {
36+
$output = array();
37+
$keys = array_keys(reset($this->data_array));
38+
foreach ($keys as $key) {
39+
if (isset($insert_arr[$key])) {
40+
$output[$key] = $insert_arr[$key];
41+
} else {
42+
$output[$key] = NULL;
43+
}
44+
}
45+
}
46+
return $output;
47+
}
48+
49+
public function test() {
50+
echo '<pre>';
51+
echo (new Translator(Translator::UPDATE, $this->update_array))->translate() . "\n" . $this->translate();
52+
echo '</pre>';
53+
}
54+
55+
}

ArrayManager/Changers/Update.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace Mesour\ArrayManage\Changer;
4+
5+
use Mesour\ArrayManage\Searcher\Search,
6+
Mesour\ArrayManage\Translator,
7+
Mesour\ArrayManage\Validator;
8+
9+
/**
10+
* @author mesour <[email protected]>
11+
* @package Mesour ArrayManager
12+
*/
13+
class Update extends Search {
14+
15+
private $update_array = array();
16+
17+
/**
18+
* @var array
19+
*/
20+
private $data_array = array();
21+
22+
public function __construct(array & $data_array, $update_array) {
23+
parent::__construct($data_array);
24+
Validator::validateUnknownColumns($data_array, $update_array);
25+
$this->data_array = &$data_array;
26+
$this->update_array = $update_array;
27+
}
28+
29+
public function execute() {
30+
foreach ($this->getResults() as $key => $val) {
31+
foreach ($this->update_array as $_key => $_val) {
32+
$this->data_array[$key][$_key] = $_val;
33+
}
34+
}
35+
}
36+
37+
public function test() {
38+
echo '<pre>';
39+
echo (new Translator(Translator::UPDATE, $this->update_array))->translate() . "\n" . $this->translate();
40+
echo '</pre>';
41+
}
42+
43+
}

ArrayManager/Container.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
namespace Mesour\ArrayManage;
4+
5+
/**
6+
* @author mesour <[email protected]>
7+
* @package Mesour ArrayManager
8+
*/
9+
class Container implements \Iterator, \ArrayAccess {
10+
11+
private $data;
12+
13+
public function __construct(array $arr) {
14+
$this->data = $arr;
15+
}
16+
17+
public function offsetExists($offset) {
18+
return isset($this->data[$offset]);
19+
}
20+
21+
public function offsetGet($offset) {
22+
return $this->data[$offset];
23+
}
24+
25+
public function offsetSet($offset, $value) {
26+
$this->data[$offset] = $value;
27+
}
28+
29+
public function offsetUnset($offset) {
30+
unset($this->data[$offset]);
31+
}
32+
33+
function rewind() {
34+
return reset($this->data);
35+
}
36+
37+
function current() {
38+
return current($this->data);
39+
}
40+
41+
function key() {
42+
return key($this->data);
43+
}
44+
45+
function next() {
46+
return next($this->data);
47+
}
48+
49+
function valid() {
50+
return !is_null(key($this->data));
51+
}
52+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
3+
namespace Mesour\ArrayManage\Searcher;
4+
5+
use Mesour\ManagerException,
6+
Mesour\ArrayManage\Translator;
7+
8+
/**
9+
* @author mesour <[email protected]>
10+
* @package Mesour ArrayManager
11+
*/
12+
class Condition {
13+
14+
const EQUAL = 'Equal',
15+
NOT_EQUAL = 'NotEqual',
16+
SMALLER = 'Smaller',
17+
NOT_SMALLER = 'NotSmaller',
18+
BIGGER = 'Bigger',
19+
NOT_BIGGER = 'NotBigger',
20+
STARTS_WITH = 'StartsWith',
21+
NOT_STARTS_WITH = 'NotStartsWith',
22+
ENDS_WITH = 'EndsWith',
23+
NOT_ENDS_WITH = 'NotEndsWith',
24+
CONTAINS = 'Contains',
25+
NOT_CONTAINS = 'NotContains';
26+
27+
static private $case_sensitive = FALSE;
28+
29+
private $allowed = array(
30+
self::EQUAL, self::NOT_EQUAL,
31+
self::SMALLER, self::NOT_SMALLER,
32+
self::BIGGER, self::NOT_BIGGER,
33+
self::STARTS_WITH, self::NOT_STARTS_WITH,
34+
self::ENDS_WITH, self::NOT_ENDS_WITH,
35+
self::CONTAINS, self::NOT_CONTAINS,
36+
);
37+
38+
private $matcher;
39+
40+
public function __construct($matcher) {
41+
if (!in_array($matcher, $this->allowed) && !is_callable($matcher)) {
42+
throw new ManagerException('First value must be callable or some constant of this class.');
43+
}
44+
$this->matcher = $matcher;
45+
}
46+
47+
static public function getInstance($matcher) {
48+
return new self($matcher);
49+
}
50+
51+
static public function setKeysSensitive($sensitive = TRUE) {
52+
self::$case_sensitive = $sensitive;
53+
}
54+
55+
public function match($value, $searched_value, $row_data) {
56+
if (is_string($this->matcher)) {
57+
return call_user_func(array($this, 'match' . $this->matcher), $value, $searched_value, $row_data);
58+
} else {
59+
return call_user_func($this->matcher, $value, $searched_value, $row_data);
60+
}
61+
}
62+
63+
public function translate() {
64+
return (new Translator(Translator::CONDITION, $this->matcher))->translate();
65+
}
66+
67+
private function matchEqual($value, $searched_value) {
68+
if(!self::$case_sensitive) {
69+
$searched_value = strtolower($searched_value);
70+
$value = strtolower($value);
71+
}
72+
return (is_null($searched_value) ? is_null($value) : ((string)$searched_value === (string)$value));
73+
}
74+
75+
private function matchNotEqual($value, $searched_value) {
76+
return !$this->matchEqual($value, $searched_value);
77+
}
78+
79+
private function matchSmaller($value, $searched_value) {
80+
if(!self::$case_sensitive) {
81+
$searched_value = strtolower($searched_value);
82+
$value = strtolower($value);
83+
}
84+
return ($value < $searched_value);
85+
}
86+
87+
private function matchNotSmaller($value, $searched_value) {
88+
return !$this->matchSmaller($value, $searched_value);
89+
}
90+
91+
private function matchBigger($value, $searched_value) {
92+
if(!self::$case_sensitive) {
93+
$searched_value = strtolower($searched_value);
94+
$value = strtolower($value);
95+
}
96+
return ($value > $searched_value);
97+
}
98+
99+
private function matchNotBigger($value, $searched_value) {
100+
return !$this->matchBigger($value, $searched_value);
101+
}
102+
103+
private function matchStartsWith($value, $searched_value) {
104+
if(!self::$case_sensitive) {
105+
$searched_value = strtolower($searched_value);
106+
$value = strtolower($value);
107+
}
108+
return strncmp($value, $searched_value, strlen($searched_value)) === 0;
109+
}
110+
111+
private function matchNotStartsWith($value, $searched_value) {
112+
return !$this->matchStartsWith($value, $searched_value);
113+
}
114+
115+
private function matchEndsWith($value, $searched_value) {
116+
if(!self::$case_sensitive) {
117+
$searched_value = strtolower($searched_value);
118+
$value = strtolower($value);
119+
}
120+
return strlen($value) === 0 || substr($value, -strlen($searched_value)) === $searched_value;
121+
}
122+
123+
private function matchNotEndsWith($value, $searched_value) {
124+
return !$this->matchEndsWith($value, $searched_value);
125+
}
126+
127+
private function matchContains($value, $searched_value) {
128+
if(!self::$case_sensitive) {
129+
$searched_value = strtolower($searched_value);
130+
$value = strtolower($value);
131+
}
132+
return strpos($value, $searched_value) !== FALSE;
133+
}
134+
135+
private function matchNotContains($value, $searched_value) {
136+
return !$this->matchContains($value, $searched_value);
137+
}
138+
}

0 commit comments

Comments
 (0)