Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
19430d8d
Commit
19430d8d
authored
Jul 29, 2011
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
w
parent
d2159b12
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
27 additions
and
103 deletions
+27
-103
Dictionary.php
framework/base/Dictionary.php
+11
-28
Vector.php
framework/base/Vector.php
+16
-33
DictionaryTest.php
tests/unit/framework/base/DictionaryTest.php
+0
-20
VectorTest.php
tests/unit/framework/base/VectorTest.php
+0
-22
No files found.
framework/base/Dictionary.php
View file @
19430d8d
...
...
@@ -34,11 +34,6 @@ namespace yii\base;
class
Dictionary
extends
Component
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
/**
* @var boolean whether this vector is read-only or not.
* If the vector is read-only, adding or moving items will throw an exception.
*/
public
$readOnly
;
/**
* @var array internal data storage
*/
private
$_d
=
array
();
...
...
@@ -48,15 +43,13 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
* Initializes the dictionary with an array or an iterable object.
* @param mixed $data the initial data to be populated into the dictionary.
* This can be an array or an iterable object.
* @param boolean $readOnly whether the dictionary is read-only
* @throws Exception if data is not well formed (neither an array nor an iterable object)
*/
public
function
__construct
(
$data
=
array
()
,
$readOnly
=
false
)
public
function
__construct
(
$data
=
array
())
{
if
(
$data
!==
array
())
{
$this
->
copyFrom
(
$data
);
}
$this
->
readOnly
=
$readOnly
;
}
/**
...
...
@@ -119,16 +112,11 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/
public
function
add
(
$key
,
$value
)
{
if
(
!
$this
->
readOnly
)
{
if
(
$key
===
null
)
{
$this
->
_d
[]
=
$value
;
}
else
{
$this
->
_d
[
$key
]
=
$value
;
}
if
(
$key
===
null
)
{
$this
->
_d
[]
=
$value
;
}
else
{
throw
new
Exception
(
'Dictionary is read only.'
)
;
$this
->
_d
[
$key
]
=
$value
;
}
}
...
...
@@ -140,19 +128,14 @@ class Dictionary extends Component implements \IteratorAggregate, \ArrayAccess,
*/
public
function
remove
(
$key
)
{
if
(
!
$this
->
readOnly
)
{
if
(
isset
(
$this
->
_d
[
$key
]))
{
$value
=
$this
->
_d
[
$key
];
unset
(
$this
->
_d
[
$key
]);
return
$value
;
}
else
{
// the value is null
unset
(
$this
->
_d
[
$key
]);
return
null
;
}
if
(
isset
(
$this
->
_d
[
$key
]))
{
$value
=
$this
->
_d
[
$key
];
unset
(
$this
->
_d
[
$key
]);
return
$value
;
}
else
{
throw
new
Exception
(
'Dictionary is read only.'
);
else
{
// the value is null
unset
(
$this
->
_d
[
$key
]);
return
null
;
}
}
...
...
framework/base/Vector.php
View file @
19430d8d
...
...
@@ -40,11 +40,6 @@ namespace yii\base;
class
Vector
extends
Component
implements
\IteratorAggregate
,
\ArrayAccess
,
\Countable
{
/**
* @var boolean whether this vector is read-only or not.
* If the vector is read-only, adding or moving items will throw an exception.
*/
public
$readOnly
;
/**
* @var array internal data storage
*/
private
$_d
=
array
();
...
...
@@ -58,15 +53,13 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
* Initializes the vector with an array or an iterable object.
* @param mixed $data the initial data to be populated into the vector.
* This can be an array or an iterable object.
* @param boolean $readOnly whether the vector should be marked as read-only.
* @throws Exception if data is not well formed (neither an array nor an iterable object)
*/
public
function
__construct
(
$data
=
array
()
,
$readOnly
=
false
)
public
function
__construct
(
$data
=
array
())
{
if
(
$data
!==
array
())
{
$this
->
copyFrom
(
$data
);
}
$this
->
readOnly
=
$readOnly
;
}
/**
...
...
@@ -141,20 +134,15 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/
public
function
insertAt
(
$index
,
$item
)
{
if
(
!
$this
->
readOnly
)
{
if
(
$index
===
$this
->
_c
)
{
$this
->
_d
[
$this
->
_c
++
]
=
$item
;
}
elseif
(
$index
>=
0
&&
$index
<
$this
->
_c
)
{
array_splice
(
$this
->
_d
,
$index
,
0
,
array
(
$item
));
$this
->
_c
++
;
}
else
{
throw
new
Exception
(
'Index out of range: '
.
$index
);
}
if
(
$index
===
$this
->
_c
)
{
$this
->
_d
[
$this
->
_c
++
]
=
$item
;
}
elseif
(
$index
>=
0
&&
$index
<
$this
->
_c
)
{
array_splice
(
$this
->
_d
,
$index
,
0
,
array
(
$item
));
$this
->
_c
++
;
}
else
{
throw
new
Exception
(
'
Vector is read only.'
);
throw
new
Exception
(
'
Index out of range: '
.
$index
);
}
}
...
...
@@ -187,24 +175,19 @@ class Vector extends Component implements \IteratorAggregate, \ArrayAccess, \Cou
*/
public
function
removeAt
(
$index
)
{
if
(
!
$this
->
readOnly
)
{
if
(
$index
>=
0
&&
$index
<
$this
->
_c
)
{
$this
->
_c
--
;
if
(
$index
===
$this
->
_c
)
{
return
array_pop
(
$this
->
_d
);
}
else
{
$item
=
$this
->
_d
[
$index
];
array_splice
(
$this
->
_d
,
$index
,
1
);
return
$item
;
}
if
(
$index
>=
0
&&
$index
<
$this
->
_c
)
{
$this
->
_c
--
;
if
(
$index
===
$this
->
_c
)
{
return
array_pop
(
$this
->
_d
);
}
else
{
throw
new
Exception
(
'Index out of range: '
.
$index
);
$item
=
$this
->
_d
[
$index
];
array_splice
(
$this
->
_d
,
$index
,
1
);
return
$item
;
}
}
else
{
throw
new
Exception
(
'
Vector is read only.'
);
throw
new
Exception
(
'
Index out of range: '
.
$index
);
}
}
...
...
tests/unit/framework/base/DictionaryTest.php
View file @
19430d8d
...
...
@@ -37,12 +37,6 @@ class DictionaryTest extends \yii\test\TestCase
$this
->
assertEquals
(
2
,
$dictionary2
->
getCount
());
}
public
function
testReadOnly
()
{
$dictionary
=
new
\yii\base\Dictionary
(
array
(),
true
);
self
::
assertEquals
(
true
,
$dictionary
->
readOnly
,
'List is not read-only'
);
}
public
function
testGetCount
()
{
$this
->
assertEquals
(
2
,
$this
->
dictionary
->
getCount
());
...
...
@@ -63,13 +57,6 @@ class DictionaryTest extends \yii\test\TestCase
$this
->
assertTrue
(
$this
->
dictionary
->
contains
(
'key3'
));
}
public
function
testCanNotAddWhenReadOnly
()
{
$dictionary
=
new
\yii\base\Dictionary
(
array
(),
true
);
$this
->
setExpectedException
(
'yii\base\Exception'
);
$dictionary
->
add
(
'key'
,
'value'
);
}
public
function
testRemove
()
{
$this
->
dictionary
->
remove
(
'key1'
);
...
...
@@ -78,13 +65,6 @@ class DictionaryTest extends \yii\test\TestCase
$this
->
assertTrue
(
$this
->
dictionary
->
remove
(
'unknown key'
)
===
null
);
}
public
function
testCanNotRemoveWhenReadOnly
()
{
$dictionary
=
new
\yii\base\Dictionary
(
array
(
'key'
=>
'value'
),
true
);
$this
->
setExpectedException
(
'yii\base\Exception'
);
$dictionary
->
remove
(
'key'
);
}
public
function
testClear
()
{
$this
->
dictionary
->
clear
();
...
...
tests/unit/framework/base/VectorTest.php
View file @
19430d8d
...
...
@@ -37,14 +37,6 @@ class VectorTest extends \yii\test\TestCase
$this
->
assertEquals
(
2
,
$vector2
->
getCount
());
}
public
function
testReadOnly
()
{
$vector
=
new
\yii\base\Vector
(
array
(),
true
);
$this
->
assertEquals
(
true
,
$vector
->
readOnly
,
'List is not read-only'
);
$vector
=
new
\yii\base\Vector
(
array
(),
false
);
$this
->
assertEquals
(
false
,
$vector
->
readOnly
,
'List is read-only'
);
}
public
function
testGetCount
()
{
$this
->
assertEquals
(
2
,
$this
->
vector
->
getCount
());
...
...
@@ -70,13 +62,6 @@ class VectorTest extends \yii\test\TestCase
$this
->
vector
->
insertAt
(
4
,
$this
->
item3
);
}
public
function
testCanNotInsertWhenReadOnly
()
{
$vector
=
new
\yii\base\Vector
(
array
(),
true
);
$this
->
setExpectedException
(
'yii\base\Exception'
);
$vector
->
insertAt
(
1
,
2
);
}
public
function
testRemove
()
{
$this
->
vector
->
remove
(
$this
->
item1
);
...
...
@@ -99,13 +84,6 @@ class VectorTest extends \yii\test\TestCase
$this
->
vector
->
removeAt
(
2
);
}
public
function
testCanNotRemoveWhenReadOnly
()
{
$vector
=
new
\yii\base\Vector
(
array
(
1
,
2
,
3
),
true
);
$this
->
setExpectedException
(
'yii\base\Exception'
);
$vector
->
removeAt
(
2
);
}
public
function
testClear
()
{
$this
->
vector
->
clear
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment