Skip to content

Commit 270051a

Browse files
committed
Code clean
1 parent 0a6d065 commit 270051a

File tree

3 files changed

+51
-91
lines changed

3 files changed

+51
-91
lines changed

core/Base/Enum.class.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public static function create($id)
3333
return new $className($id);
3434
}
3535

36-
final public function __construct($id)
36+
public function __construct($id)
3737
{
3838
$this->setId($id);
3939
}
@@ -51,9 +51,18 @@ public function unserialize($serialized)
5151
}
5252
//@}
5353

54+
/**
55+
* Array of object
56+
* @static
57+
* @return array
58+
*/
5459
public static function getList()
5560
{
56-
return static::$names;
61+
$list = array();
62+
foreach (array_keys(static::$names) as $id)
63+
$list[] = static::create($id);
64+
65+
return $list;
5766
}
5867

5968
/**
@@ -71,15 +80,16 @@ public function getId()
7180
return $this->id;
7281
}
7382

83+
84+
/**
85+
* Alias for getList()
86+
* @static
87+
* @deprecated
88+
* @return array
89+
*/
7490
public static function getObjectList()
7591
{
76-
$list = array();
77-
$names = static::$names;
78-
79-
foreach (array_keys($names) as $id)
80-
$list[] = static::create($id);
81-
82-
return $list;
92+
return static::getList();
8393
}
8494

8595
public function toString()

main/Base/MimeType.class.php

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -790,43 +790,35 @@ public static function wrap($id)
790790
/**
791791
* Return MimeType object by mime-type string
792792
* @param string $value
793-
* @throws ObjectNotFoundException
793+
* @throws MissingElementException
794794
* @return MimeType
795795
*/
796796
public static function getByMimeType($value)
797797
{
798798
$list = static::getNameList();
799-
foreach ( $list as $key => $mimeType )
800-
{
801-
if( mb_strtolower($mimeType) === mb_strtolower($value) )
802-
return new self($key);
803799

804-
}
800+
$id = array_search(mb_strtolower($value), $list);
801+
if ($id === false)
802+
throw new MissingElementException('Can not find similar mime type "'.$value.'" !');
805803

806-
throw new ObjectNotFoundException(
807-
'Can not find similar mime type "'.$value.'" !'
808-
);
804+
return new self($id);
809805
}
810806

811807
/**
812808
* Return MimeType object by extension without [dot] prefix
813809
* @param string $value
814-
* @throws ObjectNotFoundException
810+
* @throws MissingElementException
815811
* @return MimeType
816812
*/
817813
public static function getByExtension($value)
818814
{
819815
$list = static::getExtensionList();
820-
foreach ( $list as $key => $extension )
821-
{
822-
if( mb_strtolower( $extension ) === mb_strtolower($value) )
823-
return new self($key);
824816

825-
}
817+
$id = array_search(mb_strtolower($value), $list);
818+
if ($id === false)
819+
throw new MissingElementException('Can not find similar extension "'.$value.'" !');
826820

827-
throw new ObjectNotFoundException(
828-
'Can not find similar extension "'.$value.'" !'
829-
);
821+
return new self($id);
830822
}
831823

832824

@@ -841,17 +833,17 @@ public static function getExtensionList()
841833

842834
/**
843835
* Return extension without [dot] prefix.
844-
* @throws ObjectNotFoundException
836+
* @throws MissingElementException
845837
* @return string
846838
*/
847839
public function getExtension()
848840
{
849841
if(
850-
isset( $this->extensions[$this->id] )
842+
isset( static::$extensions[$this->id] )
851843
)
852-
return $this->extensions[$this->id];
844+
return static::$extensions[$this->id];
853845

854-
throw new ObjectNotFoundException(
846+
throw new MissingElementException(
855847
'Can not find "'.$this->id.'" in extensions map!'
856848
);
857849
}

meta/classes/MetaConfiguration.class.php

Lines changed: 18 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -505,16 +505,14 @@ class_exists($class->getName(), true)
505505

506506
if ($this->checkEnumerationRefIntegrity)
507507
{
508-
if($object instanceof Enumeration)
508+
if(
509+
$object instanceof Enumeration
510+
|| $object instanceof Enum
511+
)
509512
$this->checkEnumerationReferentialIntegrity(
510513
$object,
511514
$class->getTableName()
512515
);
513-
elseif($object instanceof Enum)
514-
$this->checkEnumReferentialIntegrity(
515-
$object,
516-
$class->getTableName()
517-
);
518516
}
519517

520518

@@ -1334,18 +1332,29 @@ private function checkClassSanity(
13341332
}
13351333

13361334
private function checkEnumerationReferentialIntegrity(
1337-
Enumeration $enumeration, $tableName
1335+
$enumeration, $tableName
13381336
)
13391337
{
1338+
Assert::isTrue(
1339+
(
1340+
$enumeration instanceof Enumeration
1341+
|| $enumeration instanceof Enum
1342+
),
1343+
'argument enumeation must be instacne of Enumeration or Enum! gived, "'.gettype($enumeration).'"'
1344+
);
1345+
13401346
$updateQueries = null;
13411347

13421348
$db = DBPool::me()->getLink();
13431349

13441350
$class = get_class($enumeration);
13451351

13461352
$ids = array();
1347-
1348-
$list = $enumeration->getObjectList();
1353+
1354+
if ($enumeration instanceof Enumeration)
1355+
$list = $enumeration->getList();
1356+
elseif ($enumeration instanceof Enum)
1357+
$list = ClassUtils::callStaticMethod($class.'::getList');
13491358

13501359
foreach ($list as $enumerationObject)
13511360
$ids[$enumerationObject->getId()] = $enumerationObject->getName();
@@ -1383,56 +1392,5 @@ private function checkEnumerationReferentialIntegrity(
13831392

13841393
return $this;
13851394
}
1386-
1387-
private function checkEnumReferentialIntegrity(
1388-
Enum $enum, $tableName
1389-
)
1390-
{
1391-
$updateQueries = null;
1392-
1393-
$db = DBPool::me()->getLink();
1394-
1395-
$class = get_class($enum);
1396-
1397-
$ids = array();
1398-
1399-
$list = ClassUtils::callStaticMethod($class.'::'.'getObjectList');
1400-
1401-
foreach ($list as $enumerationObject)
1402-
$ids[$enumerationObject->getId()] = $enumerationObject->getName();
1403-
1404-
$rows =
1405-
$db->querySet(
1406-
OSQL::select()->from($tableName)->
1407-
multiGet('id', 'name')
1408-
);
1409-
1410-
echo "\n";
1411-
1412-
foreach ($rows as $row) {
1413-
if (!isset($ids[$row['id']]))
1414-
echo "Class '{$class}', strange id: {$row['id']} found. \n";
1415-
else {
1416-
if ($ids[$row['id']] != $row['name']) {
1417-
echo "Class '{$class}',id: {$row['id']} sync names. \n";
1418-
1419-
$updateQueries .=
1420-
OSQL::update($tableName)->
1421-
set('name', $ids[$row['id']])->
1422-
where(Expression::eq('id', $row['id']))->
1423-
toDialectString($db->getDialect()) . ";\n";
1424-
}
1425-
1426-
unset($ids[$row['id']]);
1427-
}
1428-
}
1429-
1430-
foreach ($ids as $id => $name)
1431-
echo "Class '{$class}', id: {$id} not present in database. \n";
1432-
1433-
echo $updateQueries;
1434-
1435-
return $this;
1436-
}
14371395
}
14381396
?>

0 commit comments

Comments
 (0)