Skip to content

Commit 724431b

Browse files
committed
add new files
1 parent 6b5e305 commit 724431b

File tree

58 files changed

+6843
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+6843
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
<?php
2+
/**
3+
* This file contains the ArrArgumentsDescriptor class.
4+
*
5+
* PHP Version 5.3
6+
*
7+
* @category Tools_And_Utilities
8+
* @package Arr
9+
* @author Gonzalo Chumillas <[email protected]>
10+
* @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
11+
* @link https://github.com/soloproyectos/core
12+
*/
13+
namespace com\soloproyectos\core\arr\arguments;
14+
15+
/**
16+
* Class ArrArgumentsDescriptor.
17+
*
18+
* @category Tools_And_Utilities
19+
* @package Arr
20+
* @author Gonzalo Chumillas <[email protected]>
21+
* @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
22+
* @link https://github.com/soloproyectos/core
23+
*/
24+
class ArrArgumentsDescriptor {
25+
/**
26+
* List of variable types.
27+
* @var array
28+
*/
29+
private $_types;
30+
31+
/**
32+
* Default value.
33+
* @var mixed
34+
*/
35+
private $_default;
36+
37+
/**
38+
* Is a required argument?
39+
* @var boolean
40+
*/
41+
private $_isRequired;
42+
43+
/**
44+
* Constructor.
45+
*
46+
* @param array $types Variable types
47+
* @param mixed $default Default value
48+
* @param boolean $required Is a required argument?
49+
*
50+
* @return void
51+
*/
52+
public function __construct($types, $default = null, $required = false)
53+
{
54+
$this->_types = $types;
55+
$this->_default = $default;
56+
$this->_isRequired = $required;
57+
}
58+
59+
/**
60+
* Gets default value.
61+
*
62+
* @return mixed
63+
*/
64+
public function getDefault()
65+
{
66+
return $this->_default;
67+
}
68+
69+
/**
70+
* Is the argument required?
71+
*
72+
* @return boolean
73+
*/
74+
public function isRequired()
75+
{
76+
return $this->_isRequired;
77+
}
78+
79+
/**
80+
* Does the variable match with this descriptor?
81+
*
82+
* @return boolean
83+
*/
84+
public function match($var)
85+
{
86+
$ret = false;
87+
88+
foreach ($this->_types as $type) {
89+
if (array_search($type, array("*", "mixed")) !== false) {
90+
$ret = true;
91+
} elseif (array_search($type, array("number", "numeric")) !== false) {
92+
$ret = is_numeric($var);
93+
} elseif (array_search($type, array("bool", "boolean")) !== false) {
94+
$ret = is_bool($var);
95+
} elseif ($type == "string") {
96+
$ret = is_string($var);
97+
} elseif ($type == "array") {
98+
$ret = is_array($var);
99+
} elseif ($type == "object") {
100+
$ret = is_object($var);
101+
} elseif ($type == "resource") {
102+
$ret = is_resource($var);
103+
} elseif ($type == "function") {
104+
$ret = is_callable($var);
105+
} else {
106+
$ret = is_a($var, $type);
107+
}
108+
109+
if ($ret) {
110+
break;
111+
}
112+
}
113+
114+
return $ret;
115+
}
116+
}
+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php
2+
/**
3+
* This file contains the ArrArguments class.
4+
*
5+
* PHP Version 5.3
6+
*
7+
* @category Tools_And_Utilities
8+
* @package Arr
9+
* @author Gonzalo Chumillas <[email protected]>
10+
* @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
11+
* @link https://github.com/soloproyectos/core
12+
*/
13+
namespace com\soloproyectos\core\arr\arguments;
14+
use Exception;
15+
use com\soloproyectos\core\arr\Arr;
16+
use InvalidArgumentException;
17+
18+
/**
19+
* Class ArrArguments.
20+
*
21+
* @category Tools_And_Utilities
22+
* @package Arr
23+
* @author Gonzalo Chumillas <[email protected]>
24+
* @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
25+
* @link https://github.com/soloproyectos/core
26+
*/
27+
class ArrArguments
28+
{
29+
/**
30+
* Arguments.
31+
* @var array
32+
*/
33+
private $_arguments;
34+
35+
/**
36+
* Associative array of descriptors.
37+
* @var array
38+
*/
39+
private $_descriptors;
40+
41+
/**
42+
* @param array $arguments
43+
*
44+
* @return void
45+
*/
46+
public function __construct($arguments)
47+
{
48+
$this->_arguments = $arguments;
49+
}
50+
51+
/**
52+
* Registers a new descriptor.
53+
*
54+
* @param string $name Descriptor name
55+
* @param ArrArgumentsDescriptor $descriptor Argument descriptor
56+
*
57+
* @return void
58+
*/
59+
public function registerDescriptor($name, $descriptor)
60+
{
61+
$this->_descriptors[$name] = $descriptor;
62+
}
63+
64+
/**
65+
* Fetches elements from the arguments that matches specific descriptors.
66+
*
67+
* @return array associative array
68+
* @throws InvalidArgumentException
69+
*/
70+
public function fetch()
71+
{
72+
$ret = array();
73+
$pos = 0;
74+
$len = count($this->_arguments);
75+
76+
foreach ($this->_descriptors as $name => $descriptor) {
77+
$value = $descriptor->getDefault();
78+
79+
for ($i = $pos; ; $i++) {
80+
if ($i < $len && $descriptor->match($this->_arguments[$i])) {
81+
$value = $this->_arguments[$i];
82+
$pos = $i + 1;
83+
break;
84+
} else {
85+
if ($descriptor->isRequired()) {
86+
throw new InvalidArgumentException(
87+
"Argument is required: `$name`"
88+
);
89+
}
90+
if ($i > $len - 1) {
91+
break;
92+
}
93+
}
94+
}
95+
96+
$ret[$name] = $value;
97+
}
98+
99+
return $ret;
100+
}
101+
}

