@@ -1415,27 +1415,68 @@ public function getStyle(AddressRange|CellAddress|int|string|array $cellCoordina
1415
1415
* included in a conditional style range.
1416
1416
* If a range of cells is specified, then the styles will only be returned if the range matches the entire
1417
1417
* range of the conditional.
1418
+ * @param bool $firstOnly default true, return all matching
1419
+ * conditionals ordered by priority if false, first only if true
1418
1420
*
1419
1421
* @return Conditional[]
1420
1422
*/
1421
- public function getConditionalStyles (string $ coordinate ): array
1423
+ public function getConditionalStyles (string $ coordinate, bool $ firstOnly = true ): array
1422
1424
{
1423
1425
$ coordinate = strtoupper ($ coordinate );
1424
- if (str_contains ( $ coordinate , ' : ' ) ) {
1426
+ if (preg_match ( ' /[: ,]/ ' , $ coordinate ) === 1 ) {
1425
1427
return $ this ->conditionalStylesCollection [$ coordinate ] ?? [];
1426
1428
}
1427
1429
1428
- $ cell = $ this ->getCell ($ coordinate );
1429
- foreach (array_keys ($ this ->conditionalStylesCollection ) as $ conditionalRange ) {
1430
- $ cellBlocks = explode (', ' , Coordinate::resolveUnionAndIntersection ($ conditionalRange ));
1431
- foreach ($ cellBlocks as $ cellBlock ) {
1432
- if ($ cell ->isInRange ($ cellBlock )) {
1433
- return $ this ->conditionalStylesCollection [$ conditionalRange ];
1430
+ $ conditionalStyles = [];
1431
+ foreach ($ this ->conditionalStylesCollection as $ keyStylesOrig => $ conditionalRange ) {
1432
+ $ keyStyles = Coordinate::resolveUnionAndIntersection ($ keyStylesOrig );
1433
+ $ keyParts = explode (', ' , $ keyStyles );
1434
+ foreach ($ keyParts as $ keyPart ) {
1435
+ if ($ keyPart === $ coordinate ) {
1436
+ if ($ firstOnly ) {
1437
+ return $ conditionalRange ;
1438
+ }
1439
+ $ conditionalStyles [$ keyStylesOrig ] = $ conditionalRange ;
1440
+
1441
+ break ;
1442
+ } elseif (str_contains ($ keyPart , ': ' )) {
1443
+ if (Coordinate::coordinateIsInsideRange ($ keyPart , $ coordinate )) {
1444
+ if ($ firstOnly ) {
1445
+ return $ conditionalRange ;
1446
+ }
1447
+ $ conditionalStyles [$ keyStylesOrig ] = $ conditionalRange ;
1448
+
1449
+ break ;
1450
+ }
1434
1451
}
1435
1452
}
1436
1453
}
1454
+ $ outArray = [];
1455
+ foreach ($ conditionalStyles as $ conditionalArray ) {
1456
+ foreach ($ conditionalArray as $ conditional ) {
1457
+ $ outArray [] = $ conditional ;
1458
+ }
1459
+ }
1460
+ usort ($ outArray , [self ::class, 'comparePriority ' ]);
1437
1461
1438
- return [];
1462
+ return $ outArray ;
1463
+ }
1464
+
1465
+ private static function comparePriority (Conditional $ condA , Conditional $ condB ): int
1466
+ {
1467
+ $ a = $ condA ->getPriority ();
1468
+ $ b = $ condB ->getPriority ();
1469
+ if ($ a === $ b ) {
1470
+ return 0 ;
1471
+ }
1472
+ if ($ a === 0 ) {
1473
+ return 1 ;
1474
+ }
1475
+ if ($ b === 0 ) {
1476
+ return -1 ;
1477
+ }
1478
+
1479
+ return ($ a < $ b ) ? -1 : 1 ;
1439
1480
}
1440
1481
1441
1482
public function getConditionalRange (string $ coordinate ): ?string
@@ -1465,19 +1506,7 @@ public function getConditionalRange(string $coordinate): ?string
1465
1506
*/
1466
1507
public function conditionalStylesExists (string $ coordinate ): bool
1467
1508
{
1468
- $ coordinate = strtoupper ($ coordinate );
1469
- if (str_contains ($ coordinate , ': ' )) {
1470
- return isset ($ this ->conditionalStylesCollection [$ coordinate ]);
1471
- }
1472
-
1473
- $ cell = $ this ->getCell ($ coordinate );
1474
- foreach (array_keys ($ this ->conditionalStylesCollection ) as $ conditionalRange ) {
1475
- if ($ cell ->isInRange ($ conditionalRange )) {
1476
- return true ;
1477
- }
1478
- }
1479
-
1480
- return false ;
1509
+ return !empty ($ this ->getConditionalStyles ($ coordinate ));
1481
1510
}
1482
1511
1483
1512
/**
0 commit comments