classes/arr/arr.php

+153
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
<?php
2+
/**
3+
* This file contains the Arr class.
4+
*
5+
* PHP Version 5.3
6+
*
7+
* @category Tools_And_Utilities
8+
* @package Arr
9+
* @author Gonzalo Chumillas <[email protected]>
10+
* @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
11+
* @link https://github.com/soloproyectos/core
12+
*/
13+
namespace com\soloproyectos\core\arr;
14+
use com\soloproyectos\core\arr\arguments\ArrArguments;
15+
use com\soloproyectos\core\arr\arguments\ArrArgumentsDescriptor;
16+
17+
/**
18+
* Class Arr.
19+
*
20+
* @category Tools_And_Utilities
21+
* @package Arr
22+
* @author Gonzalo Chumillas <[email protected]>
23+
* @license https://raw.github.com/soloproyectos/core/master/LICENSE BSD 2-Clause License
24+
* @link https://github.com/soloproyectos/core
25+
*/
26+
class Arr
27+
{
28+
29+
/**
30+
* Gets an attribute from a given array.
31+
*
32+
* @param array $arr Array object
33+
* @param string $name Attribute name
34+
* @param mixed $default Default value (default is null)
35+
*
36+
* @return mixed
37+
*/
38+
public static function get($arr, $name, $default = null)
39+
{
40+
return array_key_exists($name, $arr)? $arr[$name] : $default;
41+
}
42+
43+
/**
44+
* Sets an attribute.
45+
*
46+
* @param array $arr Array object (passed by reference)
47+
* @param string $name Attribute name
48+
* @param mixed $value Value
49+
*
50+
* @return void
51+
*/
52+
public static function set(&$arr, $name, $value)
53+
{
54+
$arr[$name] = $value;
55+
}
56+
57+
/**
58+
* Does the attribute exist?
59+
*
60+
* @param array $arr Array object
61+
* @param string $name Attribute name
62+
*
63+
* @return boolean
64+
*/
65+
public static function exist($arr, $name)
66+
{
67+
return array_key_exists($name, $arr);
68+
}
69+
70+
/**
71+
* Deletes an attribute.
72+
*
73+
* @param array $arr Array object (passed by reference)
74+
* @param string $name Attribute name
75+
*
76+
* @return void
77+
*/
78+
public static function del(&$arr, $name)
79+
{
80+
if (array_key_exists($name, $arr)) {
81+
unset($arr[$name]);
82+
}
83+
}
84+
85+
/**
86+
* Fetches the elements of an array that matches a given list of descriptors.
87+
*
88+
* <p>This function is especially suitable for getting optional arguments.
89+
* In the below example, 'title' is the only required argument. The
90+
* arguments 'message', 'x' and 'y' are not required and have default values.
91+
* The argument 'options' is not required and the default value is null. Note
92+
* that the arguments 'x' and 'y' can be either strings or numbers. Available
93+
* types are: '*' or 'mixed', 'number' or 'numeric', 'bool' or 'boolean',
94+
* 'string', 'array', 'objetc', 'resource', 'function'.</p>
95+
* <pre>
96+
* function test($title, $message, $x, $y, $options) {
97+
* $args = Arr::fetch(func_get_args(), array(
98+
* "title" => "string",
99+
* "message" => array(
100+
* "type" => "string",
101+
* "default" => "Default message ..."
102+
* ),
103+
* "x" => array(
104+
* "type" => "string|number",
105+
* "default" => 0
106+
* ),
107+
* "y" => array(
108+
* "type" => "string|number",
109+
* "default" => 0
110+
* ),
111+
* "options" => array(
112+
* "type" => "array",
113+
* required => false
114+
* )
115+
* );
116+
* print_r($args);
117+
* }
118+
* // this throws an InvalidArgumentException, as 'title' is required.
119+
* test(120, 250);
120+
* </pre>
121+
*
122+
* @param array $arr Array of mixed elements
123+
* @param array $descriptors Associative array of descriptors.
124+
*
125+
* @return array
126+
* @throws InvalidArgumentException
127+
*/
128+
public static function fetch($arr, $descriptors)
129+
{
130+
$args = new ArrArguments($arr);
131+
132+
foreach ($descriptors as $name => $descriptor) {
133+
$types = array();
134+
$default = null;
135+
$required = false;
136+
137+
if (is_string($descriptor)) {
138+
$types = explode("|", $descriptor);
139+
} elseif (is_array($descriptor)) {
140+
$types = explode("|", Arr::get($descriptor, "type"));
141+
$default = Arr::get($descriptor, "default");
142+
$required = Arr::get(
143+
$descriptor, "required", !Arr::exist($descriptor, "default")
144+
);
145+
}
146+
147+
$desc = new ArrArgumentsDescriptor($types, $default, $required);
148+
$args->registerDescriptor($name, $desc);
149+
}
150+
151+
return $args->fetch();
152+
}
153+
}

0 commit comments

Comments
 (0)